我有hooks.java作为驱动程序类,loginpage.java作为所有登录页对象和步骤定义类。我的页面工厂对象没有在我的登录类中初始化,由于驱动程序为null,它返回null。具有@before标记的驱动程序类及其initiate位于所有类之前,但驱动程序为null。当我使用pagefactory.initelements(driver,loginpage.class)时;在hook类中,该时间页工厂初始化并且程序成功运行。我在loginpage构造函数中使用它,只是想知道我做错了什么。为什么页面工厂返回null?如何使用页面工厂。
以下是错误java.lang.nullpointerexception:无法调用“org.openqa.selenium.webelement.sendkeys(java.lang.charsequence[]),因为“steps.loginpage.txtusernamebox”为空
请帮忙。谢谢
Hooks.java
package steps;
import Util.GetConfigProp;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java8.En;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class Hooks extends BaseUtil implements En {
BaseUtil base;
// GetConfigProp prop;
static GetConfigProp prop;
static {
try {
prop = new GetConfigProp();
} catch (IOException e) {
e.printStackTrace();
}
}
public Hooks() {
//Default constructor
Before(() -> {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.WINDOWS);
cap.setCapability("marionette", true);
ChromeOptions COptions = new ChromeOptions();
COptions.merge(cap);
String hubUrl = null; //get hub url from properties file
hubUrl = prop.getHubUrl();
base.driver = new RemoteWebDriver(new URL(hubUrl), COptions);
base.driver.navigate().to(prop.getUrl()); //get url from properties file
base.driver.manage().window().maximize();
base.driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// PageFactory.initElements(driver, LoginPage.class);
});
}
public Hooks(BaseUtil base) {
this.base = base;
}
public WebDriver getDriver() {
return base.driver;
}
@After
public void tearDown() {
base.driver.quit();
}
}
LoginPage.java
package steps;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class LoginPage extends BaseUtil {
BaseUtil base;
@FindBy(how = How.ID, using = "user-name")
public static WebElement txtUserNameBox;
@FindBy(how = How.ID, using = "password")
public static WebElement txtPassword;
@FindBy(how = How.ID, using = "login-button")
public static WebElement btnLogin;
public LoginPage() {
//Default constructor
}
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void login() {
System.out.println("this is title" + base.driver.getTitle());
System.out.println("this is getwindow" + base.driver.getWindowHandle());
txtUserNameBox.sendKeys("standard_user");
txtPassword.sendKeys("secret_sauce");
btnLogin.click();
}
public void loginPassword() {
//to be implement
}
}
MyStepdefs8.java
package steps;
import io.cucumber.java8.En;
public class MyStepdefs8 extends LoginPage implements En {
BaseUtil base;
LoginPage login = new LoginPage();
public MyStepdefs8(BaseUtil base) {
this.base = base;
}
public MyStepdefs8() {
Given("the user is on landing page", () -> {
login.login();
});
When("^the user clicks button$", () -> {
System.out.println("this is second step");
loginPassword();
});
}
}
2条答案
按热度按时间1hdlvixo1#
在mystepdefs8类中创建登录页对象时传递驱动程序示例。
否则,它将初始化登录页的默认构造函数。
0lvr5msh2#
在类mystepdefs8中,创建loginpage对象时,初始化loginpage类的默认构造函数。因此,当您调用webelements时,它返回null,因为它们还没有初始化。