Selenium is an open-source tool that is used for automating the tests carried out on web browsers (Web applications are tested using any web browser).
- Selenium is open-source,
- Test scripts can be written in any of these programming languages: Java, Python, C#, PHP, Ruby, Perl & .Net.
- Tests can be carried out in any of these OS: Windows, Mac or Linux.
- Tests can be carried out using any browser: Mozilla Firefox, Internet Explorer, Google Chrome, Safari or Opera.
- It can be integrated with tools such as TestNG & JUnit for managing test cases and generating reports
- It can be integrated with Maven, Jenkins & Docker to achieve Continuous Testing.
Components of Selenium.
Selenium has four major components – Selenium IDE, Selenium RC, Selenium Web driver, Selenium GRID.
Selenium IDE (Integrated Development Environment) is the major tool in the Selenium Suite. It is a complete integrated development environment (IDE) for Selenium tests. It is implemented as a Firefox Add-On and as a Chrome Extension. It allows for recording, editing, and debugging of functional tests. It was previously known as Selenium Recorder.
Selenium Remote Control (RC) is a server, written in Java, that accepts commands for the browser via HTTP. RC makes it possible to write automated tests for a web application in any programming language,which allows for better integration of Selenium in existing unit test frameworks.Now it is Depreciated.
Selenium Grid is a server that allows tests to use web browser instances running on remote machines. With Selenium Grid, one server acts as the hub. Tests contact the hub to obtain access to browser instances. The hub has a list of servers that provide access to browser instances (WebDriver nodes), and lets tests use these instances. Selenium Grid allows running tests in parallel on multiple machines and to manage different browser versions and browser configurations centrally (instead of in each individual test).
Selenium Web driver:
Selenium WebDriver is the successor to Selenium RC. Selenium WebDriver accepts commands (sent in Selenese, or via a Client API) and sends them to a browser. This is implemented through a browser-specific browser driver, which sends commands to a browser and retrieves results.
WHAT IS SELENIUM WEB DRIVER?
Selenium WebDriver is a web framework that permits you to execute cross-browser tests. This tool is used for automating web-based application testing to verify that it performs expectedly.
Selenium Framework Architecture:
1.Selenium Client library
2.JSON wire protocol over HTTP
Selenium Client library:
Selenium provides support to multiple libraries such as Ruby, Python, Java, etc as language bindings have been developed by Selenium developers to provide compatibility for multiple languages.
JSON wire protocol over HTTP:
Selenium provides drivers specific to each browser and without revealing the internal logic of browser functionality, the browser driver interacts with the respective browser by establishing a secure connection.
When a test script is executed with the help of WebDriver, the following tasks are performed in the background:
- An HTTP request is generated, and it is delivered to the browser driver for every Selenium Command
- The HTTP request is received by the driver through an HTTP server
- All the steps/instructions to be executed on the browser is decided by an HTTP server
- The HTTP server then receives the execution status and in turn sends it back to the automation scripts
As discussed earlier, Selenium provides support for multiple browsers like Chrome, Firefox, Safari, Internet Explorer etc.
Benefits of Selenium WebDriver:
- It is one of the most popular Open-Source tools and is easy to get started with for testing web-based applications. It also allows you to perform cross browser compatibility testing.
- Supports multiple operating systems like Windows, Mac, Linux, Unix, etc.
- It provides compatibility with a range of languages including Python, Java, Perl, Ruby, etc.
- Provides support for modern browsers like Chrome, Firefox, Opera, Safari, and Internet Explorer.
- Selenium WebDriver completes the execution of test scripts faster when compared to other tools
- More Concise API (Application Programming interface) than Selenium RC’s
- It also provides compatibility with iPhone Driver, HtmlUnitDriver, and Android Driver.
Basic Steps in a Selenium WebDriver Script.
To Understand the Steps of Selenium WebDriver lets Take an example of Flipkart website. Here we will log in the Flipkart using userId and password.
1.Create a WebDriver instance.
WebDriver can be instantiated as:
WebDriver driver = new ChromeDriver();
WebDriver driver = new EdgeDriver();
WebDriver driver = new FireFoxDriver();
As we have already discussed that Selenium WebDriver can work with any drivers so you can choose driver of your choice (Make sure that the version of driver and the Browser which you are currently using should be same).
2.Navigate to a webpage.
3.Locate a web element on the webpage via locators in selenium.
Locators are the way to identify the html elements on webpage.
There are various types of Selenium locators are used some are:
cssSelector(), xpath(), className(), id() etc.
4.Perform one or more user actions on the element.
As seen in the above code snippet, by using an element we are sending user id and in the second one we are sending password then clicking the login button.
5.Preload the expected output/browser response to the action.
7.Record results and compare results from them to the expected output.
This is the complete code to log into the Flipkart.
Limitations of WebDriver:
- Support for new browsers is not readily available when compared to Selenium RC.
- For the automatic generation of test results, it doesn’t have a built-in command.
How Selenium WebDriver work?
Once we are ready with your script, you will click Run to execute the program. The Chrome browser will be launched, and it will navigate to the Flipkart website.
- Once you click on run, the selenium client library will communicate with Flipkart
- Flipkart API will send the command taken from language level binding to browser driver with the help of JSON wired protocol.
- Flipkart API sends the request to Browser Driver, it may be Firefox driver, IE driver, Chrome driver.
- The browser driver will use the HTTP server for getting the HTTP request and the HTTP Server filter out all the commands which need to be executed.
- Then the commands in your selenium script will be executed on the browser.
- Finally, HTTP server sends the response back to the automation test script.