NullPointer Exception while 运行 在 TestNG 和 Selenium 中进行测试
NullPointer Exception while running a test in TestNG and Selenium
我有 4 个方法可以调用我的浏览器、登录页面并执行一些操作。当我 运行 代码作为 Java 应用程序时,浏览器打开并执行操作,但是当我 运行 作为 TestNG 测试时,控制台显示所有 4 个测试都通过了,但是它还显示 Null Pointer Exception
。此外,当 运行ning 作为 testng 测试时,它不会调用我的浏览器并执行操作。
代码:
package pulse.tpr;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.*;
public class LoginInterface {
public WebDriver driver;
public Actions actions;
public void launchApplication() {
System.setProperty("webdriver.chrome.driver",
"C:\MyChromeDriver\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.get("myURL");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
actions = new Actions(driver);
}
public void closeApplication() {
driver.close();
}
@Test(priority = 0)
public void adminLogin() {
try {
actions.moveToElement(driver.findElement(By.name("userId")));
actions.click();
actions.sendKeys("sysadmin");
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(driver.findElement(By.name("password")));
actions.click();
actions.sendKeys("MMNext13#");
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(driver.findElement(By.name("dcNumber")));
actions.click();
actions.sendKeys("7036");
actions.build().perform();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(priority = 1)
public void tprPage() {
try {
actions.moveToElement(driver.findElement(By
.cssSelector("body > ion-app > ng-component > ion-nav > page-login > ion-content > div.scroll-content > ion-card > ion-grid > form > ion-list > div.login-button > button > span")));
actions.click();
actions.build().perform();
actions.moveToElement(driver.findElement(By.id("tab-t0-2")));
actions.click();
actions.build().perform();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(priority = 2)
public void createMove() {
try {
actions.moveToElement(driver.findElement(By
.className("send-associates-button")));
actions.click();
actions.build().perform();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
actions.moveToElement(driver.findElement(By
.cssSelector("#tabpanel-t0-2 > send-associate-page > ion-content > div.scroll-content > div > ion-list > ion-item:nth-child(4)")));
actions.click();
actions.build().perform();
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='footers']/ion-toolbar/div[2]/ion-row/ion-col[2]/button")));
actions.click();
actions.build().perform();
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]")));
actions.click();
actions.build().perform();
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]/ion-row[2]")));
actions.click();
actions.build().perform();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Select to area
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]")));
actions.click();
actions.build().perform();
// Select to area
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]/ion-row[2]")));
actions.click();
actions.build().perform();
// Next button in to area page
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
actions.click();
actions.build().perform();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(priority = 3)
public void rightNowToClockout() {
try {
// Right Now Time
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[4]")));
actions.click();
actions.build().perform();
// Next Button
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
actions.click();
actions.build().perform();
// Clockout time
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[2]/ion-col")));
actions.click();
actions.build().perform();
// Next button
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
actions.click();
actions.build().perform();
// Send Button
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/ion-row/ion-row")));
actions.click();
actions.build().perform();
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]")));
String element = driver
.findElement(
By.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div[1]/ion-card/ion-row[1]/ion-col/ion-row[2]/div[1]/label[1]"))
.getText();
Assert.assertTrue(element.contains("1:35"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
LoginInterface obj = new LoginInterface();
obj.launchApplication();
obj.adminLogin();
obj.tprPage();
obj.createMove();
obj.rightNowToClockout();
}
}
下面是 testNG 错误:
java.lang.NullPointerException
at pulse.tpr.LoginInterface.adminLogin(LoginInterface.java:38)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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)
java.lang.NullPointerException
at pulse.tpr.LoginInterface.tprPage(LoginInterface.java:67)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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)
而不是 运行在 main
方法中使用脚本,您应该 运行 将脚本作为 "TestNG Test" 并在 [=33] 之前进行初始设置=]宁您可以使用的测试,BeforeSuite
和 AfterSuite
。
因此,您需要在脚本中进行两处更改:
在方法 launchApplication()
之前插入一个标记 @BeforeSuite
并在方法 closeApplication()
之前插入一个标记 @AfterSuite
以执行设置和在执行脚本之前和之后关闭驱动程序。
删除您编写的整个主要方法,然后 运行 脚本作为 "TestNG Test",您的测试应该 运行 符合预期。
我有 4 个方法可以调用我的浏览器、登录页面并执行一些操作。当我 运行 代码作为 Java 应用程序时,浏览器打开并执行操作,但是当我 运行 作为 TestNG 测试时,控制台显示所有 4 个测试都通过了,但是它还显示 Null Pointer Exception
。此外,当 运行ning 作为 testng 测试时,它不会调用我的浏览器并执行操作。
代码:
package pulse.tpr;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.*;
public class LoginInterface {
public WebDriver driver;
public Actions actions;
public void launchApplication() {
System.setProperty("webdriver.chrome.driver",
"C:\MyChromeDriver\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.get("myURL");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
actions = new Actions(driver);
}
public void closeApplication() {
driver.close();
}
@Test(priority = 0)
public void adminLogin() {
try {
actions.moveToElement(driver.findElement(By.name("userId")));
actions.click();
actions.sendKeys("sysadmin");
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(driver.findElement(By.name("password")));
actions.click();
actions.sendKeys("MMNext13#");
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(driver.findElement(By.name("dcNumber")));
actions.click();
actions.sendKeys("7036");
actions.build().perform();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(priority = 1)
public void tprPage() {
try {
actions.moveToElement(driver.findElement(By
.cssSelector("body > ion-app > ng-component > ion-nav > page-login > ion-content > div.scroll-content > ion-card > ion-grid > form > ion-list > div.login-button > button > span")));
actions.click();
actions.build().perform();
actions.moveToElement(driver.findElement(By.id("tab-t0-2")));
actions.click();
actions.build().perform();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(priority = 2)
public void createMove() {
try {
actions.moveToElement(driver.findElement(By
.className("send-associates-button")));
actions.click();
actions.build().perform();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
actions.moveToElement(driver.findElement(By
.cssSelector("#tabpanel-t0-2 > send-associate-page > ion-content > div.scroll-content > div > ion-list > ion-item:nth-child(4)")));
actions.click();
actions.build().perform();
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='footers']/ion-toolbar/div[2]/ion-row/ion-col[2]/button")));
actions.click();
actions.build().perform();
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]")));
actions.click();
actions.build().perform();
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]/ion-row[2]")));
actions.click();
actions.build().perform();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Select to area
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]")));
actions.click();
actions.build().perform();
// Select to area
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]/ion-row[2]")));
actions.click();
actions.build().perform();
// Next button in to area page
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
actions.click();
actions.build().perform();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(priority = 3)
public void rightNowToClockout() {
try {
// Right Now Time
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[4]")));
actions.click();
actions.build().perform();
// Next Button
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
actions.click();
actions.build().perform();
// Clockout time
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[2]/ion-col")));
actions.click();
actions.build().perform();
// Next button
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
actions.click();
actions.build().perform();
// Send Button
actions.moveToElement(driver.findElement(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/ion-row/ion-row")));
actions.click();
actions.build().perform();
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]")));
String element = driver
.findElement(
By.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div[1]/ion-card/ion-row[1]/ion-col/ion-row[2]/div[1]/label[1]"))
.getText();
Assert.assertTrue(element.contains("1:35"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
LoginInterface obj = new LoginInterface();
obj.launchApplication();
obj.adminLogin();
obj.tprPage();
obj.createMove();
obj.rightNowToClockout();
}
}
下面是 testNG 错误:
java.lang.NullPointerException
at pulse.tpr.LoginInterface.adminLogin(LoginInterface.java:38)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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)
java.lang.NullPointerException
at pulse.tpr.LoginInterface.tprPage(LoginInterface.java:67)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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)
而不是 运行在 main
方法中使用脚本,您应该 运行 将脚本作为 "TestNG Test" 并在 [=33] 之前进行初始设置=]宁您可以使用的测试,BeforeSuite
和 AfterSuite
。
因此,您需要在脚本中进行两处更改:
在方法
launchApplication()
之前插入一个标记@BeforeSuite
并在方法closeApplication()
之前插入一个标记@AfterSuite
以执行设置和在执行脚本之前和之后关闭驱动程序。删除您编写的整个主要方法,然后 运行 脚本作为 "TestNG Test",您的测试应该 运行 符合预期。