In this blog, we elaborate on the Selenium WebDriver. The popular tool used for automated tests, as it is an "automated" test framework. It's Open Source, and you use it with any popular programming language such as Python, Java, C#, Perl, etc. And it automates browser activities. Via this article, we explain all that you require for a know-how of the testing of web apps through Selenium WebDriver. We provide complete Selenium training for Selenium certification. Naresh I Technologies also is the number one computer training institute in Hyderabad and among the top five computer training institutes in India.
And in this Selenium Tutorial, we cover What Selenium WebDriver is and whether it's easy or tough to learn. We will then be discussing what it does and its basics. Moving forward, we will look at the Selenium RC drawback and how WebDriver originated, and then we will discuss the browser elements. We then look at how we can locate the browser elements on the web page. And we end up with a look at the operations we perform on browser elements.
This blog post gives you a complete glimpse of the essential concepts needed for selenium certification.
What Is Selenium WebDriver?
The Selenium web-based automation testing framework tests the web pages executed in different web browsers and distinct operating systems. And in fact, you are up with the freedom to write the test scripts in various programming languages like Java, Perl, Python, JavaScript, PHP, and c#. You should note that Mozilla Firefox is the Selenium WebDriver's default browser.
However, the freshers sometimes doubt as below.
Is "Selenium" easy to learn?
Selenium, in reality, is very easy to learn. And for mastering it, you need basic knowledge of any popular programming language like Java, C#, Perl, Python, or PHP. If you possess the "knowledge" of any of the above programming languages, you can easily craft the test cases. However, suppose you do not know any, you need not worry as Selenium IDE is GUI based, which you use as a tool with "huge effect."
What does Selenium software do?
Below are some of the captivating applications of the Selenium software:
Automated Testing: This is handy when we deal with the "bigger" projects where if it's not "Selenium," the tester does the manual testing for each functionality. Via the "Selenium," each of the manual tasks gets automated. And, this diminishing the burden as well as stress that's over the testers.
Cross Browsers Compatibility: The Selenium supports almost all the browsers like Mozilla Firefox, Chrome, Internet Explorer, Opera, and Safari.
Increase the Test Coverage: Through the test automation, the testing time reduces, which results in the tester getting free time for performing the testing over various test scenarios in that time.
It reduces the Test Execution Time: Via "Selenium," you execute multiple test execution side by side. And this reduces the test execution time considerably.
Supports "numerous OS": This testing tool supports various OS like Windows, UNIX, LINUX and Mac, etc. With the Selenium WebDriver, it's possible to make the test cases on the Windows OS and then execute it on the Mac OS.
What are the basics of "Selenium?"
The WebDriver came into existence since Selenium version 2.0. In Selenium v1, we had only IDE, Grid, and RC. However, the "major output" in the Selenium project was WebDriver and its inclusion as a substitute for the Selenium v2. Though, with v3, RC no more exists and has been moved to the legacy package. However, you can "download" still and work with the RC. However, you should not expect any support for it.
In few words, the various advantages that we get through the WebDriver over the RC are as below:
It supports most of the programming languages, web browsers, and operating systems.
You overcome various drawbacks of Selenium 1 such, as the file upload, pop-ups, download, and dialog barrier.
We are up with easy commands in comparison to RC and a "more powerful API."
It supports batch testing, data-driven testing, and Cross-browser testing.
However, the main drawback compared to the RC is that you cannot generate the test reports. Via the RC, you get a "report."
The test script invokes the Selenium WebDriver, and the WebDriver automates the web browsers, and we have already listed the supported programming languages and the web browsers. The test scripts are written in any one of the programming languages supported.
However, you might ask why we need Selenium WebDriver? In the upcoming section, we discuss the Selenium RC drawbacks as that is the "reason" for the emergence of the WebDriver.
The emergence of Web Driver, drawbacks of RC
You might be amazed to hear that Selenium RC was an instant blockbuster on the launch. It's because it surpassed the same-origin policy problem. It was the main problem during the testing of the web apps through the Selenium Core. However, do you understand what is the "same-origin policy?"
It's the rule that puts into work the web application security model. The web browser allows the JS codes to leverage the web page elements if both the JS and the web page hosts on the same domain.
Selenium core was a JS-based testing tool that found itself handicapped, as such since it could not test all web pages.
However. with the emergence of the Selenium RC, the testers were free from the same-origin policy issue though the question was how the RC did that? It did that through the application of another component known as the Selenium RC server. Hence, RC is a combination of the Selenium RC server and the Selenium RC client.
The Selenium RC server happens to be an HTTP proxy server designed to trick the browser realizing, selenium Core and the web application tested are on the same domain. Hence, there is no stoppage for the JS code from the assessment and test of the website.
Despite that the Selenium RC was a big hit, there were some issues. And the major one was the time consumed for test execution. The Selenium RC server is in mid of the communication between the browser and the Selenium commands. And the test executions are quite time-consuming. Other than the time factor, the RC architecture is as well a little complicated.
The architecture comprises the injection of the Selenium Core inside the web browser. The selenium core registers the instructions from the RC server and converts them to the JS command. The JS code accesses and tests the web elements.
For overcoming the above issues, the Selenium WebDriver came into existence. The WebDriver is brisker as it talks directly with the browser. And you will not find any external proxy server. You will find simplicity in architecture and the browser controlled at the OS level.
There is one more benefit we get through the WebDriver, and that's it allows the HTML unit drover testing as well, and that is a headless driver. Through the headless driver, it means that the browser comes with no GUI. The RC, however, does not support it. And that's why WebDriver is better than RC.
Before understanding the Selenium concepts, we need to have a good knowledge of Java or any of the object-oriented programming languages. The supported languages are C#, Java, PHP, and Python. And at present, the Selenium WebDriver happens to be most popular with C# and Java.
Let's now move forward and understand the browser elements in the next part of the tutorial. We explain the "elements" and how the testing occurs on these web elements.
What Are Browser Elements?
We have different components present on the web pages, and elements are such components. And the most popular "elements" you will find in the browser are:
Test boxes
Images
CTA Buttons
Hyperlinks
Images
Text area/error messages
Web table/HTML table
Frame
Radio buttons/Checkboxes
And "testing" each of the above means, we check whether they work fine and respond in the manner expected. As an example, if we test text boxes, what you "test."
We can send the text or numbers via the text boxes.
We retrieve the text sent to the text boxes etc.
When we test any image, we need to download or upload the image. Or we might want to click on the image link or retrieve the image title etc.
And like that, we can perform various operations on each of the above elements. Though only once the "elements" get located on the web pages, we perform the "operations" and begin the testing, correct? Hence, in the next section, we cover the element locator techniques.
Locating the Browser elements over the Web pages
Each of the elements over the web page has attributes or properties. And there can be more than one attribute, and the "majority" is unique for each "element." Like, you can consider the pages to have two elements: the text box and the images. And each of these elements has the Name attributes and the ID attributes. And these attribute values requires values for each of the "elements." The two "elements" hence, cannot have the same value for an attribute. However, two elements can have the same value for the class name.
In our example, the text box and the image cannot have the same value for the ID and the Name value. Though, you will find some attributes common for a group of elements on the page. You will come to know about these attributes later in this blog. However, let's list "8 attributes," which we can locate the elements. And they are the ID names, Class Name, Link Text, Tag Name, CSS, XPath, and Partial Link Text.
Since these elements are with these attributes, we denote them as the locators. And they are:
By.id
Syntax: driver.findElement(By.id("zzz"));
Similarly, we have the syntax for the locators mentioned above.
And through the syntax above, you will find why the locators are known as the inside methods. Hence, before moving further, we need to learn the "methods," the functions, and the browser commands. It's applied for performing the operations on the elements.
Operations on Browser Elements
Keep your eclipse IDE open with the installed Selenium package installed.
For starting the web page testing, we require the "opening of the browser" and then move to the web page through the URL, Use the below piece of code, where this replicate. The Firefox browser gets first initiated, and then we navigate to the NareshIT Home page.
import org.openqa.selenium.WebDriver; This is a library package for initiating the loaded a certain driver.
import org.openqa.selenium.firefox.FirefoxDriver; This is the library package which has the FirefoxDriver class required for starting the FirefoxDriver on initiation of the browser by the WebDriver class.
System.setProperty(“webdriver.gecko.driver”, “files/geckodriver.exe”);– This command issues the run time engine that the Gecko driver is located at the specified path. And since the Firefox 35, we download the Gecko driver to work with the WebDriver. In case you need to test over the Chrome, you download the ChromeDriver that is an exe file and mention the path through the lines of codes. And we have to do the same in case of each browser as well.
WebDriver driver = new FirefoxDriver(); – We initiate the firefox driver object through this command.
driver.get(“https://www.nareshIT,in/”); – We open the mentioned URL through this method.
driver.getTitle(); – Through this command, we fetch the title of the current tab.
driver.quit(); – This closes the browser driver.
However, suppose you need to navigate to another URL and then make the testing? In this case we make use of the navigate.io () as described in below snippet. And if you need to come back to the pre page. And you can come back to this through the navigate.back() command. And like that we can refresh the current page with the help of the navigate.refresh() command.
As you will find in most of the basics, let's move to the new topic. We need to find an element on the web page, and then you need to operate as possible.
Create a WordPress.com account. We will find below how we can log into WordPress bypassing all the credentials from the code.
We have the two text fields in the WordPress.com login page, one is the email, and the other is the password. We need to locate these two elements, and then pass these credentials, and then find the third element Login button that we need to click.
On inspection, you will find that (Ctrl + Shift + i) opens the same window in the browser. Now, below elements, all the "elements" on the page shows. And also shown are their attributes. Though, three portions highlights. The first highlighted element is the email text field and the second is the password text field, and the third is the Login button.
If you recall, we mentioned earlier that these elements locate through the element locator. Let's now make use of these elements and provide the field values. It's the syntax for finding the element: driver.findElement(By.id("zzz"));
For providing the values, we need to make use of the sendKeys("credentials"),
For tapping the button, we need to make use of the click () method.Let's start by finding the elements, and then we will execute the operations through it. The code happens to be as below:
In line 1, we identify the email through the name attribute and send it the EmainID.
In line 2, we find the password element through the "Name" attribute and then send it the password.
In line 3, we locate the login button element through unique ID and then tap over it through the click () method.
However, only these lines are not enough. Because of the "page dynamics," it does not respond at once, and by the time the page load, the WebDriver gets terminated and throw timeout exception error. And this issue might not occur on the WordPress.com page as it is fast, though it might happen on various E-commerce site and various other dynamic web sites.
For overcoming this issue, we need to use the advanced technique. We request the WebDriver to wait after we access the page, and once it loads fully, we locate the element. And then we perform the actions.
If the WebDriver needs to wait unless the element loads in the web page and then shut the browser, then we achieve this through the driver.wait() method or the Threads.sleep() method. Though, if you write more advanced code, then we can make use of the implicit waits or the explicit waits. However, in the current case, we use the below command:
However, while we work on the wait conditions, you need to import the below library:
We perform this as the class for the wait, and such methods are in this.
The whole code is as below:
When you will provide the credentials in the original and execute this code, the WordPress.com page will open in the new window, and after entering the credentials, it logs in to accounts.
Congratulations, we have logged in successfully, and our whole code executes correctly.
We have made use of the ID and Name attributes for the location of elements. And you can make use of the other locators like XPath. You can make use of any element locator and as a requirement.
Thus, we saw the drawbacks of the Selenium RC and why we were up with Selenium WebDriver. We saw in detail the advantages of the WebDriver over the RC. And then, we saw how we replaced the RC with the WebDriver to ensure automation of testing. Then we saw how we log in to the WordPress site using the "selenium" in Eclipse IDE. And that completed our tutorial.
You can contact Naresh I Technologies for your Selenium online training. We provide Selenium training in Hyderabad and USA, and in fact, you can contact us from any part of the world through our phone or online form on our site. Just fill it and submit it, and one of our customer care executives will be contacting you. And what else you get:
You have the freedom to choose from Selenium online training and classroom training.
Chance to study from one of the best faculties and one of the best Selenium training institutes in India
Nominal fee affordable for all
Complete training
You get training for tackling all the nitty-gritty of Selenium.
Both theoretical and practical training.
And a lot more is waiting for you.
You can contact us anytime for your Selenium training and from any part of the world. Naresh I Technologies caters to one of the best Selenium training in India.