简体   繁体   中英

How can we implement 'wait' functions when using @FindBy?

当我使用页面对象模型时,我使用@FindBy方法,但是如何实现以下语句?

wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Logout")))

You can achieve it using ExpectedConditions.elementToBeClickable as :-

@FindBy(linkText = "Logout")
WebElement logOut;

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(logOut));

您可以设置等待每个页面

PageFactory.initElements(new AjaxElementLocatorFactory(driver, TIMEOUT), this);

u can create a method like this:

private void waitForVisibility(WebElement element) throws Error{
       new WebDriverWait(driver, 30)
            .until(ExpectedConditions.visibilityOf(element));
}

than call the method when you needed:

waitForVisibility(element);//find by element

Seems that most of the solutions so far are kind of incomplete because you cannot make use of the whole static methods provided by the ExpectedConditions class.

A starting note and a bit of additional info: Examples are for C# cause I don't know the Java syntax by heart, but I'm expecting that it's the same.

  • The [FindsBy] attribute is getting as parameters 2 values. First is an Enum that specifies how to find the element (id, xpath, css selector, etc) and the second, more important one for this example, is the Using that receives a constant string. This const string is used to specify the actual element id , xpath , etc.
  • Most of the ExpectedConditions static methods receive as a parameter a By element

Having in mind the above statements, I next went and determined what are the elements on the page that I needed to use an explicit wait and built my code:

const string loginButtonXpath = "xpath_here";
[FindsBy(How = How.Xpath, Using=loginButtonXpath)]
private IWebElement LoginButton {get;set;}

private By LoginButtonBy
{
    return By.Xpath(loginButtonXpath);
}

Having it set this way, a have the IWebElement button that I need to click, I have the By locator that I can use in my explicit wait and also I have the method to identify the element on the page only in one place, making as easy as before to maintain my automation solution when any changes occurred.

I'm curious how others handled this without losing the built in explicit wait methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM