目录

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

安装ChromeDriver

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

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

添加Maven依赖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<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>

使用案例:自动登录

 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://xxx.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();");
        }
    }
}

评论