Board logo

标题: 使用 Selenium 实现基于 Web 的自动化测试(3) Selenium IDE 导出 JUnit 测试用例 [打印本页]

作者: look_w    时间: 2018-10-17 19:14     标题: 使用 Selenium 实现基于 Web 的自动化测试(3) Selenium IDE 导出 JUnit 测试用例

使用 Selenium IDE 导出 JUnit 测试用例在 Selenium IDE 中执行成功后,就可以把测试脚本导出成 JUnit 测试用例了,如图 6 所示:
图 6. 导出 JUnit 代码导出用例如下:
清单 1. VerifyLogin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class VerifyLogin extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
    selenium = new DefaultSelenium("localhost", 4444, "*chrome", "localhost:8422/");
    selenium.start();
}

@Test
public void testVerifyDirectorLogin() throws Exception {
    selenium.setTimeout("300000");
    selenium.open("/ibm/console/logon.jsp");
    selenium.type("id=j_username", "test");
    selenium.type("id=j_password", "test");
    selenium.click("id=other");
    selenium.waitForPageToLoad("300000");
    verifyTrue(selenium.isTextPresent("IBM Systems Director"));
}

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




说明:首先实例化一个 DefaultSelenium 对象,传入的参数分别是 RC 服务器 IP、端口、浏览器类型和待测试的 Server 的信息。然后在测试方法里调用 Selenium 1 的 API,这里的代码完全由 IDE 生成,就为我们省去了很多重复代码工作。
运行测试用例:
有了基于 JUnit 的运行测试用例就可以把它导入到 Java IDE 中执行测试了。执行中既需要客户端驱动支持(用于 Eclipse 编译),也需要启动 RC Server:
Selenium RC Server 下载:
Selenium Client Driver:
执行命令 java –jar selenium-server-standalone-2.5.0.jar 启动 Selenium RC Server:
图 7. 启动 Selenium RC Server启动后就可以直接在 Eclipse 中运行测试用例,RC Server 就会启动新窗口并自动按照录制脚本进行测试。并可在 Eclipse 中查看运行结果。
下面让我们看看怎样脱离 Eclipse 自己搭建一个可以持续测试的容器。




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0