执行Selenium脚本时出错-java.lang.NullPointerException-TestNG

时间:2018-07-29 18:03:59

标签: selenium testng

我正在尝试通过testNG运行脚本。但是正在获取java.lang.NullPointerException。我已经在BeforeMethod中放入了驱动程序初始化代码,并在Test下放入了其余代码。请让我知道我的脚本中出了什么问题,这是由于我收到错误而引起的。谢谢。

我的脚本:

public class NewTest {

    WebDriver driver = null;
    @BeforeMethod
    public void beforeMethod() {
        System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://" + Constants.auth_Username + ":" + Constants.auth_Password + "@" + Constants.URL);
    }

    @Test
    public void f() throws InterruptedException {
        actions.Admin_Login.txbx_aUsername(driver).sendKeys(Constants.aUsername);
        actions.Admin_Login.txbx_aPassword(driver).sendKeys(Constants.aPassword);
        actions.Admin_Login.btn_login(driver).click();
        actions.create(driver).click();
        Actions action = new Actions(driver);
        action.moveToElement(actions.create.list(driver)).build().perform();


        actions.create_list.list_category(driver).click();
        actions.create_list.list_ceate_category(driver).click();
        actions.create_list.txtbx_Cat_tile(driver).sendKeys(Constants.list_title);
        actions.create_list.btn_Cat_Save(driver).click();
        System.out.println("List Created Successfully");
    }
}

我正在使用PageObjects来获取数据。如果没有testNG,此脚本可以正常运行。但是,当我将测试转换为testNG并执行时,它将引发错误。

我面临的错误:

java.lang.NullPointerException
    at actions.Admin_Login.txbx_aUsername(Admin_Login.java:13)
    at testScripts.NewTest.f(NewTest.java:43)
    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:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

常量类:

public class Constants {

    public static final String URL_Shika = "url";

    public static final String list_Title = "Test";

    public static final String aUsername = "Test";

    public static final String aPassword = "abcd123";

    public static final String uUsername = "Test01";

    public static final String uPassword = "abcd123";

    public static final String vUsername = "test321";

    public static final String vPassword = "abcd123";

    public static final String auth_Username = "admin";

    public static final String auth_Password = "admin@123";

    public static final String FF_Driver = "/path/geckodriver";

    public static final String Chrome_Driver = "/path/chromedriver";

}

动作类:

public class Admin_Login {

        private static WebElement element = null;

        public static WebElement txbx_aUsername (WebDriver driver) {

            element = driver.findElement(By.id("username"));

            return element;
        }

public static WebElement txbx_aPassword (WebDriver driver) {

            element = driver.findElement(By.id("password"));

            return element;
        }

}

1 个答案:

答案 0 :(得分:1)

@BeforeMethod 中初始化的驱动程序对象在您的测试方法中不可见。尝试以下操作:

public class NewTest {

    WebDriver driver = null;
    @BeforeMethod
    public void beforeMethod() {
        System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);
        //Pay close attention this below line. Notice how we are not creating a new WebDriver variable
        //but we are just initialising the class level data member here
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://" + Constants.auth_Username + ":" + Constants.auth_Password + "@" + Constants.URL);
    }

    @Test
    public void f() throws InterruptedException {
        Admin_Login.txbx_aUsername(driver).sendKeys(Constants.aUsername);
        Admin_Login.txbx_aPassword(driver).sendKeys(Constants.aPassword);
        Admin_Login.btn_login(driver).click();
        actions.create(driver).click();
        Actions action = new Actions(driver);
        action.moveToElement(actions.create.list(driver)).build().perform();

        //Create a Course Category
        actions.create_list.list_category(driver).click();
        actions.create_list.list_ceate_category(driver).click();
        actions.create_list.txtbx_Cat_tile(driver).sendKeys(Constants.list_title);
        actions.create_list.btn_Cat_Save(driver).click();
        System.out.println("List Created Successfully");
    }
}

在使用以下代码输入任何数据之前,请尝试等待文本框出现:

public static WebElement txbx_aUsername (WebDriver driver) {
    WebDriverWait w = new WebDriverWait(driver, 30);
    w.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
    return driver.findElement(By.id("username"));
}

如果您仍然遇到任何问题,请告诉我。