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

Selenium Automation-Basics

Uploaded by

lavanyaks21cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Selenium Automation-Basics

Uploaded by

lavanyaks21cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Selenium Automation-Basics

Tools
Selenium-Definition
Components
Components
Selenium Components
Selenium components
Selenium components-Selenium RC
Selenium components-Selenium
Web driver
Selenium components-Selenium
Web driver
Selenium components-Selenium
Web driver
Selenium WebDriver Framework
Architecture

It has four main Components:


Selenium Client library- allows us to write the Selenium automation scripts in the
language of our choice – Java, Python, C#, Ruby, Javascript, etc.
JSON wire protocol over HTTP- lightweight data format for storing and transferring
data.
Browser Drivers- allow communication between the Selenium script with the different
browsers. Also, the communication happens without revealing the internal logic of those
browsers.
Browsers- receive the command and call the respective method to perform the desired
Selenium Webdriver
Selenium WebDriver Automates Web browsers through a three-step process:
• Command Translation: Your test commands are converted into HTTP requests
using a protocol called JSON wire protocol.
• Browser Driver Initialization: Each web browser you want to automate has a
corresponding browser driver (e.g., ChromeDriver for Chrome). When you run your
Selenium script, the browser driver is initialized, which starts an HTTP server.
• Command Execution: The browser receives instructions through the initialized
HTTP server and carries them out.
• Example: Launching Chrome and Navigating to a Website
WebDriver driver = new ChromeDriver();
driver.get(“https://www.amazon.com”);
Types of Request:
• GET: GET request is used to read/retrieve data from a web server. GET returns an
HTTP status code of 200 (OK) if the data is successfully retrieved from the server.
• POST: POST request is used to send data (file, form data, etc.) to the server. On
successful creation, it returns an HTTP status code of 201.
Webdriver Locators
Locators
Locate by id
Locators by name
Locators by text
Locators by CSS or xpath
Different Web Drivers
Actions on Web Elements
1. Clicking on Elements

2. Entering Text into Input Fields


• sendKeys() method simulate typing or entering text.

3. Clearing Text from Input Fields


element.clear();
4. Selecting Options from Dropdowns:
selectByVisibleText(), selectByValue(), selectByIndex()
Actions on Web Elements
5. Retrieving Information from Elements:
getText(), getAttribute(), isDisplayed(), isEnabled().

6. Submitting Forms:
Webdriver event Listener
• Defines number of methods that are called when certain events occur.
• Register listener using register() method.
• Once listener registered, it will be notified when certain events occur.
• WebDriver Events used to monitor the state of browser and react to change in the
browsers state.
• Event listeners are objects that implement interfaces such as
WebDriverEventListener or EventListener to handle different types of events during
test execution.
• Types of Webdriver events:
– beforeNavigateTo: trigger before navigating to new URL
– afterNavigateTo: trigger after successfully navigating to new URL
– beforeNavigateBack: triggered before navigating back in the browser history
– afterNavigateBack: triggered after successfully navigating back in the browser history
– beforeNavigateForward: triggered before navigating forward in the browser history
– afterNavigateForward: triggered after navigating successfully forward in the browser history
Types of Webdriver events
• beforeFindBy: triggered before locating a web element on the page.
• afterFindBy: triggered after successfully locating a web element on the page.
• beforeClickOn: triggered before clicking on a web element.
• afterClickOn: triggered after clicking on a web element.
• beforeChangeValueOf: triggered before changing the value of a web element.
• afterChangeValueOf: triggered after changing the value of a web element.
• beforeScript: triggered before executing javascript code.
• afterScript: triggered after successfully executing javascript code.
• onException: triggered when an exception occurs during test execution.
Example code for webdriver events
public class WebEventListener implements WebDriverEventListener {

public void beforeNavigateTo(String url, WebDriver driver) {


System.out.println("Before navigating to: '" + url + "'");
}
public void afterNavigateTo(String url, WebDriver driver) {
System.out.println("Navigated to:'" + url + "'");
}
public void beforeChangeValueOf(WebElement element, WebDriver driver) {
System.out.println("Value of the:" + element.toString()+ " before any changes
made");}
public void afterChangeValueOf(WebElement element, WebDriver driver) {
System.out.println("Element value changed to: " + element.toString());
}
public void beforeClickOn(WebElement element, WebDriver driver) {
System.out.println("Trying to click on: " + element.toString());
}
public void afterClickOn(WebElement element, WebDriver driver) {
System.out.println("Clicked on: " + element.toString());
}
public void beforeNavigateBack(WebDriver driver) {
System.out.println("Navigating back to previous page");
}
public void afterNavigateBack(WebDriver driver) {
System.out.println("Navigated back to previous page");
}
public void beforeNavigateForward(WebDriver driver) {
System.out.println("Navigating forward to next page"); }


public void afterNavigateForward(WebDriver driver) {
System.out.println("Navigated forward to next page");
}
public void onException(Throwable error, WebDriver driver) {
System.out.println("Exception occured: " + error);
}
public void beforeFindBy(By by, WebElement element, WebDriver driver) {
System.out.println("Trying to find Element By : " + by.toString());
}
public void afterFindBy(By by, WebElement element, WebDriver driver) {
System.out.println("Found Element By : " + by.toString());
}
• /* * non overridden methods of WebListener class */
public void beforeScript(String script, WebDriver driver) {
} public void afterScript(String script, WebDriver driver) {
• }}
Selenium Grid
Selenium Grid

You might also like