Reputation: 920
Why does this work?
public void mymethod(){
wait.until(ExpectedConditions.invisibilityOfElementLocated(by.id("myid"));
}
And this doesn't work? I don't understand.
@FindBy (id="myid")
WebElement myid;
public mypagefactory(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver);
}
public void mymethod(){
wait.until(ExpectedConditions.invisibilityOfElementLocated((By) myid));
}
I keep getting an "invalid cast" error on the "(By)". I'm trying to use the page factory methodology.
Upvotes: 2
Views: 3118
Reputation: 1
My solution when I ran into needing the By locator to use for an ExpectedConditions and I had my locators in the Page Object Factory was to use a String that had the locator in it and then build my By object and the element locator from that.
public class PageObject(){
private static final String ID = "myid";
public @FindBy (id=ID)
WebElement myid;
public By getByID(){
return new By.ById(ID);
}
public PageObject(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver);
}
}
public void mymethod(){
By by = getByID();
wait.until(ExpectedConditions.invisibilityOfElementLocated(by);
}
Upvotes: 0
Reputation: 42528
The condition invisibilityOfElementLocated
is expecting a locator of type By
, but you are providing the proxied WebElement
. Use invisibilityOfAllElements
instead:
wait.until(ExpectedConditions.invisibilityOfAllElements(Arrays.asList(myid)));
Upvotes: 1
Reputation: 13980
The statement
@FindBy (id="myid")
WebElement myid;
is an equivalent of
By by = By.id("myid");
WebElement myid = driver.findElement(by);
As you see myid
is of type WebElement
in both cases (annotation does not convert it into object of type By
), while casting from WebElement
to By
is not possible, since they are not sharing the same class hierarchy.
Upvotes: 1
Reputation: 148
You are trying to cast a WebElement to By, but it's not a By element, it's WebElement.
by.id("myid") returns By, so there's no problem.
Upvotes: 1