使用TestNG运行代码时出现空指针异常

时间:2015-03-15 19:20:56

标签: java selenium selenium-webdriver nullpointerexception testng

我正在尝试自动化TestNG中的登录和注销场景,并将浏览器作为参数从XML传递,基于哪个chromedriver()实例将被创建并且代码将运行。我有两个类TestRunner和Login where 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>

2 个答案:

答案 0 :(得分:0)

您的问题是 - 您的方法beforeTest未调用。您的driver未指向任何类型的对象。

TestNG仅针对@Test注释搜索所有类。如何知道@BeforeClass注释位于您创建的特定类中?

要运行@BeforeClass注释,您必须为extends类设置Login(对多个脚本无效)或手动运行beforeTest方法。或者将其设置为包含@Test注释的运行类。

如果我错了,请纠正我。 希望它有所帮助!

编辑:

将您的方法和WebDriver变量移至Test Runner Class

 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 :(得分:0)

您的脚本中存在许多问题

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);

引发NullPointException,因为以下代码行中的驱动程序为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);