Selenium is an open-source tool that is used for test automation. It is licensed under Apache License 2.0. Selenium is a suite of tools that helps in automating only web applications. Selenium is an open-source and a portable automated software testing tool for testing web applications. It has capabilities to operate across different browsers and operating systems. Selenium is not just a single tool but a set of tools that helps testers to automate web-based applications more efficiently.
Install Firepath Addon for Firefox
Before explaining how to install Firepath, I would like to explain why Firepath is used in Selenium Test Automation. Firepath is used to create the CSS and XPath locators to locate the UI elements of the Web Application Under Test. (Locators concept will be explained later)
- Open Firefox browser and access https://addons.mozilla.org/en-US/firefox/addon/firepath/
- Click on ‘Add to Firefox’ Button to download the Firepath Add-on
- Click on FireBug icon on the top right side of the page
- Ensure that Firepath tab is now displayed in the FireBug interface after installing Firepath
What is Selenium WebDriver ?
- Selenium WebDriver allows you to write Automation tests in any supported programming language (say Java).
- Selenium WebDriver is also known as Selenium 2.
- Selenium WebDriver is an open source and free tool.
- Selenium WebDriver is the main Selenium Project currently (i.e. Selenium RC or Selenium 1 was the main project earlier). But Selenium 2 still runs Selenium 1’s Selenium RC interface for backwards compatibility.
- Selenium 2 can run Selenium RC command and the concept is known as Backed Selenium.
- Selenium WebDriver has support for almost all browsers
Downloading the Selenium WebDriver for JAVA
- Open http://docs.seleniumhq.org/download/ page
- Find the ‘Selenium Client & WebDriver Language Bindings’ section on the page
- In the ‘Selenium Client & WebDriver Language Bindings’ section, click on the ‘Download’ link that is related to Java Programming language
- Ensure that selenium-java-2.32.0.zip file got downloaded
- Extract the downloaded selenium-java-2.32.0.zip file
- Ensure that the zip file got extracted
- Open the extracted folder and ensure that all the files got extracted
You can use these downloaded Selenium WebDriver files for configuring the Project created in Eclipse IDE to work with Selenium WebDriver.
Set Up Selenium WebDriver with Visual Studio in C#
In this section we will learn to Set Up Selenium WebDriver with Visual Studio in C# which cover the following topics:
- How to Download Selenium WebDriver
- How to run first Selenium Test
Before moving on, let’s just first change the name of the test case, which got created by default by the Visual Studio at the time of the Project Creation. As of now, the name of the test case is Program, let’s just change this to something more meaningful, like FirstTestCase.
1) In the Solution Explorer window, which is on the right side of the Visual Studio in the above image. Right Click on the Program.cs and Select Rename.
2) Notice that the text Program.cs is selected by default, now just type the new test case name ‘FirstTestCase‘.
3) The new name will start reflecting everywhere in the project or code window.
Steps to Download Selenium WebDriver
1) Go to Tools >> Nuget Package Manager >> Manage Nuget Packages for Solution....
Note: Please select Nuget Package Manager >> Manage Nuget Packages for Solution…
2) In the Seach Box, search for Selenium.WebDriver or Selenium. This will take few seconds to populate the Selenium. Once done, just select Selenium.WebDriver and click on Install to start the installation process.
3) Once Visual Studio is finished with the successful installation of the Selenium WebDriver, it will generate the output logs.
Now that we have the Selenium DLLs referenced in our project its time to test the configuration by running a quick test.
Steps to Write the First Selenium C Sharp Test
1) At the top of your project code after the last ‘using’ namespace add the following Selenium namespaces:
2) Add the following code in your static void Main section:
IWebDriver driver = new FirefoxDriver();
driver.Url = “http://www.google.com”;
Note: We will come back to the understanding of the code later, once we start with Selenium Tutorial, but as of now just understand that the above code is creating an instance of Firefox driver and opening an URL in it.
3) Run the test by clicking on the Start button given on the top bar.
Notice that the Visual Studio started a Console application and just followed that it initiated a Firefox driver and opened the website.
Selenium Example Using JAVA
Create a Package named “firstPackage” under the ‘Selenium WebDriver’ configured project
1. Create and configure a project
2. Right click on the Project and select ‘Package’ option as shown below:
3. Ensure that ‘New Java Package’ dialog is displayed, enter “firstPackage” as Package Name into the ‘Name’ text box and click on ‘Finish’ button as shown below:
4. Ensure that the New Package got added as shown below:
Create a New Class under the above created Package
1. Right click on the newly created package and select ‘Class’ option as shown below:
2. Ensure that ‘New Java Class’ dialog is displayed, type “ImFeelingLucky” class name into the ‘Name’ text box and click on ‘Finish’ button as shown below.
3. Ensure that the New Class file named as “ImFeelingLucky’ got added under the “firstPackage” as shown below:
4. Copy/Write the source code
5. Paste the source code without removing the existing code in the newly created class file as shown below:
6. Ensure that few Eclipse Errors got displayed after pasting the source code as shown below:
Let’s Rectify the first Eclipse Error – Package error
1. As only one package declaration is allowed in a class, but after copying the source code there are now two packages as shown below:
2. As ‘Copied code’s package’ is not available in the Eclipse IDE, lets remove or comment it to resolve this error as shown below:
Rectify the second Eclipse Error – Two Class Names Error
1. A single Class File can have only one Class declared inside it, but after copying the source code there are now two class declarations with different names as shown below:
2. Remove or Comment the existing class declaration as it doesn’t contain any code or we are not going to write any code into it (as shown below):
Rectify the third kind of Eclipse Error – Class declaration name in the Class file should have the same name as the Class file Name error
1. Observe that the Class file name is different from the Class declaration name as shown below:
2. Change the Class declaration name as Class File Name as shown below:
3. Ensure that all the errors got resolved and no other errors exist in the source code.
Remove the warning Messages
1. Observe that there are three warning message highlighted in yellow color icon as shown below:
2.In this case, the warning message are displayed for the three import statements as these import statements are never used in this source code (You can know this by mouse over this warning icons) as shown below:
3. Remove or comment these import statements to resolve (as shown below):
4. Ensure that the warning messages with the import statements got resolved now.
5. Observe that there are two methods that are throwing warning messages as they are never used locally as shown below:
6. Comment these methods to resolve the warning messages as shown below:
7. After commenting the above two methods, observe that there is one more warning message displayed as shown below:
8. Comment the above displayed warning message to resolve it as shown below:
9. Now ensure that there are no more warning messages and finally select ‘Save All’ button on the Eclipse IDE to save all the changes as shown below:
Remove the selenium command, so that after running the test, the selenium server won’t close the browser on which it runs the test
1. Remove or Comment the “driver.quit( )’ in the code
Ensure compatible Firefox Browser is installed
Before Running the created Selenium RC Test we’ve to verify whether a compatible Firefox Browser is installed on your machine.
Firefox browser version you are using in your computer machine should be compatible with the Selenium WebDriver version you are using
1. View the version of the Selenium WebDriver you are using as shown below: (i.e. the version of the Selenium WebDriver we have downloaded and extracted
2. Go to http://selenium.googlecode.com/git/java/CHANGELOG and start reading from the first line to find out the first occurrence of “Firefox Version Support” text
3. If the Firefox you have installed is supported by Selenium Webdriver you installed, then you may proceed to running the tests otherwise download a compatible Firefox in order to run the Selenium tests.
Run the Created Selenium WebDriver Test
Run the Selenium WebDriver Test
1. In Eclipse IDE, right click on the ‘ImFeelingLucky’ class and select ‘Run As’ -> ‘JUnit Test’ as shown below:
2. See how our automation test runs on Firefox Browser.
Check the Results after the test run is finished
1. After the test run is finished, we’ve to find out whether the automation test run by Selenium WebDriver is passed or failed
2. In order to do that, view the Eclipse IDE after the test run and view the test results as shown below:
3. Red color bar will be displayed instead of Green color bar if the test gets failed.
That’s it!! This is how we run the Selenium WebDriver tests in Eclipse IDE.
Testmatick company has a professional team of Selenium automation specialists and great experience in the development and implementation of automated testing of web and mobile applications.