编程语言
首页 > 编程语言> > java-如果我通过testng.xml运行测试,则获取NullPointerException,但一次运行正常

java-如果我通过testng.xml运行测试,则获取NullPointerException,但一次运行正常

作者:互联网

序言:我是自动化测试的新手

我在两次测试中遇到问题,我尝试将它们作为一个套件运行在testng.xml中.但是,如果我分开运行它们,它们会正常工作.
这是我的代码:
BasePage.java

public class BasePage {
    protected WebDriver driver;
    public BasePage(WebDriver driver) {
        this.driver = driver;
    }
}

BaseTest.java

public class BaseTest {
    private WebDriver driver;
    private final String chromeDriverPath = "src//main//resources//chromedriver.exe";
    private final String firefoxDriverPath = "src//main//resources//geckodriver.exe";

    public WebDriver getDriver() {
        return driver;
    }

    @BeforeTest
    @Parameters({"browser", "baseURL"})
    public void setUp(@Optional String browser, @Optional String baseURL) {
        driver = getBrowserType(browser);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.navigate().to(baseURL);
    }

    private WebDriver getBrowserType(String browserType) {
        if (driver == null ) {
            if (browserType.equalsIgnoreCase("firefox")) {
                 System.setProperty("webdriver.gecko.driver", firefoxDriverPath);
                 return  new FirefoxDriver();
            }
            else if (browserType.equalsIgnoreCase("chrome")) {
                System.setProperty("webdriver.chrome.driver", chromeDriverPath);
                return new ChromeDriver();
            }
        }
        return driver;
    }

    @AfterTest
    public void tearDown() {
        if (driver != null)
            driver.quit();
    }
}

HomePage.java

public class HomePage extends BasePage {

    public HomePage(WebDriver driver) {
        super(driver);
    }

    public boolean verifyHomePage() {
        return driver.getCurrentUrl().contains("url");
    }
}

NavigationMenuComponent.java

public class NavigationMenuComponent extends BasePage{

    public NavigationMenuComponent(WebDriver driver) {
        super(driver);
    }

    @FindBy(xpath = "//ul[@class='right']//a[contains(@href, '/about')]")
    private WebElement aboutLink;

    public void test() {
         aboutLink.click();
    }
}

HomePageTest.java

public class HomePageTest extends BaseTest {

    private HomePage homePage;

    @BeforeClass(alwaysRun = true)
    public void init() {
        homePage = PageFactory.initElements(getDriver(), HomePage.class);
    }

    @Test(groups = "home-page")
    public void verifyFirstPage() {
        Assert.assertTrue(homePage.verifyHomePage());
    }
    }

NavigationMenuTest.java

public class NavigationMenuTest extends BaseTest{

    private NavigationMenuComponent navigationMenuComponent;

    @BeforeClass
    public void init() {
        navigationMenuComponent = PageFactory.initElements(getDriver(), NavigationMenuComponent.class);
    }

    @Test(groups = "about-page")
    public void verifyAboutLink() {
       navigationMenuComponent.test();
    }
}

还有我的testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suite" parallel="tests" thread-count="2">
    <parameter name="baseURL" value="https://www.foodpanda.com/"></parameter>
    <test name="chromeTest" >
        <parameter name="browser" value="chrome"></parameter>
        <classes>
            <class name="tests.HomePageTest"></class>
            <class name="tests.NavigationMenuTest"></class>
        </classes>
    </test>
    <test name="firefoxTest">
        <parameter name="browser" value="firefox"></parameter>
        <classes>
            <class name="tests.HomePageTest"></class>
            <class name="tests.NavigationMenuTest"></class>
        </classes>
    </test>
</suite>

这是stacktrace

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy9.click(Unknown Source)
    at utils.NavigationMenuComponent.test(NavigationMenuComponent.java:36)
    at tests.NavigationMenuTest.verifyAboutLink(NavigationMenuTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我被困在这一个.我应该单独运行它们还是做错了吗?

解决方法:

问题在于如何初始化webdriver实例.下面的代码片段

@BeforeTest
@Parameters({"browser", "baseURL"})
public void setUp(@Optional String browser, @Optional String baseURL) {
    driver = getBrowserType(browser);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.navigate().to(baseURL);
}

导致setUp方法在每个< test>中仅被调用一次.标记,因为它是两个测试类的通用基类的一部分.

因此,如果您的< test>中有两个测试类,标签,例如

<test name="chromeTest" >
    <parameter name="browser" value="chrome"></parameter>
    <classes>
        <class name="tests.HomePageTest"></class>
        <class name="tests.NavigationMenuTest"></class>
    </classes>
</test>

那么setUp方法将仅为HomePageTest调用,并在执行NavigationMenuTest时被跳过

这就解释了为什么您在< test>中看到第二个测试类的NullPointerException的原因.标签.您可能需要通过使用@BeforeClass而不是@BeforeTest注释setUp方法来更改此设置

标签:selenium,testing,automated-tests,testng,java
来源: https://codeday.me/bug/20191025/1932093.html