【发布时间】:2015-03-15 19:20:56
【问题描述】:
我正在尝试在 TestNG 中自动化登录和注销场景,并根据将创建 chromedriver() 实例和运行代码的基础从 XML 传递浏览器作为参数。我有两个类 TestRunner 和 Login,其中登录方法获取数据(用户名和密码)TestRunner 并从 excel 中获取数据。我在调试时发现异常 e 正在获取空值。有什么办法解决吗?
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>
【问题讨论】:
-
我建议使用调试器逐步执行此操作,以确保将
driver设置为您认为的设置,并准确找出哪个值意外为空。跨度> -
@DavidWallace 异常 e 正在获取空值。知道如何解决吗?
-
我已经告诉过你我建议你做什么。
-
@DavidWallace 是的,我进行了调试,它说异常 e 的值为 null。
标签: java selenium selenium-webdriver nullpointerexception testng