《Java测试驱动开发》示例中如何运行应该注意的问题

最近再读关于如何提高程序员的工作效率,以及《Java测试驱动开发》,针对文章的示例,比如使用自动化测试工具Seleium和Selenide的代码有几个地方需要注意下:

1.升级Jar包,build.gradle进行修改,如果不升级Jar包,汇报连接超时的错误,具体代码如下图:

dependencies {
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-all:1.3'

    testCompile 'org.seleniumhq.selenium:selenium-java:3.141.59'
    testCompile 'com.codeborne:selenide:4.14.2'

    testCompile 'info.cukes:cucumber-java:1.2.2'
    testCompile 'info.cukes:cucumber-junit:1.2.2'

    testCompile 'org.jbehave:jbehave-core:4.3.5'
    testCompile 'org.jbehave.site:jbehave-site-resources:3.2@zip'
}

2.SeleniumTest.java和SelenideTest.java添加几句代码:

package com.packtpublishing.tddjava.ch02friendships;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;

public class SeleniumTest {
    @Test
    public void wikipediaSearchFeature() throws InterruptedException {

        // declaration and instantiation of objects/variables
        System.setProperty("webdriver.gecko.driver", ".\\tools\\geckodriver.exe");
        System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");

        // Declaring the web driver used for web browsing
        WebDriver driver = new FirefoxDriver();

        // Opening Wikipedia page
        driver.get("https://wiki.czm233.tk/wiki/Main_Page");

        // Searching TDD
        WebElement query = driver.findElement(By.name("search"));
        query.sendKeys("Test-driven development");

        // Clicking search button
        WebElement goButton = driver.findElement(By.name("go"));
        goButton.click();

        // Checks
        assertThat(driver.getTitle(), startsWith("Test-driven development"));

        driver.quit();
    }
}
package com.packtpublishing.tddjava.ch02friendships;

import org.junit.Test;
import org.openqa.selenium.By;

import static com.codeborne.selenide.Selenide.*;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;

public class SelenideTest {
    @Test
    public void wikipediaSearchFeature() throws InterruptedException {
        // declaration and instantiation of objects/variables
        System.setProperty("webdriver.gecko.driver", ".\\tools\\geckodriver.exe");
        System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");

        // Opening Wikipedia page
        open("https://wiki.czm233.tk/wiki/Main_Page");

        // Searching TDD
        $(By.name("search")).setValue("Test-driven development");

        // Clicking search button
        $(By.name("go")).click();

        // Checks
        assertThat(title(), startsWith("Test-driven development"));
    }
}

 

你可能感兴趣的:(测试)