Quantcast
Channel: Yudiz Solutions Ltd.
Viewing all articles
Browse latest Browse all 595

Getting started with Selenium

$
0
0

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.

//Clicking an element directly
	driver.findElement(By.id("button1")).click();

//Or by creating a WebElement first and then applying click() operation
	WebElement submitButton = driver.findElement(By.id("button2"));
	submitButton.click();

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

//Creating a textbox webElement
	WebElement element = driver.findElement(By.name("q"));

//Using sendKeys to write in the textbox
	element.sendKeys("ArtOfTesting!");

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.

driver.findElement(By.name("q")).clear();

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

driver.findElement(By.id("element123")).getText();

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

driver.navigate().back();

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

driver.navigate().forward();

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

//Refreshing browser using navigate().refresh()
	driver.navigate().refresh();

	//By pressing F5 key on any textbox element
	driver.findElement(By.id("id123")).sendKeys(Keys.F5);

	//By reopening the current URL using get() method
	driver.get("http"//artoftesting.com");

	//By reopening the current URL using navigate() method
	driver.navigate().to("http://www.artoftesting.com");

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.

//To close the current browser instance
driver.close();

//To close all the open browser instances
	driver.quit();

Basic Script:

package demo;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;

public class firstscript
{
	public static void main(String[] args)
	{
		 System.setProperty("webdriver.chrome.driver","/home/yudiz/eclipse-workspace/facebook/lib/chrome driver/chromedriver");
		driver = new ChromeDriver();
		driver.get("https://www.facebook.com/");
		driver.quit();
	}
}

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! 😀


Viewing all articles
Browse latest Browse all 595

Trending Articles