使用Selenium-Java实现自动化UI测试

ChromeDriver

https://npm.taobao.org/mirrors/chromedriver/

下载指定版本的 chromedriver 后,放入 PATH 中。

pom.xml

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.wuwenze.test</groupId>
<artifactId>selenium-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>
</project>

Test 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.wuwenze.test;

import lombok.extern.java.Log;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Log
public class TestNgSelenium {
private String baseUrl = "http://wuwenze.itkeeping.com";
private ChromeDriver webDriver = null;

@BeforeClass
public void before() {
webDriver = new ChromeDriver();
webDriver.manage().window().fullscreen();
webDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
webDriver.get(baseUrl);
}

@Test
public void login() throws InterruptedException {
// 进入登录页面
webDriver.findElementByClassName("btn-login").click();
Thread.sleep(1000);

// 输入用户名密码
List<WebElement> elements = webDriver.findElements(By.tagName("input"));
Assert.assertNotNull(elements);
Assert.assertEquals(elements.size(), 3);
elements.get(0).sendKeys("17311223306");
elements.get(1).sendKeys("xxx");
elements.get(2).click(); //取消记住密码
webDriver.findElementByClassName("btn-login").click();

Thread.sleep(3000);
// 验证是否登录成功
Assert.assertEquals("客服-我的易维帮助台", webDriver.getTitle());
}

@AfterClass
public void after() {
if (null != webDriver) {
webDriver.quit();
log.info("webDriver.quit();");
}
}
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×