问题
While end-to-end testing our app, I am often waiting for a state transition (a modal to close, a button to appear, etc). Blind experimentation has led me to sometimes use browser.wait(ExpectedConditions.presenceOf(someElement), and other times use browser.wait(someElement.isPresent()).
To me, the names imply they're interchangeable. But it isn't so. Is there something about the situations in which I use these that will help me tell which to use when?
回答1:
In my experience, I have used element.isPresent() for when I do not want to specifically verify something. For example I can write:
element.isPresent().then(function(elm){
if(elm) { //if the element is present, do something }
else { //if the element is not present, do something else }
});
But if I need some element to be present/visible/clickable in order to execute an expect or something else, then I would use a browser.wait(EC.presenceOf/visibilityOf/elementToBeClickable(element))
It also depends on the element you need to wait for. If its a button that you need to click, you'll have to use elementToBeClickable
回答2:
If we take a look at the code, we will see that isPresent method looks like this
isPresent(): wdpromise.Promise<boolean> {
return this.count().then((count) => {
return count > 0;
});
}
While presenceOf is dependent on isPresent
presenceOf(elementFinder: ElementFinder): Function {
return elementFinder.isPresent.bind(elementFinder);
};
In fact they are doing the same. But note, that ExpectedConditions operators, like not, or and etc. expecting ExpectedConditions function as an argument whichpresenceOf is, while isPresent is not.
Sometimes you need more complicated conditions, than to check that one particular element is present browser.wait(someElement.isPresent()). This is where ExpectedConditions constructions are incredibly useful and this is where you would need presenceOf.
来源:https://stackoverflow.com/questions/55049961/when-should-one-use-element-ispresent-over-expectedconditions-presenceofeleme