What is Selenium?
- Selenium is automation tool which is used to automate web based application
for software automation process.
- It contains several classes, commands which are useful to handle different
web element.
Selenium Tool Suite
Selenium Integrated Development Environment (IDE)
Selenium Remote Control (RC)
WebDriver
Selenium Grid
Advantages of Selenium:
It is open source automation tool.
It supports multiples programming languages such as java, python, C sharp etc.
Cross browser testing is possible
Selenium supports Parallel Test Execution.
Selenium Test Case Execution time is faster than other tools like UFT, RFT, TestComplete, SilkTest, etc.
Disadvantages of Selenium:
We can’t automate desktop based application.
We can’t automate standalone applications.
We can’t automate captcha code using selenium.
We can’t read barcode using selenium tool.
It doesn’t support file uploading.
Limited support for Image Testing.
Different java concepts used in selenium:
Inheritance
Interface
Polymorphism
Casting (upcasting)
Encapsulation
Abstraction
Arrays
Collection , List, HashMap, HashSet
Loops
Control statements
String
Selenium Types:
1. Selenium IDE:
- IDE stands for Integrated Development Environment.
- In this version of selenium we can’t perform compatibility testing.
- We can run our script only in Mozilla Firefox browser.
- We have record and playback option in this IDE.
2. Selenium RC:
- This type of selenium can support compatibility testing.
- That means we can run test scripts in different browser such as chrome,
Firefox, internet explorer etc. but we can use only Java programming language to
write the script.
3. Selenium Webdriver:
-This type of selenium can support compatibility testing. That means we can run
test scripts in different browser such as chrome, Firefox, internet explorer etc.
- It can support multiple programming languages such as java, python C sharp
etc to write the script.
- Currently we are using selenium tool having version 4
4. Selenium grid
- It works with Selenium RC.
- Server is required for selenium grid to start automation
- Core engine is JavaScript base
Selenium Architecture:
1. Search Context: It is a super most interface in selenium. It consists of all abstract methods and that methods are inherited to the Webdriver interface.
2. Webdriver: It is an interface present in selenium which consists of two types of abstract methods that is abstract methods of search context and his own abstract methods.
3. Selenium Remote Webdriver: It a class which implements all the abstract methods of both the interfaces that is search context and Webdriver. This implementation class is extended to the different browsers such as chrome. Firefox, internet explorer etc.
4. Browser driver: For compatibility testing we need to use runtime polymorphism to perform up casting in selenium. For example, to open a browser using script, we create an object of chrome driver with reference of Webdriver interface.
WebDriver driver = new ChromeDriver ();
WebElement
The term WebElement refers to a HTML element. The HTML documents are
composed of HTML elements. It consists a start tag, an end tag and
the content in between.
WebElement commands
Before going through each and every action of WebElement, just understand
that how we get a WebElement object/element. we
learned that every method of the WebDriver either returns something or return
void(means return nothing). The same way find Element command
of WebDriver returns WebElement.
WebElement can be of any type, like it can be
a Text, Link, Radio Button, Drop Down, Web Table or any HTML element. But
all the actions will always populate against any element irrespective of whether
the action is valid on the WebElement or not. For e.g. clear() command, even if
you have a link element still you get the option to choose clear() command on it,
which if you choose may result in some error or may not does anything.
The findElement
The findElement(By, by) method searches and locates the first element on
the current page, which matches the criteria given as a parameter. This
method is usually used in commands to simulate user actions like click, submit,
type etc.
findElement | findElements |
Throws NoSuchElementException in case there are no matching elements. | Returns an empty list in case there are no matching elements. |
Returns a single web element | Returns a collection of web elements |
DOM:
It is an API interface provided by the browser.(every browser has this api
internally).
DOM stands for Document Object Model. In simple words, DOM specifies the
structural representation of HTML elements.
When a web page is located, the browser automatically creates the DOM of the
page
All the html tags and hierarchy will be aligned in the form of DOM structure.
Locators:
What is Locator?
The locator can be termed as an address that identifies a web element uniquely within the web page.
Locators are the HTML properties of a web element which tells the Selenium about the web element
it needs to perform the action on.
Locator is a command that tells Selenium IDE/Selenium which GUI elements (say TextBox, Buttons,
Check Boxes etc) its needs to operate on.
Identification of correct GUI elements is a prerequisite to creating an automation script.
Locating By ID :
This is the most common way of locating elements since ID's are supposed to be unique for each
element.
Example: driver.findElement(By.id("UserName"));
Locating By Name :
Locating By ClassName :
Locating By Link Text
This type of locator applies only to hyperlink texts. We access the link by prefixing our target with "link="
and then followed by the hyperlink text.
Example: driver.findElement(By.linkText("Login Page"));
Locating By Partial Link Text
XPath Locators -Types
CSS Selector
CSS Selector Locators
1> Tag and ID
Xpath Locators -Methods
1> Contains() is a method used in XPath expression. It is used when the value of any attribute changes dynamially, for example , login information
Complete value of 'Type' is 'submit' but using only partial value 'sub' -> Syntax: Xpath = //input[contains(@type,'sub')]
Complete value of 'name' is 'btnLogin' but using only partial value 'btn' -> Syntax: Xpath = //input[contains(@name,'btn')]
Xpath using Contains() Sample Example : -
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class xpathusingcontains_example1
{
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\shree\\Desktop\\Automation jar\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("C:\\Users\\shree\\Desktop\\HTMLcode\\demo.html");
Thread.sleep(1000);
//enter username
driver.findElement(By.xpath("//input[contains(@class,\"1\")]")).sendKeys("abhgj");
//enter password
driver.findElement(By.xpath("//input[contains(@type,\"pass\")]")).sendKeys("abgc");
//open facebook application
driver.findElement(By.xpath("//a[contains(text(),\"Face\")]")).click();
driver.quit();
}
}
2> Starts-with() function finds the element whose attribute value changes on refresh or any operation on the webpage.
For example :- Suppose the ID of particular element changes dynamically like Id ="message12" | Id = "message345" | Id ="message8769"
Syntax : Xpath = //label[starts-with(@id,'message')]
Xpath using starts-with Sample Example : -
package Locators;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class xpathusingstartswith_example1 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\shree\\Desktop\\Automation jar\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
Thread.sleep(2000);
driver.findElement(By.xpath("(//button[starts-with(@class,\"_2KpZ6l\")])[1]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//div[starts-with(text(),\"Mob\")]")).click();
}
}
3> Text() function , we find the element with exact text match as shown below . Suppose, we find the element with text "UserID"
Syntax : Xpath = //td[text()="UserID"]
WebElement:
It is an interface use to perform action on element present on webpage.
If we want to perform multiple actions on same element then we can find that element at once and store it in reference variable having data type as web element. It will remove every time finding of same element. We just use that reference variable and perform the desired action on it.
Some important web element methods are given below.
Sendkeys( ) :
This method is use to enter value in the input/text field
Syntax:
WebElement ele=location of element; ----identify the webelement ele.sendKeys(“value”); -------perform action on that webelement
Clear( ):
Click( ):
getText:
isEnabled():
isDisplayed():
isSelected( );