如何解决当我尝试将登录转换为 POM 中的单独方法时出现空指针异常 - selenium webdriver with TestNG
我一直在尝试使用 TestNG 在 Selenium webdriver 上构建 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;
}
}
pageActions 类
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();
}
}
commonFunctions 类:
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);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。