Selenium Java Cheat Sheet
Tags: testing, testautomation, selenium
Published: 2021-01-23 09:03:46

#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
```java
System.setProperty("webdriver.chrome.driver", "//home//IdeaProjects//chromedriver");
```
Create an object for the Class and make Class object reference to the Webdriver Interface which you want to implement. 
```java
WebDriver driver = new ChromeDriver();
``` 
There are several methods that are available from the Webdriver interface. These methods are accessed using the instance variable driver. 
```java
 driver.methodName();
```
### open && close URL in a Browser
```java
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

```
### Locators
```java 
driver.findElement(By.id("username")).sendKeys("This is my first code");
driver.findElement(By.name("pw")).sendKeys("123");
driver.findElement(By.className("id-ig"));
``` 
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
```java 
//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
``` 
more on [Xpath Contains, Following Sibling, Ancestor & Selenium AND/OR] (https://www.guru99.com/using-contains-sbiling-ancestor-to-find-element-in-selenium.html#:~:text=A%20Sibling%20in%20Selenium%20Webdriver,Xpath%20expression%20in%20selenium%20webdriver.)

//contains in CSS Selectors is 
#### CSS selector Syntax
```java
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();   

``` 


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