Getting started with Selenium

Overview

Selenium is a suite of softwares’, each serves to different testing needs of an organization. It has 4 components.

  • Selenium Integrated Development Environment (IDE)
  • Selenium Remote Control (RC)
  • Selenium WebDriver
  • Selenium Grid

Introduction to Selenium Webdriver:

WebDriver is a web automation framework. It permits you to execute your tests against completely different browsers, not just Firefox (unlike Selenium IDE).

WebDriver also allows you to use a programming language for creation of your test scripts (not attainable in Selenium IDE).

  • You can now perform conditional operations like if-then-else or switch-case
  • You can also use looping like do-while.

Following programming languages are supported by WebDriver

  • Java
  • .Net
  • PHP
  • Python
  • Perl
  • Ruby

Download and Installation:

Step 1 – Install Java on your computer:

Download and install the Java Software Development Kit (JDK)

selenium-image1

selenium-image2

JDK version comes bundled with Java Runtime Environment (JRE), so you don’t have to download and install the JRE separately.

Step 2 – Install Eclipse IDE:

Download “Eclipse IDE for Java Developers”, an exe file named “eclipse-inst-win64”

selenium-image3

selenium-image4

selenium-image5

selenium-image6

selenium-image7

Step 3 – Download the Selenium Java Client Driver:

Download the Selenium Java Client Driver. Many client drivers for other languages are found there, but the one for Java should be chosen.

selenium-image8

Step 4 – Configure Eclipse IDE with WebDriver:

Create a new project by clicking on File > New > Java Project. Name the project as “demo project”.

A new pop-up window will get opened, enter details as follow :

  1. Project Name
  2. Location to save project
  3. Select an execution JRE
  4. Select layout project option
  5. Click on Finish button

selenium-image9

selenium-image10

In this step,

  1. Right-click on the newly created project and
  2. Select New > Package, and name that package as “demo package”.

selenium-image11

A pop-up window will get opened to name the package.

  1. Enter the name of the package
  2. Click on Finish button

selenium-image12

Create a new Java class under new package by right-clicking on it and then selecting- New > Class, and then name it as “MyClass”.

selenium-image13

selenium-image14

selenium-image15

Now add selenium JARs to Java Build Path
In this step,

  1. Right-click on “demo project” and select Properties.
  2. On the Properties dialog, click on “Java Build Path”.
  3. Click on the Libraries tab, and then
  4. Click on “Add External JARs button.”

When you click on “Add External JARs..” A pop-up window will get opened. Select all the JAR files you want to add.

selenium-image16

Select all jar files which are inside the lib folder.

selenium-image17

selenium-image18

Select jar files which are outside lib folder

selenium-image19

Add all the JAR files which are inside and outside the “libs” folder.

selenium-image20

How to identify Web Element:

To locate elements in Webdriver, we can use “findElement(By.locator())” method.

Locators are the HTML properties of a web element by which Selenium locates the web element on which it needs to perform the action.

selenium-image21

Selenium WebDriver Commands:

Opening a URL:-
Using Get method

  • Selenium has driver.get() method which is used for navigating to a web page by passing the string URL as parameter.
  • Syntax: driver.get(“http://google.com”);

Clicking on web element :-
The click() method in Selenium is used for performing the click operation on web elements.

Writing in a textbox :-
The sendKeys() method is used for writing in a textbox or any element of text input type.

Clearing text in a textbox :-
The clear() method is used to clear the text written in a textbox or any web element of text input type.

Fetching text written over any web element :-
We have getText() method in selenium webDriver for fetching text written over an element.

Navigating backwards in a browser :-
Selenium provides navigate().back() command for moving backwards within the browser’s history.

Navigating forward in a browser :-
Selenium provides navigate().forward() command for moving forward in a browser.

Refreshing the browser :-

  • There are multiple ways for refreshing a page in Selenium WebDriver-
  • Using driver.navigate().refresh() command
  • Using sendKeys(Keys.F5) on any textbox on the webpage
  • Using driver.get(“URL”) with current URL
  • Using driver.navigate().to(“URL”) with current URL

Closing the browser :-
Selenium has two commands for closing browsers: close() and quite(). The driver.close() command is used for closing the browser having focus. Whereas, the driver.quite command is used for closing all the browser instances open.

Basic Script:

Conclusion:
You might find script writing as lengthy as writing test cases but my friend, the scripts are reusable! You don’t need new scripts all the time, even if the version of the OS on the device changes. It allows you to redo the test exactly the same, without forgetting any steps. In the end you will have a better quality software which will be released earlier, with less problems and having less resources used. And it’s FUN after all! Happy Testing to You! 😀

Comments are closed.

Top