DEV Community

Cover image for Selenium Java Cheat Sheet
Anne Quinkenstein
Anne Quinkenstein

Posted on • Edited on

Selenium Java Cheat Sheet

Selenium

Selenium is open Source Automation Testing tool which is exclusively for webbased applications.

Architecture

simplified

3 Components
  • Selenium Java Client Library (with Java, C++, ..)
  • Browser Driver(chromedriver, geckodriver(Firefox), msedgedriver,...)
  • Browser (Chrome, Firefox, Edge, Safari, ...)
Communication
  • Selenium Library (Client)........ <-JSON(over HTTP)->
  • Browser Driver (Server) ......... <-HTTP(over HTTP)->
  • Browser
  1. trigger Test -> Selenium (e.g. Java) code converts to JSON
  2. sends to BrowserDriver throught HTTP
  3. Browser drivers communicate with its respective browser: It executes the commands by interpreting JSON and gives response back to Browser Driver.
  4. Browser Driver wraps Browserresponse in Json and sents it back to Client.

Using the Webdriver API

Every automation Java work file starts with creating a reference of web browser.
Webdriver is the Interface. ChromeDriver( or other drivers) is the class that implements the interface (which is all of the methods defined in the interface)
Download selected driver, hint Selenium to the location

System.setProperty("webdriver.chrome.driver", "//home//IdeaProjects//chromedriver");
Enter fullscreen mode Exit fullscreen mode

Create an object for the Class and make Class object reference to the Webdriver Interface which you want to implement.

WebDriver driver = new ChromeDriver();
Enter fullscreen mode Exit fullscreen mode

There are several methods that are available from the Webdriver interface. These methods are accessed using the instance variable driver.

 driver.methodName();
Enter fullscreen mode Exit fullscreen mode

open && close URL in a Browser

driver.get("https://google.com");
//might be checked with
String url = driver.getCurrentUrl();
boolean result = driver.getPageSource().contains("String to find");
String title = driver.getTitle();
//navigate
driver.navigate().back();
driver.navigate().forward();
//close
driver.close(); // closes only current window
driver.quit(); // closes all the windows opened by WebDriver instance

Enter fullscreen mode Exit fullscreen mode

Locators

driver.findElement(By.id("username")).sendKeys("This is my first code");
driver.findElement(By.name("pw")).sendKeys("123");
driver.findElement(By.className("id-ig"));
Enter fullscreen mode Exit fullscreen mode

Not every object might got an ID, className or name, than you can use Xpath or CSS selectors
if there are multipl values - Selenium identifies the first one

caution ID, Class

If it is an alpha numeric id, check if it varies on every refresh.
Classes should not have spaces- Compound classes will not be accepted

xpath Syntax

//tagName[@attribute='value']  
//tagName[contains(@attribute,'value')]  -  regEx
with index //*[@id=’login’]/ul/li[3]/a
css is not recommended, cause it counts hidden childs as well

driver.findElement(By.xpath("//*[@id='Login']")).click();

traverse to sibling 
//*[@id='tablist1-tab1']/following-sibling::li[2]
traverse back to Parent element from Child 
//*[@id='tablist1-tab1']/parent::ul
Enter fullscreen mode Exit fullscreen mode

more on Xpath Contains, Following Sibling, Ancestor & Selenium AND/OR

//contains in CSS Selectors is

CSS selector Syntax

tagName[attribute='value']   
tagName#id  
tagname.classname
tagName[Atrribute*='value'] - regEx

 driver.findElement(By.cssSelector("[class='datepicker-days'] th[class='next']")).click();

//contains is *
driver.findElement(By.cssSelector("[id*='SeniorCitizenDiscount']")).click();   

Enter fullscreen mode Exit fullscreen mode

check in Browser with cropath extention or the console with:
$("") - for css , $x("") or xpath

Multiple values - Selenium identifies the first one- Scans from top left
id, class, xpath, css selectors, linkedText
id: check if it is changing

Top comments (0)