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
- trigger Test -> Selenium (e.g. Java) code converts to JSON
- sends to BrowserDriver throught HTTP
- Browser drivers communicate with its respective browser: It executes the commands by interpreting JSON and gives response back to Browser Driver.
- 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");
Create an object for the Class and make Class object reference to the Webdriver Interface which you want to implement.
WebDriver driver = new ChromeDriver();
There are several methods that are available from the Webdriver interface. These methods are accessed using the instance variable driver.
driver.methodName();
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
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"));
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
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();
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)