我有一个关于WebDriverWait数据类型的问题,我假设这种数据类型只需要一个实例,这意味着我可能会创建一个包装器,并通过使用单例模式方法只允许创建一个实例。
目前我使用的是thread.sleep,基本上所有需要调用该函数的地方,我都是从类扩展而来的,这可能不是最好的方法,当然,我应该使用WebDriverWait而不是线程。有人可以建议的方法->到目前为止,我创建了网页对象的网页元素和独立的服务本身的逻辑,所以现在我也需要在每个服务的WebDriverWait,因为这是一个必要的操作。
客户打开页面后弹出的Cookie窗口:
/**
* acceptCookies -> clickable
* cookieBanner -> just to identify that cookie component showed up.
* PageFactory -> will initialize every WebElement variable with a reference to a corresponding element on the actual web page.
*/
public class CookieModal {
WebDriver driver;
@FindBy(css = ".cookie-accept-all")
public WebElement acceptCookies;
public CookieModal(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}然后我分离了服务(操作)
public class CookieService {
private final CookieModal cookieModal;
public CookieService(WebDriver driver) {
this.cookieModal = new CookieModal(driver);
}
public void acceptCookies() {
cookieModal.acceptCookies.click();
}
}这必须更改为WebDriverWait,但我也认为在每个页面对象从AbstractPage扩展是没有必要的,所以我想听到一些有经验的人谁可以评估我的结构和初始化WebDriverWait的建议
public class AbstractPage {
// this is not good as thread sleep is not dynamic and you have to specify time yourself
// change to webdriver wait
private AbstractPage microsleep(Integer milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (Throwable e) {
String error = String.format("Unable to put thread to sleep (requested %d milliseconds).", milliseconds);
throw new RuntimeException(error, e);
}
return this;
}
public AbstractPage emulateWaitingUser() {
return microsleep(800);
}
public AbstractPage sleep(Integer seconds) {
return microsleep(1000 * seconds);
}提前谢谢你。
发布于 2020-10-16 14:39:34
WebDriverWait通常与Expected类一起使用。在这种情况下,您不能只等待800ms,您需要等待,直到满足条件。例如,等待页面标题显示,或者等待加载器图标不可见,等待登录按钮可点击,等等。
这意味着如果您希望在抽象方法中实例化一个等待,您将需要添加一个未知(和非抽象)等待条件。您可以只实例化一个通用的等待对象,然后在已知的情况下添加一个条件,但这似乎有点不完整。
我想到的另一个想法是将WebDriverWait声明为Cookie Service类中的一个字段,并将其传递给它的方法。
发布于 2020-10-16 18:06:33
Selenium支持将显式等待集成到页面对象中。这是通过使用初始化页面的特殊方式来实现的。在您的示例中,您正在执行以下操作:
PageFactory.initElements(driver, this);这涉及到一些基本的默认方式。然而,你可以在这里增加更多的复杂性,同时获得更有效的架构。
您可以扩展AjaxElementLocator类,在该类中您将以涉及任何类型的条件和等待的方式覆盖isElementUsable方法。然后,您将通过专用的LocatorFactory使用该定位器初始化您的页面。有关如何使用您可以找到here所有类的示例。
https://stackoverflow.com/questions/64383746
复制相似问题