当我试图在POM selenium webdriver中使用TestNG将login转换为单独的方法时,java出现空指针异常
我一直在尝试使用TestNG在SeleniumWebDriver上构建POM框架。为页面元素和页面操作(单击、发送键等)创建单独的类。到目前为止,它运行良好。然后我决定使用commonFunctions类进行登录。在该实现之后,它将不工作,并抛出空指针异常。我不太擅长OOPS概念。问题是我什么时候打电话
commonFunction.login(username, password);
有人能帮我找出问题的原因吗
逻辑测试类
package Tests;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;
import lib.excelDataConfig;
import lib.pageActions;
import pages.HomepageObjects;
import pages.LoginObjects;
import functions.commonFunctions;
public class LoginTest{
public String baseUrl = "https://sampleurl.com/";
String driverPath = "C:\\selenium\\geckodriver-v0.27.0-win64\\geckodriver.exe";
public WebDriver driver ;
public pageActions actionObj;
public LoginObjects loginObj;
public HomepageObjects homepageObj;
public commonFunctions commonFunction;
excelDataConfig excel = new excelDataConfig("C:\\Users\\vsaisiva\\eclipse-workspace\\TestNgProject\\datasource\\UserData.xlsx");
@BeforeTest
public void LaunchTest() {
System.out.println("launching firefox browser");
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
loginObj = new LoginObjects(driver);
homepageObj = new HomepageObjects(driver);
actionObj = new pageActions(driver);
commonFunction = new commonFunctions(driver);
driver.get(baseUrl);
}
@Test (priority=1)
public void verifyHomepageTitle() {
String expectedTitle = "RM Assessment Hub";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
@Test (priority=2)
public void LoginAndVerify() {
//driver.findElement(By.id("usernameBox")).sendKeys("bobsmith@email.com");
String username = "user@email.com";
String password = "user";
commonFunction.login(username, password);
actionObj.waitForVisibility(homepageObj.loginConfirmText);
if(actionObj.isElementPresent(homepageObj.loginConfirmText)) {
System.out.println("Login success for user:"+username);
homepageObj.clickUsermenu();
homepageObj.clickLogout();
}
}
@AfterTest
public void CloseTest() {
driver.close();
}
}
登录对象类:
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
public class LoginObjects {
WebDriver driver = null;
public By usernameBox = By.id("usernameBox");
public By passwordBox = By.id("passwordBox");
public By loginbutton = By.id("loginbutton");
public LoginObjects(WebDriver driver) {
this.driver=driver;
}
}
页面操作类
package lib;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
public class pageActions {
WebDriver driver = null;
public pageActions(WebDriver driver){
this.driver = driver;
}
public void click(By locator){
driver.findElement(locator).click();
}
public void type(By locator, String text){
driver.findElement(locator).clear();
driver.findElement(locator).sendKeys(text);
}
public void waitForVisibility(By locator) throws Error{
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
public boolean isElementPresent(By locator) throws Error{
return driver.findElement(locator).isDisplayed();
}
}
通用函数类:
package functions;
import org.openqa.selenium.WebDriver;
import pages.LoginObjects;
import lib.pageActions;
public class commonFunctions {
WebDriver driver = null;
public commonFunctions(WebDriver driver) {
this.driver=driver;
}
public pageActions actionObj = new pageActions(driver);
public LoginObjects loginObj = new LoginObjects(driver);
public void login(String username, String password){
actionObj.type(loginObj.usernameBox, username);
actionObj.type(loginObj.passwordBox, password);
actionObj.click(loginObj.loginbutton);
}
}
共 (0) 个答案