繁体   English   中英

我在页面 object class 中收到 null 指针异常,为什么我会收到 NPE

[英]I am getting null pointer exception in page object class why I am getting NPE

Base class
package resources;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.Properties;

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

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;


public class base {

public WebDriver driver;
public Properties property;
public String url= "qwerty"; 


public WebDriver initializeDriver() throws IOException {
    
    
    property = new Properties();
    FileInputStream file = new FileInputStream("D:\\qwe\\rty\\src\\main\\java\\resources\\data.properties");
    
    property.load(file);
    String BrowserName = property.getProperty("browser");
     
    if(BrowserName.equals("chrome")) 
    {
     System.setProperty("webdriver.chrome.driver", "D:\qwe\\rty\\chromedriver.exe");   
        driver = new ChromeDriver();
        
    }
    else if(BrowserName.equals("firefox")) 
    {
        driver = new FirefoxDriver();
    }
    else if(BrowserName.equals("IE")) 
    {
        //Executes IE
    }
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    return driver;
}

public WebDriver verifyPage() {
    
    driver.get(url);
    String Expected = driver.findElement(By.cssSelector("p[class='login-box-msg']")).getText();
    String Actual = "Sign in to start your session";
    Assert.assertEquals(Actual, Expected);
    System.out.println("Homepage is displayed");
    return driver;
}
}

Page Object class

我在这一行的 user() 方法中得到 NPE return driver.findElement(username);

package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;


public class LoginPage {

public WebDriver driver;

By username = By.xpath("//input[@type='text']");
By password = By.xpath("//input[@type='password']");
By login = By.xpath("//button[@type='submit']");
By profile = By.xpath("//li[contains(@class,'nav-item dropdown user-menu')]/a[1]/img");
By logout = By.xpath("//li[@class='user-footer']/a[1]");
 



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


public WebElement user() {

    return driver.findElement(username);
}

public WebElement pass() {

    return driver.findElement(password);
}

public WebElement signIn() {

    return driver.findElement(login);
}

public WebElement pro() {

    return driver.findElement(profile);
}

public WebElement signOut() {

    return driver.findElement(logout);
} 
}

Testcase

当我在页面 object class 中调用 lp.user() 时运行测试用例时出现 NPE 错误

package Admission;

import java.io.IOException;
import java.time.Duration;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import pageObjects.LoginPage;
import resources.base;


public class homePage extends base{


public WebDriver driver;
LoginPage lp = new LoginPage(driver);

@Test
public void loginDetails() throws IOException, InterruptedException
{
    
    driver=initializeDriver();
    verifyPage();
    
    
    WebElement aadharNo = lp.user();
    aadharNo.sendKeys("111111111111");
    
    WebElement password = lp.pass();
    password.sendKeys("21102021");
    
    WebElement submit = lp.signIn();
    submit.click();
    
    
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
    wait.until(ExpectedConditions.visibilityOf(lp.pro())).click();
    
    WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10)); 
    wait1.until(ExpectedConditions.visibilityOf(lp.signOut())).click();
    
}

    @Test
    public void testCase() {
        
    WebElement aadharNo = lp.user();
    WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(30)); 
    wait2.until(ExpectedConditions.visibilityOf(aadharNo));
    aadharNo.sendKeys("111111111111");
    
    WebElement password = lp.pass();
    WebDriverWait wait3 = new WebDriverWait(driver, Duration.ofSeconds(20)); 
    wait3.until(ExpectedConditions.elementToBeSelected(password));
    password.sendKeys("hgfhg");
    lp.signIn();
}
}

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null at pageObjects.LoginPage.user(LoginPage.java:31) at Admission.homePage.testCase(homePage.java:52)

您需要在创建LoginPage object 之前initializeDriver()

public class homePage extends base{
    public WebDriver driver = initializeDriver();
    LoginPage lp = new LoginPage(driver);

    @Test
    public void loginDetails() throws IOException, InterruptedException {
        verifyPage();
        
        WebElement aadharNo = lp.user();
        aadharNo.sendKeys("111111111111");
        
        WebElement password = lp.pass();
        password.sendKeys("21102021");
        
        WebElement submit = lp.signIn();
        submit.click();
        
        
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
        wait.until(ExpectedConditions.visibilityOf(lp.pro())).click();
        
        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10)); 
        wait1.until(ExpectedConditions.visibilityOf(lp.signOut())).click();      
    }

    (...)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM