使用Page Object Model通过TestNG执行测试时发生java.lang.NullPointerException

时间:2017-06-13 04:48:23

标签: java selenium-webdriver nullpointerexception testng pom.xml

使用Page Object Model通过TestNG执行测试时发生java.lang.NullPointerException。以下是类详细信息:

Project Structure

类Confg:

package com.wordpress.config;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

public class Confg {
    public WebDriver driver;
    String pageURL="http://demosite.center/wordpress/wp-admin/index.php";

    public WebDriver getdriver()
    {
        return driver;
    }

    @Parameters({"browser"})
    @BeforeClass
     public void invokeBrowser(String browser){

        if(browser.equalsIgnoreCase("chrome"))
        {
        System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
        driver=new ChromeDriver();
        driver.get(pageURL);
        }

    }

}

类登录页面:

package com.wordpress.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {

        public WebDriver driver;
        public String title;
        String username = "****";
        String password = "***";
        By usernmeTextBox = By.xpath(".//*[@id='user_login']");
        By passwordTextBox = By.xpath(".//*[@id='user_pass']");
        By loginButton = By.xpath(".//*[@id='wp-submit']");
        public String expectedURL="http://demosite.center/wordpress/wp-admin/index.php";


        public LoginPage(WebDriver driver) {
            this.driver = driver;
        }


        public void loginHome() {
            driver.findElement(usernmeTextBox).sendKeys(username);
            driver.findElement(passwordTextBox).sendKeys(password);
            driver.findElement(loginButton).click();

        }

        public void verifyTitle(){
            title=driver.getTitle();
            System.out.println(title);
        }

    }

类LoginPageTest:

package com.wordpress.test;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.wordpress.config.*;
import com.wordpress.pages.LoginPage;

public class LoginPageTest extends Confg {

    LoginPage login = new LoginPage(driver);

    @BeforeClass
    void setup() {
        driver = getdriver();
    }

    @Test
    void verifyLogin() {
        // LoginPage login = new LoginPage(driver);
        login.loginHome();
        System.out.println(driver.getCurrentUrl());

        try {
            Assert.assertEquals(login.expectedURL, driver.getCurrentUrl());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test(dependsOnMethods="verifyLogin")
    void verifyPageTitle() {
        login.verifyTitle();

        try {
            Assert.assertEquals(login.expectedURL,login.title);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

的testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Test">
        <parameter name="browser" value="Chrome"/>
         <classes>
            <class name="com.wordpress.config.Confg" />
            <class name="com.wordpress.test.LoginPageTest"/>
         </classes>
     </test> <!-- Test -->
</suite> <!-- Suite -->

以下是testng跟踪:

java.lang.NullPointerException
    at com.wordpress.pages.LoginPage.loginHome(LoginPage.java:32)
    at com.wordpress.test.LoginPageTest.verifyLogin(LoginPageTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)

问题:

执行上述testng.xml时发生java.lang.NullPointerException。我试图调试这个问题。当我创建一个LoginPage类的实例时,似乎驱动程序值传递为 null

如果我在LoginPageTest类中本地实例化LoginPage类,则代码可以正常工作。但我想在LoginPageTest类中全局创建LoginPage类的对象,以便我可以再次使用它来调用LoginPage类的其他方法。

请帮我找到空指针异常的原因..

注意:它与上述问题不重复,因为testNg的控制流程与建议问题中回答的普通java执行不同。

0 个答案:

没有答案