黄瓜-WebDriverWait空指针异常-Java

时间:2019-09-09 09:38:16

标签: java selenium cucumber pageobjects

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

以下是我的TestStep类

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);
}    
}

以下是MySteps扩展的我的BaseStep类。

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包中的此类中声明了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时,它会给出一个Null Pointer异常

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还是陌生的,因此也许我错过了一些细节,或者也许我找不到一些重要的东西。知道我在哪里犯错真是太好了。

0 个答案:

没有答案
相关问题