Wednesday, 6 December 2017

Selenium Tips and Tricks


Get the DOM Object into View:


At times, few actions are not performed on a DOM object until it is visible in the view-port. Below is the small code snippet to get the DOM object into view using WebDriver's 'JavascriptExecutor' interface. 

WebElement element = driver.findElement(By.id("someId"));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);",element);

OR

((JavascriptExecutor) driver).executeScript("document.getElementById('text123').scrollIntoView(true);");

Selenium WebDriver Wait Commands:

For any automation scripts to work without any sync issues with application, wait statements are very important. Selenium WebDriver provide different types of Wait conditions to make automation scripts effective.

Below are different types of waits & its usage.




  • Implicit Wait: An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element. The default setting is 0.
    NOTE: Implicit wait is set for the life of the WebDriver instance.

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

  • Explicit Wait: An explicit wait is wait time explicitly defined to wait for a certain condition to occur before proceeding to the next step. Setting Explicit wait using 'WebDriverWait' is an effective way to handle certain expected conditions.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

Other Expected Conditions:


wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("someid")));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id= 'TestId'"), "Sample Text"));
wait.until(ExpectedConditions.textToBePresentInElementValue(By.xpath("//div[@id= 'TestId'"), "Sample Text"));
wait.until(ExpectedConditions.alertIsPresent());
wait.until(ExpectedConditions.titleContains("Sample Title"));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("newFrameId")));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("someid"))); 
wait.until(ExpectedConditions.invisibilityOfElementWithText(By.id("someid"), "txt"));

Expected Conditions with Logical Operators:


// When both conditions returns true
wait.until(ExpectedConditions.and(
ExpectedConditions.invisibilityOfElementLocated(By.id("TestLink")),
ExpectedConditions.invisibilityOfElementLocated(By.name("TestProd"))));

// When either of the condition returns true
wait.until(ExpectedConditions.or(
ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='test']")),
ExpectedConditions.presenceOfElementLocated(By.id("devId")),
ExpectedConditions.presenceOfElementLocated(By.name("Rel"))));

Wait for Ajax Calls to Complete:


public boolean waitForAjaxCalls(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 30);

ExpectedCondition<Boolean> jQueryWait = new ExpectedCondition<Boolean>() {
  @Override
  public Boolean apply(WebDriver driver) {
try {
  return ((Long)((JavascriptExecutor)driver).executeScript("return jQuery.active") == 0);
}
catch (Exception e) {
  return true;
}
  }
};

ExpectedCondition<Boolean> jsWait = new ExpectedCondition<Boolean>() {
  @Override
  public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").toString().equals("complete");
  }
};

return wait.until(jQueryWait) && wait.until(jsWait);
}

Set Wait Timeouts:


driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(100, TimeUnit.SECONDS);


Handle Split Button Click:

Normally split buttons are put in "<em>" tag & getting the locator working  is tricky.
Long xpaths might not work with "<em>" tag, even though you find that correct in FirePath or any xpath vaidator. This would not work during execution.


As per my experience with split button automation, it is good to go with shorter xpath containing <em> tag's className or id.

Below example to make split button work:


System.setProperty("webdriver.chrome.driver","pathTo/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://any url/");

// To handle Split Button
driver.findElement(By.xpath("//td/em[@class='x-btn-split']")).click();

driver.close();




1 comment:

  1. Thanks for providing the information . The articles in your blog helped me a lot for improving the knowledge on the subject. Also check my small collection on this at selenium Online Training Bangalore blog

    ReplyDelete