unit 5(Selenium Introduction)
unit 5(Selenium Introduction)
Introduction to selenium
Selenium is one of the most popular open-source automated testing Tools available
today.
Selenium automates web applications for testing purposes but is certainly not limited to
just that.
Introducing Selenium: Selenium is not a single tool, instead it is a set of different tools
each with a different approach to support test automation.
Learning all the tools will give you different options for approaching different test
automation problems.
These tools when used together give many options for locating the UI elements and
comparing expected test results against actual application behavior.
The following are the four tools that comprise the Selenium's Tools Suite:
1. Selenium IDE
2. Selenium RC (Outdated)
3. Selenium WebDriver
4. Selenium Grid
Few
1. Selenium is a browser based testing tool. It can’t be used for automating the desktop
applications and mobile applications. (i.e. It can be used only for automating the web based
applications)
The following are the list of supported browsers on which Selenium Automation scripts can be
executed:
Chrome Browser
Firefox Browser
Internet Explorer Browser
Safari Browser
Opera Browser
The following are the list of supported Operating System Environments on which Selenium
Automation scripts can be executed:
Microsoft Windows OS
Apple OS X
Linux OS
The following are the list of programming languages using which the Selenium Automation
scripts can be developed:
1) Selenium IDE
2) Selenium RC
3) Selenium WebDriver
4) Selenium Grid
Selenium IDE is a firefox extension which is used to record the tests like recording a
video and playback the recorded tests like playing a video.
Using Selenium IDE we can create Selenium Automation Code without writing the code
instead the code gets generated automatically when we record the tests using Selenium
IDE. Where as with Selenium RC and Selenium WebDriver tools we need to write the
code ourselves.
We use Selenium RC and Selenium WebDriver to write automation scripts for complex
projects where as Selenium IDE can only be used to automate simple projects.
Can we ignore Selenium IDE as we can automate all our tests using Selenium 1
and Selenium 2 ?
No, you cannot ignore Selenium IDE. It is the only selenium tool which has record and
playback feature.Selenium 1 and Selenium 2 don't have this feature. We can use
Selenium IDE to automate simple project.
Reason#1 - In order to automate simple scripts, the tester may not require Selenium 1
and Selenium 2 tools, hence tester will just record and playback using Selenium IDE.
Reason#2 - While automating tests using Selenium 1 or Selenium 2, the tester may
face problems while writing the automation scripts. In this case the tester will record and
playback the test using the Selenium IDE tool and view the automation code generated
by the tool. He/She will compare his Selenium 1 or Selenium 2 self written code with
Selenium IDE's auto generated code, and make necessary changes to his Selenium 1
or Selenium 2 code in order to make it work.
Recording Process:
1. Launch Firefox and open Selenium IDE from Tools Menu as shown below:
2. Ensure that the Selenium IDE is launched as shown below:
3. After the Selenium IDE is launched, ensure that the Recording Button is turned on by default.
(i.e. Selenium IDE is in recording mode by default on launching) as shown below:
4. While the recording button is enabled, open any website say www.google.com in the Firefox
browser as shown below:
5. In the 'Google' page, click on 'I am feeling lucky' button as shown below:
6. Turnoff the 'Recording' button on the Selenium IDE to stop recording as shown below:
7. Ensure that the recorded commands are displayed under the table tab as shown below:
Playback Process:
8. Open any other site say www.yahoo.com in the Firefox browser and Click on the 'Play
current test case' button on the Selenium IDE as shown below:
There are two mechanism for validating elements that are on the application under test
using Selenium IDE. In order to understand what actually the validation means, please
go through the below examples first:
Examples of Validations:
The following steps will show how assert mechanism validates elements:
Example: Check whether the Google Logo is available on the www.Google.com page. (i.e. The
test will pass if the Google Logo exists on the Google page else it fails).
6. In the right click menu options, select 'Show All Available commands' option as shown below:
7. Ensure that the sub-menu option for the 'Show All Available commands' option is displayed
and select 'AssertElementPresent' option as shown below:
8. In Selenium IDE -> Table tab, ensure that assertElementPresent element is added as shown
below:
9. Click on the 'Record' button to turn off the recording process as shown below:
10. Playback or Run the above recorded validation, by click on the 'Play current test case'
option on the Selenium IDE as shown below:
11. After the test Run, ensure that the 'assertElementPresent' command under the Selenium
IDE -> Table tab is displayed in Green color (i.e.Green means the step got passed and the UI
element is present i.e. Google Logo in this example is present on the Google Page) as shown
below:
12. If the element is not present (i.e. Google Logo in this example), then the step will fail and
displayed in red color as shown below:
13. If in case the test fails as shown in the step 12, all the steps to be
executed after the failed step wont be executed
Validate elements using verify mechanism
The following steps will show how verify mechanism validates elements:
Example: Check whether the Google Logo is available on the www.Google.com page. (i.e. The
test will passes if the Google Logo exists on the Google page else it fails).
6. In the right click menu options, select 'Show All Available commands' option as shown below:
7. Ensure that the sub-menu options for the 'Show All Available commands' option is displayed
and select 'VerifyElementPresent' option
8. In Selenium IDE -> Table tab, ensure that verifyElementPresent element is added .
9. Click on the 'Record' button to turn off the recording process .
10. Playback or Run the above recorded validation, by click on the 'Play current test case'
option on the Selenium IDE
11. After the test Run, ensure that the 'verifyElementPresent' command under the Selenium IDE
-> Table tab is displayed in Green color (i.e.Green means the step got passed and the UI
element is present
12. If the element is not present (i.e. Google Logo in this example), then the step will fail and
displayed in red color as shown below:
13. If in case the test fails as shown in the step 12, all the steps to be executed after the failed
step will also get executed
Example#1 - Suppose we've to automate a test, where the User have to log
in to Facebook
Example#2 - Suppose we've to automate a test, where the User have to search for 'selenium-
by-arun' in Google.
Identifying an UI element on the application screen is an important feature for any automation
tool. Locators allows the Selenium Automation tool to find UI elements on a page that can be
used in our automation tests.
i.e. The Selenium Tool uses Locators to find and match the elements on your page that it needs
to interact with while running the automation scripts.
1. ID
2. Name
3. Class Name
4. Link
5. CSS
6. XPath
Practically, most of the people will try to locate the UI element using a unique ID. If
unique ID value is not available, they will use CSS selector locator followed by XPath
locator.
Why different types of locators are used instead of having only one kind of locator to
identify the UI elements ?
While building the application code, it is seen as a good practice to make sure that
every element you need to interact with has an ID attribute and a Name attribute.
Unfortunately, developers may not have provided ID or Name attributes to some
elements in your application. So identifying those kind of elements may not be possible
if we don't use the other type of locators like XPath or CSS selector locators
1. Do we have to install anything in Internet Explorer? The Answer is No. Starting from
Internet Explorer 7 to the latest version of the browser, IE Developer Tools are inbuilt in the IE
browser.
2. Open www.google.com in any Internet Explorer Browser as shown in the screenshot
3. Press 'F12' key on your keyboard
4. Ensure that 'IE Developer Tools' are displayed in the IE browser window as shown below
5. At this point I would like to say that 'IE Developers Tools' shown above are used to
identify the locators while using the IE Browser
Chrome Browser -
1. Do we need to install anything in Chrome Browser? The Answer is No. 'Google Chrome
Developer Tools' are inbuilt in the Chrome Browser
2. Open www.google.com in the chrome browser
3. Press 'F12' on your keyboard
4. Ensure that 'Google Chrome Developer Tools' are displayed in the Chrome Browser as
shown below
As we all know, the first thing to start with is to find the HTML element in DOM (Document
Object Model), for which we need to grab the locator. We can follow the following steps to
identify the web element in DOM on a web browser:
1. DOM can be accessed in Google Chrome either by pressing F12 or by right click on the web
page and then by selecting Inspect (as shown in the screenshot below).
2. Once we click on the "Inspect option", it will open the Developer Tools console, as shown
below. By default, it will open the "Elements" tab, which represents the
complete DOM structure of the web page. Now, if we hover the mouse pointer over
the HTML tags in the DOM, it will highlight the corresponding elements it represents on the
webpage.
3. Now, the main point is, how do we find the web element in the DOM. Click on the "Mouse
Icon" arrow (as designated by Marke 1 in below screenshot) and then select the web element on
the web page. It will automatically highlight the corresponding HTML element in the DOM.
Suppose we want to find the HTML elements corresponding to the banner image (as shown
below by marker 3). When we select the mouse point and click on the banner image, it will
automatically highlight the corresponding HTML element, as shown by marker 2, in the below
screenshot:
So, this way, we can easily search the HTML element in the DOM corresponding to a web
element on the webpage.
To access all these locators, Selenium provides the “By” class, which helps in locating elements
within the DOM. It offers several different methods (some of which are in the image below)
like className, cssSelector, id, linkText, name, partialLinkText, taName, and xpath, etc.,
which can identify the web elements based on their corresponding locator strategies.
You can quickly identify all the supported locators by Selenium by browsing all the visible
methods on the "By" class, as shown below:
"ID" as a locator is one of the most common ways of identifying elements on a web page.
According to W3C, an ID for a web element always needs to be unique. The ID is one of the
fastest and unique ways of locating web elements and is considered as one of the most reliable
methods for determining an element. But with the advancement in technologies and more of
the dynamic web pages, "IDs" are generated dynamically and generally not the reliable way to
locate a web element, as they change for different users.
Let's understand the usage of the "id" attribute for locating a web element with the help of an
example.
Suppose we have the locate the text box of First Name on the web page
"https://demoqa.com/automation-practice-form". When we inspect it in DOM, we see the
following DOM structure:
As we can see, the "Input" HTML tag has the following properties and attributes:
By.id("firstName");
How to locate a web element by using the "name" attribute?
Similar to the id attribute, Selenium also offers users a way to identify the element using
the name attribute. But contrary to id, a web page can have multiple elements with the same
“name” attribute. So, if we are going to use the name attribute for the identification of a web
element, we should always make sure that the name attribute must contain a unique value. If
multiple elements have the same value of the ‘name’ attribute, then, Selenium will just select the
first values in the page which matches the search criteria. So, we always recommend making
sure that the value of the name attribute should be unique when we select it for locating a web
element.
Let's understand the usage of the "name" attribute for locating a web element with the help of an
example.For example, suppose we need to locate the checkbox in the gender section on the web
page "https://demoqa.com/automation-practice-form", as highlighted below:
As we can see, the "Input" HTML tag representing the Male Checkbox has the following
properties and attributes:
By.name("gender");
CSS or Cascading style sheets are used extensively to style webpages and hence can be an
effective medium to locate various web elements. These days most of the web pages are
dynamically designed. Thus its quite challenging to get a unique id, name, or class to locate
element. In this type of situation, CSS selectors can be a great alternative as these are way faster
compared to another locator strategies.The basic syntax of identifying a web element
using CSS is as follows:
css=(HTML Page)[Attribute=Value]
For example, let’s say we want to find the following input textbox using CSS selector.
As we can see, the input element has the following properties and attributes:
By.cssSelector("input[id= ‘userName’]");
Similarly, we can use other attributes along with the tags to define different CSS locators.
XPath uses the XML expression to locate an element on the webpage. Similar to CSS selectors,
Xpath is quite useful in locating dynamic elements on a webpage. Xpath can access any element
present in the webpage even when they have dynamic properties.
The basic syntax of identifying a web element using the XPath locator strategy is as follows:
//tag_name[@attribute_value]
Where the tag_name is the name of the tag in the DOM structure, and the attribute is an
attribute of the target element that can identify the web element uniquely.
By.xpath("//input[@id='userName']");
Here we have used the input tag and id attribute to identify the element.
package TestPackage;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium {
public static void main(String[] args) {
String exePath = "C:\\Selenium\\chromedriver\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
driver.get("https:\\demoqa.com\\");
/**Locate by ID Attribute
* URL - https://demoqa.com/automation-practice-form
*/
driver.get("https://demoqa.com/automation-practice-form");
driver.findElement(By.id("firstName"));
/**
* Locate by Name attribute
* URL - https://demoqa.com/automation-practice-form
*/
driver.get("https://demoqa.com/automation-practice-form");
driver.findElement(By.name("gender"));
/**
* Locate by cssSelector attribute
* URL - https://demoqa.com/text-box
*/
driver.get("https://demoqa.com/text-box");
driver.findElement(By.cssSelector("input[id= ‘userName’]"));
/**
* Locate by xpath attribute
* URL - https://demoqa.com/text-box
*/
driver.get("https://demoqa.com/text-box");
driver.findElement(By.xpath("//input[@id='userName']"));
}}
Now, the interaction with any web application requires the Selenium driver to identify web
elements on the page. Until and unless an element gets correctly identified, it's impossible to
trigger events like click, enter, etc. on any web element. Now to search the web elements,
Selenium provides two methods:
findElement: find and return a single web element based on the search criteria of the
locator.
findElements: find and return all the web elements based on the search criteria of the
locator.
Both of these methods accept the object of the "By" class and then return the corresponding web
element. We have used the findElement of the Selenium WebDriver to search the various web
elements and have passed different types of locators by invoking the various method of the "By"
class.
Selenium Grid
Selenium Grid speed up the execution process when there are huge number of
tests. i.e. Huge list of tests will be distributed across multiple machines and
executed at the same time for reducing the execution time. For example, if one
test is executing on machine 1, at the same time another test will get executed
on machine 2.
Example: Lets say, we have 10 tests where each test takes 1 minute each. If you
are not using Selenium Grid, then one test will be executed after another in the
same machine and hence takes 10 minutes of time for executing 10 tests. But if
we use Selenium Grid to distribute these tests across 5 machines, then 5 tests
will be executed at the same time and hence it takes only 2 minutes for executing
all the 10 tests.
Using Hub and Nodes concept, Selenium Grid will distribute the tests across
multiple machines, operating systems and browsers.
Selenium Grid uses Hub as the central machine and uses it to distribute the tests
for execution across multiple machines which are knows as Nodes.
The below diagram depicts how Selenium Grid uses Hub and Nodes concept to
distribute the tests across multiple machines, OS and browsers:
Versions of Selenium Grid:
Selenium Grid has the two versions i.e. Selenium Grid 1 and Selenium Grid 2.
Selenium Grid 1 can be used with the outdated Selenium RC and hence Selenium Grid
1 can be ignored.
Selenium Grid 2 can be used with the latest Selenium WebDriver and hence it is the
latest Selenium Grid version used in the market.
But Selenium RC is an outdated now, almost all projects now moved to a new API
known as WebDriver.
Server is required for executing the automation scripts
Selenium RC Server acts as the middle man between ‘Selenium RC Automation Scripts’ and
the Browser as explained below:
Need to launch ‘Selenium RC Server’, as Selenium RC Server acts as the middle man between
‘Selenium RC Automation Scripts’ and the Browser. On executing the ‘Selenium RC Automation
Scripts’, ’Selenium RC Server’ first injects a JavaScript program called Selenium Core into the
browser. Once the Selenium Core is injected into the Browser by ‘Selenium RC Server’,
Selenium Core injected into the browser will start receiving instructions by ‘Selenium RC Server’
from your test program. On receiving the instructions, Selenium Core will execute the
instructions on the residing browser using the JavaScript commands. The browser will accept
the instructions from Selenium core and the response of the executed instructions will be
received by Selenium RC Server. Selenium RC Server will receive the responses of the browser
and then displays the results. Selenium RC Server will then fetch the next instructions from the
Automation scripts and repeats the same cycle.
Selenium is not an independent tool; rather, it is a collection of tools that make the Selenium suite, which
was created when two projects Selenium RC and WebDriver were merged.
WebDriver was integrated with Selenium RC to overcome a few of the limitations of Selenium RC and
has now become the de-facto for Web automation. You must be aware that after Selenium 2, there are
new versions have been released. By now in Jan 2021 they have reached Selenium 4.
Feature* Description
Dynamic Web Selenium WebDriver can automate dynamic websites where the content of pages
Page Automation changes by user actions.
Works Close to Browser vendors ship their WebDriver implementation. Hence they are tightly
Browser coupled to the browser giving a better testing experience.
Selenium WebDriver allows you to automate the test cases for all the web
Technology
applications, irrespective of the technology used for the development of the
Agnostic
application under test.
Selenium WebDriver allows QA to mimic user actions on the websites. Selenium
Mimics Real User WebDriver can mimic typical user actions like form filling, clicking, double-clicking,
key-press, as well as advanced user actions like drag and drop, click and hold, etc.
Selenium WebDriver has the most significant advantage when doing cross-browser
Supports Cross testing - where a QA can test for the same website, using the same piece of code on
Browser Testing different browsers. It enables the verification and validation of test cases on multiple
sets of browsers at the same time.
Supports parallel If there are more scripts to be executed on multiple browsers, then doing them one by
Feature* Description
one is time-consuming. So Selenium WebDriver allows parallel execution, using
Execution frameworks like TestNG, so that execution of test cases is faster. This allows large
scale execution of test cases in a short time.
Selenium WebDriver allows a QA to view the live execution of the automated test run
View Execution
on the computer system as well as on any other CI/CD pipeline machine by supporting
Results
functionalities like the screenshot, video-recording of test cases, etc.
Supports modern Selenium WebDriver integrates very well with modern Software Development
development principles like Behaviour Driven Development via integration with the Cucumber
techniques library.
Apart from the above-mentioned capabilities, WebDriver, being part of the Selenium family, also
encompassed some of the unique characteristics, which adds to its popularity as a web automation tool. A
few of those characteristics are:
Multi-Browser Compatibility - One of the prime reasons for the popularity of Selenium and
WebDriver is its cross-browser support using the same piece of code. It gives the ability to run a
specific piece of code that mimics a real-world user using a browser's native support to hit direct
API calls without the need for any middleware software or device. The below shows a sample list
of browsers supported:
Multi-Language Support - Not all testers are well versed with a particular language. Since
Selenium provides support for many languages so a tester can use any language out of the
supported languages and then use WebDriver for automation. This gives the freedom to write
code in the language people are comfortable with.
Faster Execution - Unlike Selenium RC, WebDriver doesn't depend on a middleware server to
communicate with the browser. WebDriver directs communications with browsers using a
defined protocol (JSON Wire), which enables it to communicate faster than most Selenium tools.
Also since JSON Wire itself uses JSON, which is very lightweight, so the amount of data transfer
per call is very minimum. The below figure shows clearly how the WebDriver communicates
with the Browser:
Locating Web Elements - In order to perform actions like Click, Type, Drag, and Drop we first
need to identify on which web element (like button, checkbox, drop-down, textarea) we need to
perform an action. To facilitate this, WebDriver has provided methods to identify web elements
using various HTML attributes - like id, name, class, CSS, tag name, XPath, linktext etc.
Handling dynamic web elements - There are times when there are web elements on-page, which
change with every reload of the page. Since the HTML attributes change, it becomes a challenge
to identify these elements. Selenium provides multiple methods to handle these situations -
o Absolute Xpath - this contains the complete XML path of the element in question.
o Contains( ) - using these functional elements can be searched with partial or full text and
can be used to handle dynamic elements.
o Starts-With( ) - this function is based on finding elements using starting text of the
attribute under question.
Handling Waiting for Elements - Not all the pages have the same structure. Some are
lightweight, while some have a considerable amount of data handling or AJAX calls. Many times
the web elements take some time to load. To account for this WebDriver has provided multiple
waiting mechanisms that can be used to pause the script execution for a required amount of time
based on certain conditions and then continue once the condition is full-filled. The following
figure shows a sample list that shows the capabilities of WebDriver which helps in handling the
dynamic behavior of web pages.
Being a part of the overall component system, we deduce that the Selenium WebDriver is not a
standalone testing tool. It comprises various components that are required to run tests. These are the
architectural components of Selenium.
This image tells us about the core selenium webdriver architecture and the major selenium
components which comprise WebDriver.
Selenium WebDriver Client Libraries / Language Bindings – Software Testers want to select
languages that they are comfortable with. Since WebDriver Architecture supports different
languages, so there are bindings available for a range of languages like Java, C#, Python, Ruby,
PHP, etc
JSON WIRE PROTOCOL - As per the Selenium Architecture above, the JSON Wire Protocol
facilitates all the communication that is happening in Selenium between the browser and the
code. This is the heart of Selenium. JSON Wire Protocol provides a medium for data transfer
using a RESTful (Representational State Transfer) API which provides a transport mechanism
and defines a RESTful web service using JSON over HTTP.
Browser Drivers – Since there are various browsers that are supported by Selenium, each
browser has its own implementation of the W3C standard that Selenium provides. As such
browser-specific binaries are available that are specific to the browser and hides the
implementation logic from the end-user. JSONWire protocol establishes a connection between
the browser binaries and the client libraries.
Browsers – Selenium will be only able to run tests on the browsers if they are locally installed,
either on the local machine or on the server machines.
When a user writes a WebDriver code in Selenium and executes it, the following actions happen in the
background –
An HTTP request generates, and it goes to the respective browser driver (Chrome, IE, Firefox).
There is an individual request for each Selenium command.
The browser driver receives the request through an HTTP server.
The HTTP server decides which actions/instructions need to execute on the browser.
The browser executes the instructions/steps as decided above.
The HTTP server then receives the execution status and then sends back the status to an
automation script, which then shows the result ( as passed or an exception or error).
Basic Steps in a Selenium WebDriver Script
Create a WebDriver instance.
Navigate to a webpage.
Locate a web element on the webpage via locators in selenium.
Perform one or more user actions on the element.
Preload the expected output/browser response to the action.
Run test.
Record results and compare results from them to the expected output.
Test commands are converted into an HTTP request by the JSON wire protocol.
Before executing any test cases, every browser has its own driver, which initializes the server.
The browser then starts receiving the request through its driver.
Let’s take an example with the code snippet below:
WebDriver driver = new ChromeDriver ();
As soon as you complete writing your code, execute the program. The above code will result in the
launching of the Chrome browser which will navigate to the BrowserStack website.
Now let us understand what goes behind the scene when you click on Run until the launching of the
Chrome Browser.
Once the program is executed, every line of code/script will get transformed into a URL. The JSON Wire
protocol over HTTP makes this possible. Then this URL is passed to the browser drivers (in our example,
the ChromeDriver). At this point, our client library (Python in our example) translates the code into JSON
format and interacts with the ChromeDriver.
The URL after JSON conversion looks as follows:
https://localhost:8080/{"url":https://www.browserstack.com"}
To receive the HTTP requests, every Browser Driver uses an HTTP server. Once the browser driver
receives the URL, it processes the request by passing it to the real browser over HTTP. And then, all your
commands in the Selenium scripts will be executed.
Run Selenium Tests for Free
Types of Requests:There are two types of requests you might be familiar with – GET and POST.
If it’s a GET request then it results in a response that will be generated at the browser end and it will be
sent over HTTP to the browser driver and eventually, the browser driver with the help of JSON wire
protocol sends it to the UI (Eclipse IDE).
Writing First Selenium Test Case:Before start writing the first Selenium, we need to fulfill a few
pre-requisites as mentioned below:
Once done with the above steps, you are now all set to write your First Selenium Test Case. Suppose we
take the next user journey for automation using Selenium WebDriver:
Note: We will use the login username - 'testuser' and password- 'Password@123' for our test script.
Additionally, you can create a user for yourself if need be.
Let us now begin with the step by step journey of writing our first Selenium test case.
The first step for automating any test case using Selenium WebDriver is "Open a Browser".
The Selenium WebDriver provides multiple drivers for different browsers like ChromeDriver for Chrome
browser, InternetExplorerDriver for IE, GeckoDriver for Firefox browser. Consequently, depending on
your requirement, you can choose the desired driver and instantiate the WebDriver for the specified
browser.
As we know that WebDriver is an interface which can instantiate using the driver class of any of the
browser. A few of the samples for instantiating the WebDriver using Firefox and Chrome driver is as
shown below:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver", "<Gecko Driver executable location on your system>");
WebDriver driver = new FirefoxDriver();
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
System.setProperty("webdriver.chrome.driver", "Chrome Driver executable location on your system");
WebDriver driver = new ChromeDriver();
Subsequently, let's understand the details of the above code snippets:
Import WebDriver packages:
As we can see, both the above code snippets start with two import statements. Java import
statements allow access to classes/methods that we declare in other packages. We can use
the import keyword to import the built-in and user-defined packages in our java source file. Additionally,
in doing so, our class can refer to a class that is in another package by directly using its name.
You can see that the first import statement is common for all the three examples, i.e.,
The above line of code would instantiate a Chrome browser in safe mode, with no plugins and
extensions.Once we have created the object of WebDriver corresponding to a specific browser, the next
step is to open our test application in the browser. Subsequently, let's see how we can achieve the same
using Selenium WebDriver:
Once the WebDriver instantiates, we will navigate to the desired web page. There are two methods
in Selenium WebDriver using which you can navigate to a particular web page.
1. driver.get("URL") - Navigates to the URL passed as an argument and waits till the page loads
2. driver.navigate().to("URL") - Navigates to the URL passed as an argument and didn't wait for the
page load. Additionally, it maintains browser history to navigate forward and backward.
You can use either of these two methods, but generally, the get() method is preferred since it would halt
any further action until the page fully loads. We will be using the get() method in our examples.
Moreover, the code for it would look like below-
driver.get("https://demoqa.com/login");
The driver variable would invoke the get() method to navigate to the website URL, which passes as a
String argument.
Generally, when Selenium WebDriver opens the browser, it doesn't open the browser in a full window,
which is how users use any browser. So, even in test automation, we should open the browser in full size.
Let's see how we can achieve the same using Selenium WebDriver:
driver.manage().window().maximize();
Where the manage() method returns an instance of the Options interface, now this Options interface has
a window method that returns Window type. We then use the maximize method of the Window
interface to maximize the browser window.
The next step towards writing the automation scripts is locating the web elements we want to interact
with. Selenium WebDriver provides various locator strategies using which it can locate various web
elements on a page. The "By" provides all these locators. Class of Selenium WebDriver. For our example
scenario, we have used the XPath locator strategy for locating the userName, password, and login button
elements, as shown below:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By; - It references the By class through which we call the locator
type.
import org.openqa.selenium.WebElement; - It references the WebElement class that helps
instantiate a new web element.
Now that we have located the web elements, let's see how we can perform various actions, such as type,
click, etc., on these web elements:
After we have located the web elements and stored them as WebElement instances, we will be performing
actions on them like entering text for the user name and password and clicking on the login
button. Selenium WebDriver provides multiple methods to perform actions on various web elements. A
few of those methods exposed by the WebElement interface are:
For example, we can use the methods as mentioned above, to enter the user name and password, as shown
below:
uName.sendKeys("testuser");
pswd.sendKeys("Password@123");
loginBtn.click();
Once we perform the corresponding actions on the web elements, the next step should be to perform
validations on certain web elements, which will ensure that the action which we performed execute
successfully and result in the needed state of the element.
For Example, for the test user journey, once we will be able to log in to the ToolsQA Demo Site
successfully, the logout button should be visible. Let's see how we can validate the visibility of the logout
button and click on the logout button if it is visible:
try {
if(logoutBtn.isDisplayed()){
logoutBtn.click();
System.out.println("LogOut Successful!");
}
}
catch (Exception e) {
System.out.println("Incorrect login....");
}
Where,
Note: You can refer to the article on Exception Handling for understanding more about this.
Now that the actions and validations needed for the test script are complete, the final step will be to close
the browser. Let's see how we can close a browser using Selenium WebDriver:
The last step that marks the closing of your test script is the closing of the browser. You can close the
browser using any of the following methods provided by Selenium WebDriver:
The below code is used for the driver to call the quit method.:driver.quit();
Now, we can club all the steps as mentioned above to create the complete test script. The consolidated
code would look like below:
package firstPackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
//Maximizing window
driver.manage().window().maximize();
In Selenium RC we have to use Selenium RC Server as mediator between the Automation Code and the
Browser. Generally Browsers wont allow any external code to get executed on any Website they are
holding. Since our Selenium Automation code is treated as external code by the Browsers, we required a
kind of mediator who can communicate with the browsers, so that the browser can allow the execution of
Selenium Automation code on the website they are holding. Hence Selenium RC Server acts as a proxy to
allow the Selenium Automation Code to get executed on the Application Under Test in Browsers. So in
Selenium RC we have to make Selenium command calls to the browsers via Selenium RC Server.
But in Selenium WebDriver, we don't require any Server which acts as a mediator. We can make direct
Selenium command calls to the browsers.This is possible because of the Browser drivers used in the
Selenium Automation code. There is a separate driver for each browser. Suppose if we want to run our
Selenium Automation code on Internet Explorer browser, we have to the use the Internet Explorer
WebDriver. Here WebDriver of browser will communicate with the browser. We use WebDriver API
(Application Programming Interface) code in the our Selenium Automation Code and hence our Final
Automation Tests containing the WebDriver API code can directly communicate with the browsers.
If your browser and automation tests will all run on the same machine, then your tests only use the
WebDriver API, you do not need to run the Selenium-Server ( i.e. WebDriver will run the browser
directly).
The following are the differences between the Selenium RC and Selenium Web Driver:
Difference 1:
Selenium RC
Selenium Server is required to start.
Explanation:
Selenium RC don't call the browsers directly instead it uses Selenium Server to communicate
with the Test Browsers on the same machine or different machine. So we've to start the Selenium
Server before running the Selenium RC tests.
How Selenium RC uses Selenium Server ?
Selenium RC works the same way for each supported browser by injecting the JavaScript
functions into the browser using Selenium Server when the browser is loaded and also drives the
application under test in the browser using Selenium Server (Selenium Server uses Java Script to
driver the application under test in the browser).
Drawbacks:
Selenium RC server doesn't use the Browser's built in support for test automation. Hence it has to
use Selenium Server as a mediator.
Selenium Web Driver
Selenium Server is not required to start.
Explanation:
Selenium Web Driver makes direct calls to the Test Browsers on the same machine without using
Selenium Server as a mediator.
Exception Case:
Selenium Server will be used as a mediator by Selenium Web Driver when the Selenium
Automation tests need be run on a different machine (i.e. While implementing Selenium Grid
concept which distributes the Automation tests over multiple machines. This concept may require
the Selenium WebDriver to communicate with the test browsers not on the current machine.
Hence it uses Selenium Server to communicate with all the test browsers which are not on the
current machine.)
How can the Selenium Web Driver make direct calls to the test browsers ?
Selenium Web Driver uses Browser's native support/built in support for test automation. i.e.
While writing the Selenium Web Driver automation tests we also use driver code of the browser
to be tested. Suppose if we have to run our tests on Firefox Browser, then we will use Firefox
Driver code in our Selenium WebDriver automation tests. (Browser Drivers will be explained in
the upcoming posts). Hence usage of Selenium Server is not required when the Automation tests
and the test browsers are on the same machine.
Difference 2: Selenium RC code looks different from the Selenium WebDriver code (Selenium
RC uses different commands to perform the operations when compared to Selenium Web
Driver )
Difference 3: Difference 4:
Selenium RC Selenium RC
Simple and compact API when compared to Less object oriented API.
Selenium Web Driver API. Selenium Web Driver
Selenium Web Driver Complete Object Oriented API.
Complex and a bit large API when compared to
Selenium RC API.
Difference 5: Difference 6:
Selenium RC
Selenium RC
Cannot automate to move the mouse cursor using
Cannot test iPhone/Android applications
Selenium RC automation
Selenium Web Driver
Selenium Web Driver
Can test iPhone/Android applications.
Can automate to move the mouse cursor using
Selenium WebDriver automation.
Example: TestNG unit testing framework is compatible with Java programming language and NUnit unit
testing framework is compatible with C# programming language.
Purpose of Unit Testing Frameworks in Selenium:
Before understanding how Unit Testing Frameworks are used in Selenium, we have to understand that
Unit testing is generally done by developers. i.e. Developer break the complete application code into
smaller units and test all the units separately. This process of breaking the code into smaller units and
testing is known as Unit Testing. Unit Testing can be performed in both manual and by automation.
In order to automate this unit testing process, developer use different Unit Testing Frameworks based
on the programming languages they have used. i.e. While the developer continue developing the
application code by simultaneously creating unit test for the developed code, the existing unit tests will
be automatically run by the Unit Testing Framework on the earlier developed code, to check the newly
developed code is not breaking the earlier developed code. As this is done automatically by the Unit
Testing frameworks, developers dont have to waste their time in manually testing the units tests on
earlier written code, each time they develop new code
Unit Testing Frameworks can be used in Selenium to automate/support/perform any of the below:
Controls the flow of test case execution
Grouping the test cases in to separate groups is possible
Prioritizing the test cases is possible to prioritize which test needs to be executed first and which
next.
Parameterizing the tests in such a way that the same tests can run multiple times using the
different sets of data
Supports reading the data from the external sources like Excel files etc.
Parallel execution of tests to save time of test execution by executing multiple tests at the same
time
Supports generating text logs to later find-out what are the different things that happened while
the tests were executing
Supports generating reports to find out the test results after tests execution
The below diagram, depicts the purpose of Unit Testing Frameworks in Selenium:
The below are the different Unit Testing frameworks that are supported by Selenium and categorized
according to their compatible Programming languages.
The below diagram depicts the above categorisation of Unit Testing frameworks according to different
programming languages:
TestNG Tutorial: Introduction To
TestNG Framework
Introduction To Automation
Framework
A framework is a set of rules or guidelines like Coding standards, Test data handling,
etc. that the testing world follows in a systematic manner during Automation scripting to
achieve the best output or results. It comprises of best practices and tools that help the
QA professionals to test and achieve more efficient results.
Testing frameworks are an essential part of any successful automation testing process
as they help in reducing the Maintenance costs, Testing efforts, Code reusability and
many other things to achieve. Here we can achieve the best of Return on Investment
(ROI) by showcasing it to the client along with the benefits that we are achieving.
What Is TestNG?
It is a Test Automation Framework for Java, inspired by JUnit and NUnit with new
introductory functionalities that make it more powerful and easier to use. NG means
Next Generation. It is an open-source and free tool.