Posted in Java, QA, Tips

Useful scripts for Selenium in Java


Selenium HQ is really useful for testing, you can find its info at Selenium Home Page . During working on it in Java projects, I found some scripts which help us save time for test cases implementation.

  • First is the necessary component in Selenium lib:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
  • Constructors:
WebDriverWait wait = new WebDriverWait(driver, 60);
WebDriver driver;
  • Collapse / Expand

This script is an action of Mouse when User clicks on an expand-icon for collapsing or expanding

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("$('.expand-icon')[0].dispatchEvent(new MouseEvent('click'))");
  • Click on a button:
String buttonId = "//button[@id='button_id_here']";
  • Check a webpage exist or not
String pageTitle = "//span[contains(.,'Page_Title')]";
  • Find a button
String buttonElement = "//button[@title='Button text here']";
WebElement buttonElement = driver.findElement(By.xpath(buttonElement ));
  • Find a content:
String contentId = "//span[@id='Content ID Here']";
WebElement contentElement = driver.findElement(By.xpath(contentId ));
  • Others (continues)



If this topic is helpful for you. Please share 🙂