Cucumber语言 - WebDriverWait 空指针异常 - Java



我正在尝试将Cucumber框架集成到现有的自动化框架中。我无法理解的是,每次调用方法WaitForElement,它给了我一个空指针异常。也许有人可以解释我哪里做错了。

以下是我的测试步骤类

package Steps;
import TestFramework.*;
import io.cucumber.java.en.*;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class MyStepdefs extends BaseStep{
private WebDriver driver = null;
private Hooks lHooks;
private SceanarioContext sceanarioContext;
private WebDriverWait wait;

public MyStepdefs(Hooks lHooks, Hooks lwait, SceanarioContext sceanarioContext) {
this.driver = lHooks.driver;
this.wait=new WebDriverWait(driver,10);
this.sceanarioContext=sceanarioContext;
}
@Given("The user login to the application")
public void the_user_login_to_the_application() {
LoginPage loginObject = new LoginPage(driver,wait);
resultValue = loginObject.VerifyUrl();
resultMessage = "URL is verified";
Assert.assertTrue(resultMessage,resultValue);
}    
}

以下是我的BaseStep类,它由Mysteps扩展。

package Steps;
import Pages.BasePage;
import io.cucumber.datatable.DataTable;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import java.util.List;
import java.util.Map;
public class BaseStep{
public WebDriver driver = null;
public long explicitWaitValue = 10;
public WaitHelper wait;
public Boolean resultValue;
public final static Logger logger = Logger.getLogger(BasePage.class);
public String resultMessage="";
public Object scenarioContextObject;
public String GetTableValues(String headerValue, DataTable dataTable) {
List<Map<String, String>> data = dataTable.asMaps(String.class, String.class);
String value = data.get(0).get(headerValue);
return value;
}
}

以下是我的 Hooks 类,它执行浏览器和驱动程序实例化。我已经在 src/Pages 包中的此类中声明了页面,以共享 Webdriver 和 WebdriverWait 类的实例化。我无法在 BaseStep 类中扩展 Hooks 类,因为它不允许我这样做。

package Steps;
import Pages.Page;
import TestFramework.ConfigFileReader;
import TestFramework.ManageWebDriver;
import io.cucumber.core.api.Scenario;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.xhtmlrenderer.css.parser.property.PrimitivePropertyBuilders;
public class Hooks {
protected static WebDriver driver = null;
public static WebDriverWait wait =null;
protected static Page page;
@Before
public void setUp() throws Exception {
ConfigFileReader configFileReader = new ConfigFileReader();
String browserName = configFileReader.getDefaultBrowser();
ManageWebDriver manage = new ManageWebDriver();
this.driver = manage.GetBrowser(browserName);
driver.get(configFileReader.getApplicationUrl());
wait = new WebDriverWait(driver,10);
page = new Page(driver,wait);
}
@After
public void tearDown(Scenario scenario) {
if (scenario.isFailed()) {
final byte[] screenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png"); //stick it in the report
}
driver.close();
}
}

以下是我的 LoginPage 类,在我的执行功能的步骤中调用

package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPage extends BasePage{

WebDriver driver;
WebDriverWait wait;
Boolean url = false;

public LoginPage(WebDriver driver,WebDriverWait wait) {
super(driver,wait);
this.driver = driver;
this.wait=wait;
PageFactory.initElements(driver, wait);
}
@FindBy(how = How.ID, using = "username")
private WebElement loginEmail;
public boolean VerifyUrl(){
WaitUntilElement(loginEmail);
if(driver.getCurrentUrl().contains("auth"))
return url=true;
return false;
}
}

以下是我的 BasePage 类,它由 LoginPage 扩展。这就是问题发生的地方。调试时,我确实得到了一个等待值。但是当调用 WaitUntilElement 时,它会给出一个空指针异常

package Pages;
import org.apache.log4j.Logger;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
public class BasePage extends Page {
public WebDriver driver = null;
public long explicitWaitValue = 10;
public Boolean resultValue;
public final static Logger logger = Logger.getLogger(BasePage.class);
public String resultMessage="";
public Object scenarioContextObject;
public BasePage(WebDriver driver,WebDriverWait wait) {
super(driver,wait);
this.driver = driver;
this.wait=wait;
}
public void WaitUntilElement(WebElement element){
wait.until(ExpectedConditions.visibilityOf(element));
}
}

下面是 Page 类,它获取驱动程序的值,并在 Hooks 类中实例化时等待。这反过来又由BasePage扩展

package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Page {
public WebDriver driver;
public WebDriverWait wait;
public Page(WebDriver driver, WebDriverWait wait){
this.driver = driver;
this.wait = new WebDriverWait(driver,10);
}
}

我也尝试在BasePage和Page中实例化WebDriver。但不知何故,它总是给我一个空指针异常。

我是用 Java 实现 Cucumber 的新手,因此也许我错过了一些微小的东西,或者可能是我无法找到的主要内容。知道我在哪里犯了错误就太好了。

我遇到了同样的问题。我的驱动程序在传递给网络驱动程序等待的参数中为空。我用 if 语句来检查驱动程序是否为空并且是。因此启动了设置驱动程序的页面类,并且它是固定的。如果仍然卡住,请给我发消息

最新更新