四.Selenium与Junit结合

前提:必须安装了eclipse,且在eclipse中新建了project,如project名称为test

1.对test加载selenium-server-standalone-2.15.0.jar包及Junit4包

2.将Selenium IDE中录制的脚本转换成Junit 4(options-->format-->Junit 4(Remote control)),对转换后的脚本进行复制,拷贝至eclipse的某个新建测试类中

3.修改代码:在setup()方法中增加Selenium RC启动方法,否则运行失败

public class SampleTest extends SeleneseTestCase {

SeleniumServer SELENIUM_SERVER;
 @Before
 public void setUp() throws Exception {
  RemoteControlConfiguration rcc = new RemoteControlConfiguration();
  rcc.setPort(4444); 
  SELENIUM_SERVER = new SeleniumServer(rcc);
  SELENIUM_SERVER.start();

   //若Firefox是默认安装,则不需要指定路径“*firefox”即可;最后参数为URL
  selenium = new DefaultSelenium("localhost", 4444, "*firefox D:/Program Files/Mozilla Firefox 4.0.1/firefox.exe", "http://ip:8085/xx/aa.jsf/");
  selenium.start();
 }

@Test
 public void testUntitled() throws Exception {
  selenium.open("/xx/aa.jsf");
  assertEquals("xx系统", selenium.getTitle());//获取URL标题
  selenium.type("id=ext-comp-12", "admin");//输入用户名
  assertTrue(selenium.isElementPresent("xpath=//input[@id='ext-comp-1012']"));
  selenium.type("id=ext-comp-13", "123");//输入密码
  Thread.sleep(2000);//等待2S装载数据
  selenium.clickAt("id=ext-gen4","登录");//点击登录按钮
  selenium.waitForPageToLoad("30000");

  .....

  .....
 

 }

 @After
 public void tearDown() throws Exception {
  selenium.stop();
  SELENIUM_SERVER.stop();
 }

 注:运行Testcase时出现错误,建议在https://groups.google.com/forum/#!forum/selenium-users中查找问题

原文地址:https://www.cnblogs.com/keeping/p/2296447.html