提问人:Kyle 提问时间:10/31/2023 更新时间:10/31/2023 访问量:24
注入 fluent wait Cucumber 始终以 null 形式返回
Injecting fluent wait Cucumber always returning as null
问:
我有一个 cucumber 项目,它使用 pico-container 注入 Webdriver,我想用我流畅的等待做同样的事情。我正在使用POM设计。我总是收到错误
CucumberRunner.runScenario » NullPointer Cannot invoke "org.openqa.selenium.support.ui.Wait.until(java.util.function.Function)" because "this.wait" is null
我不明白为什么它会返回它,因为我相信我正在初始化它。
在我的 Hooks 课程中,我正在做:
package stepdefinitions;
import org.openqa.selenium.WebDriver;
import base.BaseClass;
import io.cucumber.java.After;
import io.cucumber.java.Before;
public class Hooks {
WebDriver driver;
BaseClass baseclass;
public Hooks(final BaseClass baseclass) {
this.baseclass = baseclass;
}
@Before
public void bf() {
baseclass.setDriver();
baseclass.setWait(baseclass.getDriver());
baseclass.navigateToURL();
}
@After
public void af() {
baseclass.getDriver().quit();
}
}
然后在我的 BaseClass 中,我有:
package base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.time.Duration;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.NoSuchElementException;
import utils.TestUtils;
public class BaseClass {
private WebDriver driver;
protected static Properties prop;
private Wait<WebDriver> wait;
public BaseClass() {
try {
prop = new Properties();
FileInputStream inputFile = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/config/config.properties");
prop.load(inputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("file not found");
} catch (IOException e) {
System.out.println("io exception");
}
}
public WebDriver getDriver() {
return driver;
}
public void setDriver() {
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("--window-size=1920,1080");
driver = new ChromeDriver(options);
}
public void navigateToURL() {
driver = getDriver();
driver.get(prop.getProperty("url"));
}
public Wait<WebDriver> getWait() {
return wait;
}
public void setWait(final WebDriver driver) {
wait = createFluentWait(driver);
}
// Helper method to create the FluentWait
private Wait<WebDriver> createFluentWait(final WebDriver driver) {
return new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(TestUtils.ELEMENT_TIMEOUT))
.pollingEvery(Duration.ofMillis(TestUtils.POLLING_INTERVAL))
.ignoring(NoSuchElementException.class, ElementNotInteractableException.class);
}
}
我正在我的 HomePage 对象中使用它:
package pageobjects;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.*;
import org.openqa.selenium.support.ui.Wait;
import base.BaseClass;
public class HomePage extends BaseClass {
SharedPageObjects sharedPageObjects;
WebDriver driver;
Wait<WebDriver> wait;
// Account Nav Bar Elements
@FindBy(css = "[class=\"nav__top-inner\"]")
WebElement accountNavBar;
@FindBy(css = "[class=\"nav__top-type\"] > a:nth-child(1)")
WebElement personalHeader;
@FindBy(css = "[class=\"nav__top-type\"] > a:nth-child(2)")
WebElement businessHeader;
@FindBy(css = "[data-action=\"navigation--primary#signin\"]")
WebElement loginButton;
@FindBy(css = "[class=\"nav__top-account-item\"]:nth-child(2)")
WebElement signupButton;
@FindBy(css = "[class=\"nav__user-name\"]")
WebElement navUserName;
@FindBy(css = "[data-test-id=\"user\"]")
WebElement navUserIcon;
// Main Nav Bar Elements
@FindBy(css = "[class=\"nav__main\"]")
WebElement mainNavBar;
@FindBy(css = "[data-test-id=\"evri_logo_made_for_you\"]")
WebElement logo;
@FindBy(css = "div:nth-child(2) > div > a")
WebElement sendButton;
@FindBy(css = "div:nth-child(3) > div > a")
WebElement trackButton;
@FindBy(css = "div:nth-child(4) > div > a")
WebElement returnButton;
@FindBy(css = "div:nth-child(5) > div > a")
WebElement ourServicesButton;
@FindBy(css = "div:nth-child(6) > div > a")
WebElement parcelShopButton;
@FindBy(css = "div:nth-child(7) > div > a")
WebElement helpButton;
@FindBy(css = "[data-test-id='basket']")
WebElement basketIcon;
@FindBy(css = "button#onetrust-accept-btn-handler")
WebElement cookieAcceptButton;
// Hero Elements
@FindBy(css = "img.the-hero-banner__title-image")
WebElement heroBanner;
@FindBy(css = "[class=\"the-hero-banner__heading\"]")
WebElement heroBannerHeading;
@FindBy(css = "[class=\"tabs-card\"]")
WebElement heroCard;
// Constructor
public HomePage(final WebDriver driver) {
this.driver = driver;
sharedPageObjects = new SharedPageObjects(driver, getWait());
PageFactory.initElements(driver, this);
}
public HomePage verifyHomePageElements(final Boolean loggedIn) {
verifyAccountNav(loggedIn);
verifyMainNav();
verifyHero();
return this;
}
public HomePage verifyAccountNav(final Boolean loggedIn) {
List<WebElement> elementsToCheck = new ArrayList<>();
elementsToCheck.addAll(List.of(accountNavBar, personalHeader, businessHeader));
if (!loggedIn) {
elementsToCheck.addAll(List.of(loginButton, signupButton));
} else {
elementsToCheck.addAll(List.of(navUserName, navUserIcon));
}
// Assert each element in list is visable by calling sharedFunction
sharedPageObjects.areAllElementsVisible(elementsToCheck);
return this;
}
}
在我的主页中,如果我要在构造函数中初始化 fluent wait,它可以正常工作。它似乎无法从之前的钩子初始化。
任何解释都非常感谢!
答: 暂无答案
评论