使用 TestNG 从 selenium 测试用例中的页面对象调用方法时如何修复 NullPointerException 错误?

How to fix NullPointerException error when calling methods from page objects in selenium testcases using TestNG?

我已经为 selemium 测试用例创建了页面对象模型。我在'LoginPage'class中定义了登录方法,在'CommonMethods'class中定义了一些方法。现在当我 运行 测试时,它显示 NullPointerException 错误。我对 JUnit 做了同样的事情,它在这个设置下运行良好,但不适用于 TestNG。

这是我的页面对象文件:

package pages;

import org.openqa.selenium.WebDriver;

public class PageObject {

    public WebDriver driver;

    public PageObject(WebDriver driver) {
        this.driver=driver;
    }
}

CommonMethods 文件


package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class CommonMethods extends PageObject {

    public CommonMethods(WebDriver driver) {
        super(driver);
    }

    public By ByLocator(String locator) {
        By result=null;
        if(locator.startsWith("/")||locator.startsWith("//"))
            result=By.xpath(locator);
        else if(locator.startsWith(".")||locator.startsWith("#"))
            result=By.cssSelector(locator);
        return result;
    }

    public void waitForElementPresent(By locator, int time) {
        WebDriverWait wait = new WebDriverWait(driver, time);
        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    }
}

登录页面文件


package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage extends PageObject {

    public LoginPage(WebDriver driver) {
        super(driver);
    }

    CommonMethods cm = new CommonMethods(driver);

    By usernameField = cm.ByLocator("#login");
    By passwordField = cm.ByLocator("#password");
    By submitButton = cm.ByLocator("#signin_submit");

    public void loginWithCustomer() {
        driver.get("https://www.stg.keepcollective.com/signin");
        //cm.waitForElementPresent(usernameField, 3000);
        driver.findElement(usernameField).sendKeys("joshimeghakeep@yopmail.com");
        driver.findElement(passwordField).sendKeys("111111");
    }
}

测试文件

package tests;

import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;

import java.io.*;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;

import pages.*;

public class LoginCheck {

    public WebDriver driver;

    CommonMethods cm=new CommonMethods(driver);
    LoginPage lp=new LoginPage(driver);

    @BeforeClass
    public void beforeClass() throws Exception {
        System.setProperty("webdriver.chrome.driver", "/home/himanshu/Downloads/chromedriver_linux64/chromedriver");
        driver= new ChromeDriver();  
    }

    @Test
    public void checkLoginFunctionality() {
      lp.loginWithCustomer();
    }

    @AfterMethod
    public void Screenshot(ITestResult result) throws IOException {
       if(result.getStatus()==ITestResult.FAILURE) {
           TakesScreenshot ss= (TakesScreenshot)driver;
           File screenshotfile = ss.getScreenshotAs(OutputType.FILE);
           FileUtils.copyFile(screenshotfile, new File("./Screenshots/"+result.getName()+".png"));
       }
    }

    @AfterClass
    public void afterClass() {
        driver.close();
    }
}

I am getting this error:

    FAILED: checkLoginFunctionality
    java.lang.NullPointerException
        at pages.LoginPage.loginWithCustomer(LoginPage.java:19)
        at tests.LoginCheck.checkLoginFunctionality(LoginCheck.java:35)
        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: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:115)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

CommonMethods cmLoginPage lp的初始化移到beforeClass()方法中,放在ChromeDriver的创建之后。 @BeforeClass 并不意味着该方法将在LoginCheck class 的初始化之前执行。这意味着这个方法将在执行此 class 中的任何其他方法之前执行。

public class LoginCheck {

    public WebDriver driver;
    private CommonMethods cm;
    private LoginPage lp;

    @BeforeClass
    public void beforeClass() throws Exception {
        System.setProperty("webdriver.chrome.driver", "/home/himanshu/Downloads/chromedriver_linux64/chromedriver");
        driver= new ChromeDriver();
        cm = new CommonMethods(driver);
        lp = new LoginPage(driver);
    }

    @Test
    public void checkLoginFunctionality() {
        lp.loginWithCustomer();
    }

    @AfterMethod
    public void Screenshot(ITestResult result) throws IOException {
        if(result.getStatus()==ITestResult.FAILURE) {
             TakesScreenshot ss= (TakesScreenshot)driver;
             File screenshotfile = ss.getScreenshotAs(OutputType.FILE);
             FileUtils.copyFile(screenshotfile, new File("./Screenshots/"+result.getName()+".png"));
        }
    }

      @AfterClass
      public void afterClass() {
          driver.close();
      }
    }