Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Selenium Tutorial

Author: Akshay Akshay
by Akshay Akshay
Posted: Sep 01, 2019
selenium webdriver

Introduction to SeleniumSelenium is a free (open-source) automated testing suite for web applications that supports cross-browser and cross-operating-system interoperability. It is quite similar to HP’s QuickTest Pro (QTP, now UFT), only that Selenium focuses on automating web-based applications. Testing done using the Selenium tool is usually referred to as Selenium Testing.Selenium is useful for testing web applications only. Neither desktop (software) testing nor the testing of mobile applications is possible with Selenium.

Watch this Selenium Tutorial Video: https://intellipaat.com/blog/tutorial/selenium-tutorial/

A web application is an application program stored on a remote server that is allowed to get accessed through a web browser over the Internet. Many websites contain web applications. Any website component that performs functions for users qualifies as a web application. Few examples of web applications are:Google Search EngineAliExpressComing back, the testing of web applications done through the Selenium tool is referred to as Selenium Testing.Prepare Yourself for the interview with these Selenium Interview QuestionsSelenium TypesSelenium is not just a single tool but a suite of software, each catering to different testing needs of an organization. It has four components:Selenium Integrated Development Environment (IDE)Selenium Remote Control (RC)Selenium GridSelenium WebDriverNow that you know about its types, let’s talk about each of them briefly.

For the complete Tutorial on selenium visit: https://intellipaat.com/blog/tutorial/selenium-tutorial/

Selenium IDESelenium IDE (Integrated Development Environment) is a tool that helps you develop your Selenium test cases. It’s an easy-to-use Chrome and Firefox extension and is generally the most reliable method to develop test cases. It records users’ actions in the browser for you, using the existing Selenium commands, with parameters defined by the context of the web element. This is not only a time-saver but also an excellent way of learning Selenium script syntax. Previously known as Selenium Recorder, Selenium IDE was initially created by Shinya Kasatani, of Japan and contributed to the Selenium project in 2006.

It was introduced as a Firefox plugin for faster creation of test cases. As it was a Firefox extension, it could automate the browser through a record-and-play feature providing autocompletion support and the ability to move commands around quickly.Scripts are recorded in a special test scripting language called Selenese for Selenium. Selenese comes up with commands for carrying out actions in a web browser and restoring data from the resulting pages.The advantage of Selenium IDE is that the tests recorded via the plugin can be exported in different programming languages like Java, Ruby, Python, etc.

Selenium RC (Remote Control)Let me first tell you that Selenium Core was the first version. But with that version, testers had to install both Selenium (a JavaScript program) and the web server containing the web application being tested on their local systems so that they would belong to the same domain.Then, another ThoughtWorks’ engineer, Paul Hammant decided to create a server that will act as an HTTP proxy to trick the browser into believing that Selenium Core and the web application being tested belong to the same domain, thus making RC a two-component tool.Selenium RC ServerSelenium RC Client (Library containing the programming language code)RC can support the following programming languages:JavaC#PHPPythonPerlRuby

Selenium GridSelenium Grid is a testing tool that lets you run your tests on various machines against different browsers. It is part of the Selenium suite that specializes in running multiple tests across different browsers, operating systems, and machines. You can connect to it with Selenium Remote Control by stating the browser version, browser, and operating system as per your choice. You will be able to specify these values through Selenium Remote Control capabilities. With Selenium Grid, one server makes a move as a hub. Tests communicate to the hub to get 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 parallel testing and also allows managing different browser versions and browser configurations centrally (instead of in each individual test).There are multiple online platforms that provide an online Selenium Grid that you can access to run your Selenium automation scripts. For example, you can use LambdaTest.Selenium Grid has more than 2,000 browser environments over which you can run your tests and truly automate cross-browser testing.

Selenium WebDriverFounded by Simon Stewart in 2006, ThoughtWorks consultant in Australia. Selenium WebDriver was the first cross-platform testing framework that would control the browser at the OS level. Selenium WebDriver is a successor to Selenium RC. Selenium WebDriver accepts commands (sent in Selenium or via a Client API) and sends them to a browser.This is implemented through a browser-specific driver, which sends commands to a browser and retrieves the results. Each driver launches and accesses a browser application. Different WebDrivers are:Firefox Driver (Gecko Driver)Chrome DriverInternet Explorer DriverMicroEdgeOpera DriverSafari DriverHTML Unit DriverBenefits of Selenium WebDriverSelenium WebDriver supports seven programming languages: Java, C#, PHP, Ruby, Perl, Python, and.Net.It supports cross-browser interoperability that helps you perform testing on various browsers like Firefox, Chrome, IE, Safari, etc.Tests can be performed on different operating systems: Windows, Mac, Linux, Android, and iOS.Selenium WebDriver overcomes limitations of Selenium v1 like file upload, download, pop-ups, and dialog barrierCons of Selenium WebDriverDetailed test reports cannot be generated.Testing images is not possible.No matter what, these shortcomings can be overcome by integrations with other frameworks. That is, for testing images Sikuli can be used, and for generating detailed test reports, TestNG can be used.Now, you know what Selenium is and have a fair idea about the various tools of Selenium suite.Next, I will tell you everything you need to know to get started with testing web apps using Selenium WebDriver. The below image depicts how WebDriver works:

What is Selenium WebDriver?In this part, let’s dig deeper into learning Selenium WebDriver. Let’s understand more about what Selenium WebDriver is, what browser elements are, and how to locate browser elements on a web page.Locating and testing of web elements in the web application is implemented through a browser-specific driver. It controls the browser by directly communicating with it.In Selenium WebDriver, you have the liberty to write test scripts in different programming languages like Java, Perl, Python, Ruby, C#, PHP, and JavaScript. But, make a note that Mozilla Firefox is Selenium WebDriver’s default browser.WebDriver was introduced as part of Selenium v2.0. Selenium v1.0 consisted of only IDE, RC, and Grid. But the major breakthrough in the Selenium project was when WebDriver was developed and introduced as an addition in Selenium v2. And, with the release of Selenium v3, RC has been deprecated and moved to a legacy package. Although you can still download webdriver and carry out tasks with RC, there wouldn’t be any support for it in Selenium v3.In a nutshell, the advantages WebDriver has over RC are:Support for more programming languages, operating systems, and web browsersAbility to overcome the limitations of Selenium v1Simpler commands when compared to RC, and a better APISupport for batch testing, cross-browser testing, and data-driven testingThe drawback WebDriver has when compared to RC is that test reports cannot be generated in WebDriver; whereas, RC generates detailed reports.You must have heard the term ‘browser elements’ a number of times. The next part of this Selenium tutorial will be about these elements, and you will see how testing happens on these web elements.What are Browser Elements?Browser elements are different components present on web pages. The most common elements you will notice while browsing are:Text boxesCTA buttonsImagesHyperlinksRadio buttons/CheckboxesText area/Error messagesDropdown box/List box/Combo boxWeb table/HTML tableFrameTesting these elements essentially means, you have to check whether they are working fine and responding the way you want them to. For example, if you are testing a text box, what would you test it for?1. Whether you are able to send text or numbers to the text box2. If you can retrieve text that has been passed to the text box, etc.If you are testing an image, you might want to:Download the imageUpload the imageClick on the image linkRetrieve the image title, etc.Similarly, operations can be performed on each of the elements mentioned earlier. But only after the elements are located on the web page, you can perform operations on them and start testing them.Locating Browser Elements Present on a Web PageEach element on a web page does have an attribute (property). Elements can have multiple attributes and most of these attributes will be distinctive for different elements. Consider an example, there is a page having two elements: an image and a text box. Both these elements have a ‘Name’ attribute and an ‘ID’ attribute. These attribute values need to be distinctive for each of these elements. In other words, no two elements can have the same attribute value.In the above example, the image and the text box can have neither the same ‘ID’ nor the same ‘Name’ value. However, there are some attributes that can be common for a group of elements on the page, like a group of elements can have the same value for ‘Class Name’.There are eight attributes which can be used to locate elements on a web page, they are ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS, and XPath. Since the elements are located using these attributes, they are referred to as ‘Locators’. The locators are:By.iddriver.findElement(By.id("xxx"));By.namedriver.findElement(By.name("xxx"));By.classNamedriver.findElement(By.className("xxx"));By.tagNamdriver.findElement(By.tagName("xxx"));By.linkTextdriver.findElement(By.linkText("xxx"));By.partialLinkTextdriver.findElement(By.partialLinkText("xxx"));By.cssdriver.findElement(By.css("xxx"));By.xpathdriver.findElement(By.xpath("xxx"));By looking at the syntax above, you might have realized why locators are called inside methods.Now, you need to learn all the other methods, browser commands, and functions that can be used to perform operations on the elements.But before moving on with the hands-on, creating an automated test, let’s first understand what dependencies are and how they help you in creating a Maven project.You will need certain dependencies and libraries ready with you for the Selenium project which will help you perform automated testing of a web application and such a tool is known as Maven.

What is Maven in Selenium?Maven is a build automation tool used primarily for Java projects by downloading its dependencies.Basically, Maven is a software which helps you download dependencies for a software program. When you create a Selenium project, you need to specify all Selenium components which are required to be included inside a POM file for the Selenium project to be ready. Once the dependencies are added in the POM file, you can simply save the project and all these dependencies will automatically be downloaded.Setting up Selenium with Maven and TestNG on EclipseBefore diving into this section of Selenium tutorial, let’s see how Eclipse projects run. For making Eclipse Java projects run, you need a library that gives ability to produce an HTML report of execution and display the test case that has failed, which is done by TestNG library. When bugs can be accurately located like this, they can be fixed immediately to the relief of developers.TestNG is a testing framework. It structures, groups, and launches tests. It also generates testing reports. To get a function executed, we need to include @Test annotation before the definition of that function.When you run this file as TestNG suite, the execution will start and you will get the detailed test reports. You will get the test output in your console tab and the result of the test suite in the next tab.You decided to use TestNG for several reasons:1. TestNG annotations are easy-to-create test cases.2. Test cases can be grouped and prioritized more easily.3. TestNG supports parameterization.4. It also supports data-driven testing using data providers.5. It generates HTML reports.6. Parallel test execution is possible.7. TestNG readily supports integration with other tools and plugins like Eclipse IDE and build tools like ANT, Maven, etc.Now, this tutorial will move on with the hands-on part. This section is divided into three parts:Java JDK InstallationEclipse InstallationPerforming Selenium Test CaseInstalling Java JDKStep 1: Download Java JDK from the link provided below and then click on the Oracle JDK Download buttonhttps://www.oracle.com/technetwork/java/javase/downloads/index.html

Click on the radio button, Accept License Agreement, and download the.exe file related to your OS type

Step 2: Open the downloaded execution file, and click on Next

Step 3: Select Developers Tools and click on Next, and you will be directed to the below screen. It will take a few moments to set up JDK in your system

Step 4: Click on Close to complete the set up

Before moving ahead, you need to add the environment variables to the path as follows.Step 5: Go to local C drive> Program Files> Java> jdk-12.0.1> bin

Step 6: Copy the path as shown aboveStep 7: Go to Control Panel> System and Security> System> Advanced system settings> System Properties

Step 8: Click on Environment Variables as shown above and the below window pops up in which you will perform the following steps:1. Click on Path in the System variables section, under Environment Variables2. Click on Edit

Step 9: 1. Click on Browse and find your file path as local C drive> Program Files> Java> jdk-12.0.1Keep clicking ‘OK’2. Now, you have Java JDK installed in your system

Installing EclipseStep 1: Go to the Eclipse download page (the recent one is Eclipse Installer 2019–06 R), with this link: https://www.eclipse.org/downloads/

Step 2: Click on Download 64 bit. You would land on a page as shown below:

Step 3: Once the download is finished, launch the installerStep 4: Click on Eclipse IDE for Enterprise Java Developers

Step 5: Once done, click on INSTALL

Step 6: Then, click on Launch

Now that you have successfully set up Java and Eclipse in your environment, let’s perform a Selenium Test Case using Maven.Performing Selenium Test CaseStep 1: Start a new Maven Project

Step 2: Check the Create a simple project checkbox and click on Next

Step 3: Enter Group Id and Artifact Id and click on FinishGroup Id: Group Id is the Id of the project’s group. Generally, it is unique among an organization.Artifact Id: Artifact Id is the Id of the project. It specifies the name of the project.

Step 4: Now, your project would appear in the Project Explorer section as shown below:

Before moving on to the scripting part, you need to configure the Maven Dependencies to perform Selenium Test Case in Eclipse. You will be adding the Maven Dependencies to the pom.xml file under the target folder.Step 5: Now, go ahead and add the below dependencies to the pom.xml fileNote: Copy the code from below and paste it after the junit dependency which will be already present in your.xml file org.seleniumhq.selenium selenium-java 3.141.59 org.seleniumhq.selenium selenium-server 3.141.59 org.seleniumhq.selenium selenium-api 3.141.59 org.seleniumhq.selenium selenium-chrome-driver 3.141.59

Step 6:1. Right-click on the file name2. Click on Properties

Step 7: 1. Select Java Build Path2. Click on Libraries3. Click on Add Library

Step 8: 1. Click on TestNG2. Press Next and then Finish

Step 9: Click on the Apply and Close buttonStep 10: Go to www.facebook.com, right-click on the page, and click on Inspect

Step 11: Follow the process shown below and copy the xpath of email:1. You will use this arrow to select an element in the page to inspect it2. Select the email web-element, which will point to the section of this web-element’s html code3. Click on Chropath, this helps you in getting Xpaths and CSS selectors for web elements of a web page, uniquely4. Copy the RelXpath. XPath is defined as XML path. It is a syntax or language for locating any element on the net page using XML path expression

Step 12: 1. Paste the xpath in the xpath expression (i.e., key)2. Enter the value (email ID)

Step 13: Do the same for password xpath

Step 14: Do the same for login xpath as well

Step 15: Your code would look like this:@Testpublic class App { WebDriver driver; public void test() { System.setProperty("webdriver.chrome.driver","C:\\Users\\intellipaat\\Desktop\\chromedriver.exe"); driver = new ChromeDriver(); //The website name you want to visit driver.get("http://www.facebook.com"); driver.findElement(By.xpath("//input[@id='email']")).sendKeys("helloworld@gmail.com"); driver.findElement(By.xpath("//input[@id='pass']")).sendKeys("helloworld123"); driver.findElement(By.xpath("//input[@id='u_0_2']")).click(); }}Note: Here, you must provide a valid email ID and password. In this example, dummy ID and password would be used.

Step 16: 1. Right-click on the screen and scroll down to Run As2. Click on 1 TestNG Test

Step 17: 1. Once done, Chrome browser window will pop up2. It will search for facebook.com3. Then, it will automatically type the email4. It will automatically type the password as well5. It will then click on Log in and navigate you to your FB homepage

Step 18: You’ll be navigated to your FB account

Step 19: Go to Eclipse IDE again and see the console. You would see the Test report with the results

That’s it! You have successfully performed a Selenium Test Case.Creating Automated TestsThere are three steps to execute automated tests:Find an element on the web browserPerform an action on that elementTest and create a test report with the results

Finding an Element on the Web BrowserAn element on a web browser can be found using:IDNameClass NameTag NamePerforming an Action on That ElementThe next step is performing an action. For that you can try the following options:Click(): Used to click on a web elementsendKey(): Used to take values into text boxesClear(): Used to clear text boxes of its current valueSubmit(): WebDriver will automatically trigger the submit function of the web application where that element belongs toTesting and ReportingReport generation is extremely vital once you do the Automation Testing, likewise as for Manual Testing. By looking at the result, you can easily identify how many test cases are passed, failed, and skipped. By viewing the report, you’ll come back to understand what the status of the project is.Selenium Internet Driver is employed for automating the web-application, however, it will not generate any reports. TestNG will generate the default report.

Originally published at www.intellipaat.com on August 13, 2019.

About the Author

Intellipaat In this Salesforce Tutorial for Beginners, you will learn Salesforce from scratch and will become a Salesforce Developer. Through this tutorial, you will get to know various aspects of Salesforce architecture like force website.

Rate this Article
Leave a Comment
Author Thumbnail
Please or Join to add a comment.
Author: Akshay Akshay

Akshay Akshay

India

Member since: Aug 28, 2019
Published articles: 11

Related Articles