使用TestNG运行代码时出现空指针异常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用TestNG运行代码时出现空指针异常相关的知识,希望对你有一定的参考价值。
我试图在TestNG中自动化登录和注销场景,并将浏览器作为参数从XML传递,基于该参数将创建chromedriver()实例并运行代码。我有两个类TestRunner和Login,其中login方法获取数据(用户名和密码)TestRunner和数据是从excel中获取的。异常e正在获取null值,因为我在调试时发现了。有什么办法解决吗?
FAILED: Registration_data("ashwin@gmail.com", "ashwin123")
java.lang.NullPointerException
at com.DataDriven.Login.login(Login.java:68)
at com.DataDriven.TestRunner.Registration_data(TestRunner.java:28)
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:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
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:12)
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.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
package com.DataDriven;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestRunner {
@Test(dataProvider = "Authentication")
public static void Registration_data(String sUserName, String sPassword)
throws Exception {
Login lp = new Login();
lp.login(sUserName, sPassword);
}
@DataProvider
public Object[][] Authentication() throws Exception {
Object[][] testObjArray = ReadData.getTableArray(
"F:\Automation\DataDrivenPractice\DataFiles\DataFile.xlsx",
"Sheet1");
return (testObjArray); } }
package com.DataDriven;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Login {
WebDriver driver = null;
@Parameters("browser")
@BeforeClass
// Passing Browser parameter from TestNG xml
public void beforeTest(String browser) {
// If the browser is Firefox, then do this
if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is IE, then do this
} else if (browser.equalsIgnoreCase("chrome")) {
// Here I am setting up the path for my IEDriver
driver = new ChromeDriver(); } }
public void login(String username, String password) {
try {
driver.get("https://www.facebook.com/");
driver.findElement(By.id("email")).sendKeys(username);
driver.findElement(By.id("pass")).sendKeys(password);
driver.findElement(By.id("loginbutton")).click();
driver.findElement(By.linkText("Log Out")).click();
}
catch (Exception e) {
File srcfile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcfile, new File(
"F:\Automation\Screenshots\Login.jpg"));
} catch (Exception e1) {
e1.printStackTrace();
} } }}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="FirefoxTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.DataDriven.TestRunner" />
<class name="com.DataDriven.Login" />
</classes>
</test>
</suite>
你的问题是 - 你的方法beforeTest
没有调用。你的driver
没有指向任何类型的物体。
TestNG仅针对@Test
注释搜索所有类。怎么知道@BeforeClass
注释在于你创建的特定类?
要运行@BeforeClass
注释,您必须为extends
类设置Login
(它对多个脚本无效)或手动运行beforeTest
方法。或者将其设置为包含@Test
注释的运行类。
如我错了请纠正我。希望能帮助到你!
编辑:
将方法和WebDriver变量移动到Test Runner
类
WebDriver driver = null;
@Parameters("browser")
@BeforeClass
// Passing Browser parameter from TestNG xml
public void beforeTest(String browser) {
// If the browser is Firefox, then do this
if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is IE, then do this
} else if (browser.equalsIgnoreCase("chrome")) {
// Here I am setting up the path for my IEDriver
driver = new ChromeDriver(); } }
TestRunner
类应包含@Test
和@BeforeClass
。因为TestNG从包含@Test
注释的类中读取注释。
您的脚本中存在许多问题
1 - 您的类单独初始化。来自@BeforeClass的驱动程序不会继承到@Test
2 - 当你这样做
Login lp = new Login();
你有
WebDriver driver = null;
@BeforeClass is not invoked, no driver init, your driver is still null
然后
lp.login(sUserName, sPassword);
抛出NullPointErexception,因为以下代码行中的驱动程序为null
driver.get("https://www.facebook.com/");
建议行动
- 你需要让司机通过你所有的课程
TestBase.class
Put your @BeforeClass here
LoginPage.class,请确保允许将驱动程序插入其中
public LoginPage(WebDriver driver) {
// your code here
}
// Put login(sUserName, sPassword) method here
TestRunner.class
inherite/extends from TestBase.class
LoginPage lp = new LoginPage(driver);
lp.login(sUserName, sPassword);
以上是关于使用TestNG运行代码时出现空指针异常的主要内容,如果未能解决你的问题,请参考以下文章