0% found this document useful (0 votes)
260 views

Selenium Webdriver

The document describes the Selenium WebDriver architecture and how to configure tests using Selenium. It includes how to launch browsers like Firefox, Chrome, and Internet Explorer. It also provides examples of how to interact with common web elements like text fields, buttons, radio buttons, checkboxes, dropdowns, links, windows, frames and alerts using the Selenium WebDriver API.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
260 views

Selenium Webdriver

The document describes the Selenium WebDriver architecture and how to configure tests using Selenium. It includes how to launch browsers like Firefox, Chrome, and Internet Explorer. It also provides examples of how to interact with common web elements like text fields, buttons, radio buttons, checkboxes, dropdowns, links, windows, frames and alerts using the Selenium WebDriver API.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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

You might also like