
Quick Contact
Selenium Tutorial
- Before You Start
- Introduction to Selenium
- Setup Eclipse
- WebDriver
- Browser and Navigation Commands
- Locators
- Element Identification
- Tables, Checkboxes and Radio Buttons
- Selenium Waits, Alerts and Switch Windows
- Action Class
- TestNG Framework
- Introduction to Selenium
- Database Connections
- Data Driven Automation Framework
- JUnit Java Framework
- Maven
- Page Object Model (POM)
- Miscellaneous
Download and Install Java
The following are steps to install Java SE 9.0.1 in windows environment
Step 1: Go to link: http://www.oracle.com/technetwork/java/javase/downloads/index.html. Click on Download JDK. For java latest version.
Step 2: Next,
- Accept License Agreement
- Download Java JDK according to the version (32 or 64 bit) of java for Windows.
Step 3: once the download is complete, run the exe for install JDK. Click Next
Step 4: On the Custom Setup screen, leave the installation folder as it is and click on Next button.
Step 5: Wait for few seconds for Java to install some files. After that, specify the destination folder of JRE. Leave the default location as it is and click on Next button
Step 6: JDK installation would now begin
Step 7: Once the installation is completed, the window will be shown as the below screenshot. Click on Close button to close the Window
Step 8: To verify that the installation is successful, open Java folder in Program files. New JRE & JDK folders will be there with the latest version numbers. May be the previous versions of JRE or JDK folders will also be there, so don’t worry as multiple versions of JDKs & JREs can co-exist.
Download and Start Eclipse
Eclipse is an integrated development environment (IDE) for Java and other programming languages like C, C++, PHP, and Ruby. Development environment provided by Eclipse includes the Eclipse Java development tools (JDT) for Java, Eclipse CDT for C/C++, and Eclipse PDT for PHP, among others. The steps to download Eclipse for Java Developers, extract and save it in any drive are:
Step 1: Eclipse can be downloaded from http://www.eclipse.org/downloads/. The download page lists a number of versions of eclipse.
The capabilities of each packaging of eclipse are different. Java developers typically use Eclipse Classic or Eclipse IDE for developing Java applications.
The drop down box in the right corner of the download page allows to choose the operating system (Windows, Linux and Mac) on which eclipse is to be installed. Eclipse is packaged as a zip file.
Step 2: Save the .zip file to your disk.
Step 3: After downloading the Eclipse archive, a tool (7-zip, PeaZip, IZArc) is needed to extract the contents of a zip file which will create the unzipped Eclipse folder. Open the eclipse folder.
Step 4: Eclipse IDE does not have any installer, there will be a file inside the Eclipse folder named eclipse.exe. Just double click on the file to run Eclipse.
Step 5: After eclipse has been fully installed and extracted, create a workspace folder where all the program files will be there.
Step 6: After that the Welcome to Eclipse window will be shown. After completion of installation of eclipse restart the computer so that it refreshes system memory and allows changes made by installers and uninstallers to take effect.
Download and Configure WebDriver Java Client
The steps to download Selenium WebDriver are:
Step 1: Open Selenium download section using this link
http://www.seleniumhq.org/download/
Step 2: A section called Selenium Client & WebDriver Language Bindings will be shown.There will be different download links respective to different languages such as Java, C#, Ruby. For using Java with Selenium, download the Java specific drivers. To do so, click on Download from the Java section.
Step 3: Selenium Webdriver JARs would start downloading.
Step 4: Once the file is downloaded, unzip it by using any extractor like 7-zip.
Step 5: Open the unzipped folder. It would look something like this. The JAR file names might change depending on the version. A libs folder, one or two other JAR files and some other files such as LICENSE, NOTICE will be there in the Selenium folder.
With this, the download process of the Selenium Webdriver is completed.
Setup a Project
Eclipse works with the concepts of workspaces—folders and spaces where projects can be created. A simple Java Project created by using the menu File => New => Java Project.
Fill out the basic information on the New Java Project dialog, and then click Finish to proceed.
Create Packages
In Java, a new class is needed to write test scripts and it can be club with similar classes in packages. The steps to create a package and write a sample script is as below:
Step 1: Right click on src folder. Then select New > Package
Step 2: In the New Java Package popup window, enter name as firstPackage and click on Finish button.
Step 3: The package will be displayed under src folder. Right click on the package, and then Select New > Class
Step 4: In the New Java Class popup window, write the name as FirstScript and click on Finish button
Step 5: The new class is now created and it looks as shown below. Here the script can be write.
Step 6: Inside this class, A main() method has to be create. Main method acts as a starting point for Java, and is necessary to run the script . The code for main() method is below and should be added inside the class.
public static void main(String[] args) { }
Step 7: Once the browser setup is complete, write the code snippet which launches the browser. The example below shows how the code would look like if Firefox is use as browser
Example:
package firstPackage;
import org.openqa.selenium.WebDriver;
package firstPackage; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FirstScript { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); } }
Step 8: The eclipse code should look similar to the screenshot given below. Now run the code to see if it works fine.
Step 9: To run the script, click on the run icon from the toolbar. If everything works fine, a new browser window will open.
Create a First Java Test Case
WebDriver doesn’t know how to do anything other than talk to the browser driver. As a result to execute the tests, some sort of test framework is needed. Here JUnit is used because it’s very popular and is included in Eclipse’s default installation.
Add a test case to the Eclipse project via right click => New => JUnit Test Case.
Give the test case a good name in the resulting dialog and click Finish to create the file.
A SIMPLE TEST
Below is a complete test case that starts a browser locally, executes a very simple test, then closes out the browser instance.
import static org.junit.Assert.*; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.junit.Test; public class CheckSauceLabsHomePage {
@Testpublic void site_header_is_on_home_page() { WebDriver browser; //Firefox's geckodriver requires you to specify its location. System.setProperty("webdriver.gecko.driver","/Use rs/jimholmes/Utils/geckodriver"); browser = new
FirefoxDriver(); browser.get("http://saucelabs.com"); WebElement header = browser.findElement(By.id("site- header")); assertTrue((header.isDisplayed())); browser.close(); } }
RUNNING THE TEST
Execute the test by right-clicking in the test body and select Run As => JUnit Test Case. Alternatively, use the shortcut combo Alt-Shift-X, T. The test will run and see the results in the JUnit Test Explorer pane.
Import WebDriver Source File
An example of import of webdriver source file using the structure of FirefoxDriver class is below:
Step 1: Move mouse over to the FirefoxDriver class with “Ctrl” button
The two options will be shown in the pop up. Select the “Open Declaration” option.
Step 2: Eclipse opens Class File Editor. There if no source code is attached (like in this case), will get one “Attach Source” button. Click on this button.
Step 3: One file browser pop-up window will be appear, where either source file or source folder or Work space can be select. In this case select the source file.
Step 4: Click on ‘Ok’ button.Now the source implementation for FirefoxDriver class can be seen.
Summary
- Eclipse is an integrated development environment (IDE) for Java and other programming languages like C, C++, PHP, and Ruby
- Eclipse can be downloaded from http://www.eclipse.org/downloads/
- Selenium can be downloaded from http://www.seleniumhq.org/download/
- Eclipse works with the concepts of workspaces—folders and spaces where projects can be created