Selenium-Webdrivers Architecture
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Selenium Webdriver Test Configuration
Host Machine
Selenium Selenium
Java Webdriver
Library API’s
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Software Requirements:
- Java
- Selenium Lib Jar
- Eclipse
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Launching Firefox Browser
import org.openqa.selenium.firefox.FirefoxDriver;
WebDriver driver = new FirefoxDriver();
Note : No need of driver file for selenium 2.0
For selenium 3.0 & above required geckodriver
https://github.com/mozilla/geckodriver/releases Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Launching Chrome Browser
import org.openqa.selenium.chrome.ChromeDriver;
WebDriver driver = new ChromeDriver();
Note : Need of driver file to set in system/user variable
https://sites.google.com/a/chromium.org/chromedriver/downloads
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Launching Internet Explorer
Browser
import org.openqa.selenium.ie.InternetExplorerDriver;
WebDriver driver = new InternetExplorerDriver();
Note : Need of driver file to set in system/user variable
http://selenium-release.storage.googleapis.com/index.html
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Finding web-element on webpage
WebElement element = driver.findElement(By.id("element_id"));
WebElement element = driver.findElement(By.name("element_name"));
WebElement element = driver.findElement(By.className("element_class"));
WebElement element = driver.findElement(By.xpath("element_xpath"));
WebElement element = driver.findElement(By.cssSelector("element_css"));
WebElement element = driver.findElement(By.tagName("element_tagName"));
WebElement element = driver.findElement(By.linkText("link_text"));
WebElement element = driver.findElement(By.partialLinkText("partial_link_text"));
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with Text-Field
First name: <input type="text" id="fname" name="FirstName">
WebElement element = driver.findElement(By.id("fname")); // Find Textfield Element
element.sendKeys("test"); // Enter text into textfield element
element.clear();
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with Button
<button id=”register” type="button" >Click Me!</button>
WebElement element = driver.findElement(By.id("register")); // Find button Element
element.click(); // click button Element
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with Radio Button
<input type="radio" name="gender" value="male" checked> Male
<input type="radio" name="gender" value="female"> Female
// Find radio button element
WebElement element = driver.findElement(By.xpath(".//input[@value=’male’]"));
element.click(); // Select radio button
element.isSelected(); // Verify is radio button selected or not
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with checkbox
<input type="checkbox" name="vehicle" value="Bike">Car
// Find checkbox element
WebElement element = driver.findElement(By.name("vehicle"));
element.click(); // Select checkbox
element.isSelected(); // Verify is checkbox selected or not
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with dropdown
<select id=”Cars”>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
</select>
// Find Dropdown element
WebElement dd_element = driver.findElement(By.id("Cars"));
// Get all dropdown options
Select options = new Select(dd_element);
options.selectByIndex(3); // Select option by index
options.selectByValue("saab"); // Select option by value
options.selectByVisibleText("Opel"); // Select option by text
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with Multiselect dropdown
<select id=”Cars” multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
</select>
// Find Dropdown element
WebElement element = driver.findElement(By.id("Cars"));
// Get all dropdown options
Select options = new Select(element);
options.selectByIndex(3); // Select option by index
options.selectByValue("saab"); // Select option by index
options.selectByVisibleText("Opel"); // Select option by index
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with alerts
<button id="hello" onclick="alert('hello world')">Try it</button>
// Find link Element
WebElement element = driver.findElement(By.id("hello"));
element.click();
// switch to alert and accept it (click OK)
driver.switchTo().alert().accept();
// switch to alert and dismiss it (click Cancel)
driver.switchTo().alert().dismiss();
// switch to alert and get alert text
driver.switchTo().alert().getText(); Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with links
<a href="http://www.w3schools.com/html/">w3schools</a>
// Find link Element
WebElement element = driver.findElement(By.linkText("w3schools"));
element.click();
WebElement element = driver.findElement(By.partialLinkText("schools"));
element.click();
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with browser windows
// Store the current window handle
String currentWindowHandle = driver.getWindowHandle();
// click some link
// Switch to new window opened
for(String winHandle : driver.getWindowHandles())
driver.switchTo().window(winHandle);
// perform operation
// close new window
driver.close();
// Switch back to original browser (first window)
driver.switchTo().window(currentWindowHandle); Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4
Working with frames
<frameset>
<frame src="frame_a.htm" name=”frame_a”>
<frame src="frame_b.htm" id=”frame_b”>
</frameset>
//Select frame by name
driver.switchTo().frame("frame_a");
//Find frame element
WebElement frameB = driver.findElement(By.id("frame_b"));
// Select frame by frame element
driver.switchTo().frame(frameB)
// Select to previous/main content
driver.switchTo().defaultContent();
Created By : Sameer Sawant
https://www.linkedin.com/in/sameersawant4