所以,当出现问题时,我只是创建了一个简单的 selenium/JBehave 代码。
我将首先发布简化的代码,然后稍后解释我的问题是什么。
所以这里我们有一个简单的 AbstractClass,它将在我的 PageObjects 上继承。此类仅包含等待加载页面上某些元素的方法。您可以看到我如何在 PageObject 类中使用它(在那里添加了注释)。
AbstractPage.java
public abstract class AbstractPage {
public void waitPageLoad() {
WebDriverWait wait = new WebDriverWait(webDriverProvider.get(), 30);
wait.until(ExpectedConditions.visibilityOfAllElements(elementsToWait()));
}
protected List<WebElement> elementsToWait() {
return null;
}
}
PageObject.java
public class PageObject extends AbstractPage{
@FindBy(id = "webElement1")
private WebElement webElement1;
@FindBy(id = "webElement2")
private WebElement webElement2;
public void clickWebElement1() {
webElement1.click();
}
public void sendKeysWebElement2(String strKeys) {
webElement2.sendKeys(strKeys);
}
//Note how im using the elementsToWait here
@Override
protected List<WebElement> elementsToWait() {
return Arrays.asList(webElement1, webElement2);
}
}
现在在我的步骤中,如果我想先等待页面加载,然后执行我想要的操作,我需要在其中一个步骤中从我的抽象类调用“waitPageLoad()”方法(或者肯定是所有这些)。
PageObjectSteps.java
@Component
public class PageObjectSteps {
private PageObject pageObject;
@When("User wants to click on webElement1")
public void accountToDeposit () {
pageObject.waitPageLoad(); //Calling here just as an example
pageObject.clickWebElement1();
}
@When("User wants to type on webElement2 '$strType'")
public void amountToDeposit(@Named("strType") String strType) {
pageObject.sendKeysWebElement2(strType);
}
}
现在我的问题是:
有没有一种方法可以让我在每次使用 pageObject 时调用 waitPageLoad(),但不调用步骤上的方法?
例如,我会为每个 pageObject 使用不同的 waitPageLoad(),具体取决于我需要等待的内容。 在此示例中,我将等待 webElement1 和 webElement2 可见。
selenium 是否有类似:@AlwaysWait 的东西,我可以在方法之前使用它,并且每次使用页面对象时都会调用它(同样,不在步骤中调用它)?或者一种表示法可以使每次使用页面对象时调用一个方法?
示例:
@AlwaysWait
public void waitPageObjectLoad() {
WebDriverWait wait = new WebDriverWait(webDriverProvider.get(), 30);
wait.until(ExpectedConditions.visibilityOfAllElements(webElement1, webElement2));
}
希望我的说法能够被理解, 提前致谢。
PS:四处询问,我知道您可以使用java反射框架来做到这一点,但我想知道您是否可以仅使用selenium来做到这一点。
最佳答案
这是您学习热爱多态性和代理模式的地方。
创建一个新的具体类,实现名为 LazyWebDriver
的 WebDriver
接口(interface)。创建另外两个类来延迟加载 Web 元素:LazyWebElement 和 LazyWebElementList。
LazyWebDriver 中的方法应返回 LazyWebElement 或 LazyWebElementList 对象,但这些方法的返回值应为 WebElement 或 List。
现在您只需使用 LazyWebDriver,就好像它是任何其他 Web 驱动程序一样。使用标准 WebDriver 接口(interface)查找元素将始终等待一定的秒数:
WebDriver driver = new ChromeDriver();
int secondsToWait = 15;
WebDriver lazyDriver = new LazyWebDriver(driver, secondsToWait);
// findElement(...) returns immediately
WebElement element = lazyDriver.findElement(By.id("foo"));
// Implicitly waits up to 15 seconds for the element
// to become visible before attempting to click on it
element.click();
// Returns immediately since the "wrapped" element
// has already been fetched after waiting.
String name = element.getAttribute("name");
LazyWebDriver 类
public class LazyWebDriver implements WebDriver {
private WebDriver driver;
public LazyWebDriver(WebDriver driver, int secondsToWait) {
this.driver = driver;
this.wait = new WebDriverWait(driver, secondsToWait);
}
public void close() {
driver.close();
}
public WebElement findElement(By by) {
return new LazyWebElement(driver, by, wait);
}
public List<WebElement> findElements(By by) {
return new LazyWebElementList(driver, by, wait);
}
// ... other methods just call through to driver.foo(...)
}
LazyWebElement 类
public class LazyWebElement implements WebElement {
private final WebDriver driver;
private final WebDriverWait wait;
private final By by;
private WebElement element;
public LazyWebElement(WebDriver driver, By by, WebDriverWait wait) {
this.driver = driver;
this.by = by;
this.wait = wait;
}
private WebElement getElement() {
if (element == null) {
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
element = driver.findElement(by);
}
return element;
}
public void clear() {
getElement().clear();
}
public void click() {
getElement().click();
}
public String getAttribute(String attributeName) {
return getElement().getAttribute(attributeName);
}
// Other methods in WebElement interface must first call getElement()
}
LazyWebElementList 类
public class LazyWebElementList implements List<WebElement> {
private final WebDriver driver;
private final WebDriverWait wait;
private final By by;
private List<WebElement> elements;
public LazyWebElementList(WebDriver driver, By by, WebDriverWait wait) {
this.driver = driver;
this.by = by;
this.wait = wait;
}
private List<WebElement> getElements() {
if (elements == null) {
wait.until(ExpectedConditions.visibilityOfAllElementsLocated(by));
elements = driver.findElements(by);
}
return elements;
}
public boolean add(WebElement element) {
getElements().add(element);
}
public void clear() {
getElements().clear();
}
// Other methods defined in List<E> interface must call getElements() first
}
我在您的代码示例中看到您正在从 webDriverProvider
获取 WebDriver 对象。您可以继续使用它,除非 Web 驱动程序提供程序将 LazyWebDriver 转换返回到 WebDriver 接口(interface)。您的其余代码仍然完全不知道 LazyWebDriver、LazyWebElement 和 LazyWebElementList 的存在。这应该很容易插入到您现有的测试代码中。
关于java - 始终等待页面加载到 PageObjects 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57224220/