作为Maven测试运行时,TestNG测试失败,但作为TestNG套件

时间:2017-02-08 19:59:28

标签: java selenium selenium-webdriver testng

我正在使用TestNG运行自动化Selenium测试。当我作为TestNG Suite运行时,一切正常。但是当我作为Maven Test运行时,它们会失败。我非常困惑为什么他们会在一种情况下而不是另一种情况下工作。以下是测试的总体布局:

Base.java

public class Base {

    public WebDriver driver = null;

    public WebDriver getLocalDriver() {
        driver = new FirefoxDriver();
        return driver;
    }

TestBase.java

public class TestBase extends Base {

    @BeforeTest
    public void beforeTest() {
        Base b = new Base();
        driver = b.getLocalDriver();
    }

    @AfterTest
    public void afterTest() {
        driver.quit();
    }

LoginTests.java

public class LoginTests extends TestBase {

    @Test
    public void TestOne() {

        driver.get("http://www.company.com");
        // ^^^ driver == null right here (line 28)
    }

错误讯息:

java.lang.NullPointerException
    at com.company.automation.ecom.tests.LoginTests.TestOne(LoginTests.java:28)

编辑:pom.xml

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <groupId>com.company.automation</groupId>
    <artifactId>ecom</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ecom</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
            <!--<scope>test</scope> -->
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>1.5.0</version>
        </dependency>

    </dependencies>
</project>

的testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ECOM Suite" parallel="tests" thread-count="2">

    <test verbose="2" name="Chrome">
        <parameter name="browserName" value="chrome"/>
        <parameter name="nodeURL" value="http://10.11.25.170:5552/wd/hub"/>
        <packages>
            <package name="com.company.*"></package>
        </packages>
    </test>

    <test verbose="2" name="Firefox">
        <parameter name="browserName" value="firefox"/>
        <parameter name="nodeURL" value="http://10.11.25.170:5551/wd/hub"/>
        <packages>
            <package name="com.company.*"></package>
        </packages>
    </test>

</suite>

2 个答案:

答案 0 :(得分:4)

  

我非常困惑为什么他们会在一种情况下工作而不是   另一个。

当我第一次发现TestNG(好)使用多线程时,我也是如此。

尝试将@BeforeTest@AfterTest注释更改为@BeforeMethod@AfterMethod。这将是最简单的解决方案。

除了坚持WebDriver静态ThreadLocal可能是成功的关键。

考虑一下:

public class TestBase {

    protected static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
    protected String abc;

    @BeforeMethod
    public void setUp(){
        System.out.println("I am in setUp method.");

        //WebDriver instantiation etc.
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized", "--disable-cache");
        webDriver.set(new ChromeDriver(options));
        webDriver.get().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @AfterMethod
    public void tearDown(){
        System.out.println("I am in tearDown method.");
        //You can clean up after tests.
        //webDriver.get().quit();
        webDriver.get().quit();
    }
}

在两个单独的文件中:

public class Test1 extends TestBase {

    @Test
    public void goToGoogle() {
        System.out.println("Google");
        String googleURL = "http://google.com/";
        webDriver.get().get(googleURL);
        assertTrue(webDriver.get().getCurrentUrl().contains("google"), "We are on Google!");
    }
}

public class Test2 extends TestBase {

    @Test
    public void goToStackOverflow() {
        System.out.println("SO!");
        String stackOverflowAddress = "http://stackoverflow.com/";
        webDriver.get().get(stackOverflowAddress);
        assertTrue(webDriver.get().getCurrentUrl().contains("stack"), "We are on SO!");
    }
}

答案 1 :(得分:1)

您可以使用的伴侣:

    <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-testng</artifactId>
        <version>3.0.0-M3</version>
      </dependency>

我的maven测试是这样执行的。

谢谢