Friday, August 7, 2020

How to check for existence of element by webDriver fast?

Simple answer: by JS evaluation. 
In given samples the Selenium performance almost twice over webDriver.findElements(). 

Cluecumner report on timing: 



Test .feature file:

And test element presence "50000" times by findElements
And test element presence "50000" times by js 

Java code 


@And("^test element presence \"(.*?)\" times by findElements")
public void testElement(String n) {
int number = Integer.parseInt(n);
for (int index = 0; index < number; index++) {
List<WebElement> list = webDriver.findElements(By.cssSelector("coral-multifield-item,a[fake=\""+n+"\"]"));
assert( list.size()>0 );
}
}
@And("^test element presence \"(.*?)\" times by js")
public void testElementByJs(String n) {
int number = Integer.parseInt(n);
for (int index = 0; index < number; index++) {
JavascriptExecutor js = (JavascriptExecutor) webDriver;
String o =(String)js.executeScript("return ''+!!document.querySelector('coral-multifield-item,a[fake=\""+n+"\"]')");

assert( "true".equals(o) );
}
}

No comments:

Post a Comment