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

2018-09-23

ChromeDriver

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

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

pom.xml

<?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

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();");
        }
    }
}