Selenium使用

Selenium使用

项目地址:https://gitlab.com/Hyzeta/selenium_test

Selenium IDE自动录制

首先安装Firefox低版本,比如Firefox 54,然后安装Selenium IDE插件,注意选择低版本(比如2.9版)。

然后开启Selenium IDE,这个时候已经开始录制了。在浏览器中导航到https://psych.liebes.top/st开始进行一次登陆模拟。

Selenium IDE最终录取到的结果如下:

然后导出对应的JUnit4测试代码即可。

测试项目的编写

首先建立一个空的Gradle项目,并且改写build.gradle如下,以添加依赖。

group 'github.hyzeta'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.poi', name: 'poi', version: '3.9'
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.17'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.11.0'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

然后在test文件夹下建立对应的测试类,文件结构如下:

src/
├── main
└── test
    └── java
        └── github
            └── hyzeta
                └── GitURLTest.java

这里的GitURLTest.java即是使用的Selenium IDE自动导出的代码,但是这个代码还不能直接用,必须经过修改。

首先改写包名,原先的包名是com.example:

package github.hyzeta;

测试全体样本数据

首先要编写读取样本数据的代码

BufferedReader br = new BufferedReader(new FileReader("/Users/hyzeta/Downloads/input.csv"));
String line;
while ((line = br.readLine()) != null) {
    String[] record = line.split(",");
    if (record.length == 2) {
        String username = record[0].replaceAll("\\s+", "");
        String password = username.substring(4);
        String gitURL = getGeneralGitURL(record[1].replaceAll("\\s+", ""));
//                System.out.println(username + "->" + password + "->" + gitURL);
        System.out.println("Now start to check the URL[" + gitURL + "]");
        // start test
        // write your test code here...
        System.out.println("Successfully checked the URL[" + gitURL + "]");
        System.out.println("");
    }
}
br.close();

注意这里用了一个getGeneralGitURL的函数,是因为我在测试的过程中发现会有这种错误:

expected:<...ithub.com/3015218059[/]> but was:<...ithub.com/3015218059[]>
Expected :https://github.com/3015218059/
Actual   :https://github.com/3015218059

即多了或少了分隔符,那么干脆把样例中的和获取到的结果中的末尾分隔符全部去掉,这样就保证了一致性。

private static String getGeneralGitURL(String originURL) {
    if (originURL.charAt(originURL.length() - 1) == '/') {
        return originURL.substring(0, originURL.length() - 1);
    } else {
        return originURL;
    }
}

现在我们把测试代码填充到之前写的write your test code here...这块注释这里。完成之后的代码如下:

@Test
public void testGitURL() throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("/Users/hyzeta/Downloads/input.csv"));
    String line;
    while ((line = br.readLine()) != null) {
        String[] record = line.split(",");
        if (record.length == 2) {
            String username = record[0].replaceAll("\\s+", "");
            String password = username.substring(4);
            String gitURL = getGeneralGitURL(record[1].replaceAll("\\s+", ""));
//                System.out.println(username + "->" + password + "->" + gitURL);
            System.out.println("Now start to check the URL[" + gitURL + "]");
            // start test
            driver.get(baseUrl + "/st");
            driver.findElement(By.id("username")).clear();
            driver.findElement(By.id("username")).sendKeys(username);
            driver.findElement(By.id("password")).clear();
            driver.findElement(By.id("password")).sendKeys(password);
            driver.findElement(By.id("submitButton")).click();
            assertEquals(gitURL, getGeneralGitURL(driver.findElement(By.cssSelector("p.login-box-msg")).getText()));
            System.out.println("Successfully checked the URL[" + gitURL + "]");
            System.out.println("");
        }
    }
    br.close();
}

最后,必须注意的是,一定要在setUp的过程的开头注册geckodriver的具体位置。

@Before
public void setUp() throws Exception {
    System.setProperty("webdriver.gecko.driver", "/Users/hyzeta/Packages/geckodriver/bin/geckodriver");
    driver = new FirefoxDriver();
    baseUrl = "https://psych.liebes.top";
    driver.manage().timeouts().implicitlyWait(900, TimeUnit.SECONDS);
}

测试

选择Gradle Target,这里由于做的是测试,所以选择[clean test]这两个target。

在Clean过程完成后,Test开始,可以看到每个样例都得到了测试。最终的测试结果如下:

全部测试通过。

另外我将测试的中间输出保存为了日志文件,即项目根目录下的check_log.txt,点击此处下载

posted @ 2018-04-15 17:50  Absintheme  阅读(148)  评论(0编辑  收藏  举报