【1. 加入 jUnit】
Selenium 环境搞定,接下来就要想办法让我们的测试步入正轨了,对于 java 来说,用上 jUnit 是很方便的,maven 项目加入 jUnit 也是轻而易举,稍微修改一下 pom.xml 就可以了
01 | < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
02 | xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > |
03 | < modelVersion >4.0.0</ modelVersion > |
04 | < groupId >Selenium2Test</ groupId > |
05 | < artifactId >Selenium2Test</ artifactId > |
06 | < version >1.0</ version > |
09 | < groupId >org.seleniumhq.selenium</ groupId > |
10 | < artifactId >selenium-java</ artifactId > |
11 | < version >2.25.0</ version > |
14 | < groupId >com.opera</ groupId > |
15 | < artifactId >operadriver</ artifactId > |
18 | < groupId >junit</ groupId > |
19 | < artifactId >junit</ artifactId > |
20 | < version >4.8.2</ version > |
24 | < dependencyManagement > |
27 | < groupId >com.opera</ groupId > |
28 | < artifactId >operadriver</ artifactId > |
29 | < version >0.16</ version > |
32 | < groupId >org.seleniumhq.selenium</ groupId > |
33 | < artifactId >selenium-remote-driver</ artifactId > |
38 | </ dependencyManagement > |
把上一篇中测试 FireFox 的代码拿来按照 jUnit 的结构适当修改一下,就可以直接利用 jUnit 进行测试了
05 | import org.junit.AfterClass; |
06 | import org.junit.BeforeClass; |
08 | import org.openqa.selenium.By; |
09 | import org.openqa.selenium.WebDriver; |
10 | import org.openqa.selenium.WebElement; |
11 | import org.openqa.selenium.firefox.FirefoxDriver; |
12 | import org.openqa.selenium.support.ui.ExpectedCondition; |
13 | import org.openqa.selenium.support.ui.WebDriverWait; |
15 | public class ExampleJunit { |
17 | static WebDriver driver; |
20 | public static void init() { |
21 | System.out.println( "init..." ); |
23 | System.setProperty( "webdriver.firefox.bin" , "D:\\Program Files\\Mozilla Firefox\\firefox.exe" ); |
25 | driver = new FirefoxDriver(); |
31 | driver.get( "http://www.ztree.me/v3/demo/cn/core/standardData.html" ); |
34 | ( new WebDriverWait(driver, 10 )).until( new ExpectedCondition<Boolean>() { |
35 | public Boolean apply(WebDriver d) { |
36 | WebElement element = driver.findElement(By.id( "treeDemo" )); |
37 | return element.findElement(By.tagName( "a" )) != null ; |
41 | WebElement element = driver.findElement(By.id( "treeDemo" )); |
42 | List<WebElement> elements = element.findElements(By.tagName( "li" )); |
44 | System.out.println( "treeNode DOM length = " + elements.size()); |
48 | public static void destory() { |
49 | System.out.println( "destory..." ); |
注意:
如果在一个 java 文件中的 @Test 比较多,那么需要注意 @Before / @After 和 @BeforeClass / @AfterClass 是不一样的,前者是每一个 @Test 执行前后都会运行;后者只执行一次(详细请参考 jUnit 的文档)。
【2. 执行 js】
想测试 zTree ,那么大量的测试工作都是要依靠 js 的,所以让 Selenium 与 js 通信是至关重要的,查一下文档,建立起来简单的通信是不难的。
1 | ((JavascriptExecutor)driver).executeScript(<span class = "hljs-string" style= "color:#CC9393;" > "var a = 0;" </span>); |
只要在这里面把需要执行的 js 写好就可以了,不过问题也就来了:
1、executeScript 执行 js 时应该是一个闭包的环境,所以这里面 var 定义的变量是局部变量,不可能是全局对象,使用此变量的方法都要在这一个 executeScript 里面写好全部代码。
2、一个 executeScript 只能 返回一个Object,如果你要做同一个测试,返回一个 JSON 对象中的多个值,难道要同一段代码复制好几遍吗? 这样当然不是个好的解决方案,所以我们可以利用 window 实现全局变量,这样可以将一个复杂的 executeScript 分解为多个简单的 executeScript 了。
3、executeScript 的返回值是 Object,在 API 中有详细的描述 Object 与 js 值的对应关系,但注意可是不能直接返回 JSON 对象的。
js 返回 HTML element, Object 可以转换为一个 WebElement
js 返回 decimal,Object 可以转换为一个 Double
js 返回 non-decimal number, Object 可以转换为一个 Long
js 返回 boolean, Object 可以转换为一个 Boolean
js 返回 其他对象, Object 可以转换为一个 String
js 返回 array, Object 可以转换为一个 List<Object>.
js 没有返回值 或 返回 null, executeScript 的返回值就是 null
详细情况建议大家直接参阅 API 文档
03 | import org.junit.AfterClass; |
04 | import org.junit.BeforeClass; |
06 | import org.openqa.selenium.By; |
07 | import org.openqa.selenium.JavascriptExecutor; |
08 | import org.openqa.selenium.WebDriver; |
09 | import org.openqa.selenium.WebElement; |
10 | import org.openqa.selenium.firefox.FirefoxDriver; |
11 | import org.openqa.selenium.support.ui.ExpectedCondition; |
12 | import org.openqa.selenium.support.ui.WebDriverWait; |
14 | public class ExampleJs { |
16 | static WebDriver driver; |
19 | public static void init() { |
20 | System.out.println( "init..." ); |
22 | System.setProperty( "webdriver.firefox.bin" , "D:\\Program Files\\Mozilla Firefox\\firefox.exe" ); |
24 | driver = new FirefoxDriver(); |
30 | driver.get( "http://www.ztree.me/v3/demo/cn/core/standardData.html" ); |
33 | ( new WebDriverWait(driver, 10 )).until( new ExpectedCondition<Boolean>() { |
34 | public Boolean apply(WebDriver d) { |
35 | WebElement element = driver.findElement(By.id( "treeDemo" )); |
36 | return element.findElement(By.tagName( "a" )) != null ; |
40 | System.out.println( "start...javascript" ); |
51 | ((JavascriptExecutor)driver).executeScript( "window.zTreeObj = $.fn.zTree.getZTreeObj('treeDemo');" ); |
52 | String name =(String) ((JavascriptExecutor)driver).executeScript( "return window.zTreeObj.getNodes()[0].name;" ); |
53 | Long rootCount = (Long) ((JavascriptExecutor)driver).executeScript( "return window.zTreeObj.getNodes().length;" ); |
56 | System.out.println( "treeObj[0].name = " + name); |
57 | System.out.println( "root nodes count = " + rootCount); |
61 | public static void destory() { |
62 | System.out.println( "destory..." ); |
附录:
Selenium 的 java API文档: http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html?index-all.html