First Selenium Script

First Selenium Script

All setup and basic prerequisites are done. Now lets us start with first basic selenium script.

Guys, I have developed a test website for running your selenium scripts. Below is the test website link you can use for your selenium scripts to run:
http://www.seleniumpoint/testwebsite.php

Let us start with the first script; Register a new user on selenium point test website.

first selenium script : User registration

Selenium Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class registration {

public static void main(String[] args) {

//Set FireFox geckoDriver path
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//Initiate FireFox Browser
WebDriver d = new FirefoxDriver();

//Open WebSite
d.get("[http://www.seleniumpoint.com/testwebsite.php](http://www.seleniumpoint.com/testwebsite.php)");

//Fill Registration Form
d.findElement(By.id("name")).sendKeys("harshal shewale");
d.findElement(By.id("mobile")).sendKeys("7507083311");
d.findElement(By.id("email")).sendKeys("harshal@seleniumpoint.com");
d.findElement(By.id("password")).sendKeys("harshal");

//Select state from Drop down menu
Select select = new Select(d.findElement(By.name("ustate")));
select.selectByVisibleText("Karnataka");

d.findElement(By.name("termsCheck")).click();

//Click On Submit button
d.findElement(By.id("nsubmit")).click();


}

}

Selenium Scripts with All browsers

Run Selenium WebDriver scripts on all browsers

Run Selenium WebDriver scripts on all browsers

Multi browser testing is always important. Accordingly, we should know how to run selenium scripts on all supported browsers,solet know how selenium works with different browsers.
Selenium requires external browser drivers specific to browser to run the scripts.
For example,

  • For Firefox:GeckoDriver is required.
  • For Chrome: ChromeDriver is required.
  • For IE: InternetExplorerDriver is required.
  • For Microsoft Edge Browser: MicrosoftEdgeDriver is required.
  • For Safari: SafariDriver is required.

Download All drivers from bellow link:
http://www.seleniumhq.org/download/

downlaod chromedriver,geckodriver,safaridriver,iedriver for selenium webdriver

Once you download the drivers keep it at any location, you will have to add its path in selenium program.

1.Firefox Browser:

System.setProperty (“webdriver.gecko.driver”,”\path\to\geckodriver.exe”);

Example:

1
2
3
4
5
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
//create WebDriver object for FireFox
WebDriver driver = new FirefoxDriver();
//open URL
driver.get("http://www.google.com");

2.Chrome Browser:

System.setProperty (“webdriver.chrome.driver”,”\path\to\chromedriver.exe”);

Example:

1
2
3
4
5
System.setProperty("webdriver.chrome.driver","C:\\harshal personal\\selenium\\chromedriver.exe");
//create WebDriver object for Chrome
WebDriver driver=new ChromeDriver();
//open URL
driver.get("http://www.google.com");

3.Internet Explorer Browser: (For Selenium 3.0 , IE version 9 or above supported )

System.setProperty (“webdriver.ie.driver”,”\path\to\InternetExplorerDriver.exe”);

Example:

1
2
3
4
5
System.setProperty("webdriver.ie.driver", "C:\\harshal personal\\selenium\\InternetExplorerDriverr.exe");
//create WebDriver object for IE
WebDriver driver = new InternetExplorerDriver();
//open URL
driver.get("http://www.google.com");

4.Microsoft Edge Browser:

System.setProperty (“webdriver.edge.driver”,”\path\to\MicrosoftWebDriver.exe”);

Example:

1
2
3
4
5
System.setProperty("webdriver.edge.driver", "C:\\harshal personal\\selenium\\MicrosoftWebDriver.exe");
//create WebDriver object for Edge browser
WebDriver driver = new MicrosoftWebDriver();
//open URL
driver.get("http://www.google.com");

5.Safari Browser:

System.setProperty (“webdriver.safari.driver”,”\path\to\safaridriver.exe”);

Example:

1
2
3
4
5
System.setProperty("webdriver.safari.driver", "C:\\harshal personal\\selenium\\safaridriver.exe");
//create WebDriver object for safari
WebDriver driver = new SafariDriver();
//open URL
driver.get("http://www.google.com]");

Locators in Selenium WebDriver

Locators in Selenium WebDriver

To perform any action on any web element on UI or webpage, you need its unique locator. For example, if you want to click on button present on UI, you must have to find ID, Name, Xpath, CSS etc. of that particular button.

Here are the available locators frequently used:

    1. ID
    1. Name
    1. Link Text
    1. XPath
    1. CSS
    1. DOM

(It is not mandatory to present all above locators for each web element, so use whatever is present. But make sure ID should be the highest priority if it is present.

1.ID

ID is the best way to locate web element if ID is present, as ID supposed to be unique for each element, it works fast to locate element than XPath.
Look at bellow Image and HTML code and corresponding selenium code to locate element (Name text box) by using ID.

how to find element using ID in Selenium WebDriver

Selenium Code:

d.findElement(By.id(“FirstName”)).sendKeys(“harshal”);

2.Name

If there is no ID present for any element, the next attribute to look up should be name. but make sure it should be unique.

how to find element using name in Selenium WebDriver

Selenium Code:

d.findElement(By.name(“LastName”)).sendKeys(“harshal”);

This kind of locators applies only on hyperlinks. Hyperlinks has anchor tag in HTML.
This is anchor tag : a

how to find element using Link text Selenium WebDriver

Selenium Code:

d.findElement(By.linkText(“Learn more”)).click();

4.XPath

XPath means XML Path. When any web page element does not have any unique locator then we have to use XPath.
XPath can access almost any web element.

There are two types of XPath:

  • 1) Absolute XPath
  • 2) Relative XPath

Absolute XPath:

It is the direct way to find the element, hence it is fast way to find element. but the disadvantage is that if there are any changes made in the path of the element then that XPath gets failed.
It begins with the single forward slash(/) ,which means you can select the element from the root node.
Syntax:
html/head/body/table/tr/td
Look at bellow image, I have shown you absolute XPath using Fire path.

how to find element using Link text Selenium WebDriver

Relative XPath:

In Relative XPath, path starts from the middle of the HTML DOM structure. ( from the node of your choice) It starts with double slash (//).
Syntax:
//table/tbody/tr/th
Look at bellow image, I have shown you relative XPath using Fire path.

how to find element using Link text Selenium WebDriver

How to Identify XPath:

There are many ways and tools available to identify Xpath. Let see few ways to get XPath:
Identify XPath using Fire path:
In 4th tutorial above, I have mentioned how to install Fire Path
Once you install Fire bug and its add-on Fire Path, inspect any web element using Fire Bug, it will give you XPath. See below image for the same.

how to find element using Link text Selenium WebDriver

Also you can manually identify XPath, watch video for the same.

5.CSS

Compared to XPath locators, CSS locators are faster. It is observed that, In Internet Explorer browser XPath works too slow, while CSS works pretty much faster.
Syntax:
tagName[attributename=attributeValue]
input[id=FirstName]

In CSS, for ID property, we can use hash (#), so above CSS will become
input#FirstName
Also for Class, CSS use dot (.)
For example:
Input[class=fname]
Input.fname

How to set up Selenium WebDriver Environment

Step 1: Install JDK

Download and Install Java from bellow: (for selenium 3.0, make sure you are downloading JDK 8 and above)

http://www.oracle.com/technetwork/java/javase/downloads/index.html

upload successful

Step 2: Downlaod Eclipse

Download eclipse from below url:

https://eclipse.org/downloads/

upload successful

Step 3: Download Selenium 3.0 Jars

Download Selenium WebDriver jars from below url:

http://www.seleniumhq.org/download/

upload successful

Once you download jars, extract and keep at any location, you will have to export it in Eclipse with your selenium project.

Step 4: Download Mozilla Gecko Driver

To run scripts on Firefox browser, we need Mozilla Gecko driver for Selenium 3.0, (For Selenium 2.0 it is not required)

Download Mozilla GeckoDriver from below url:

http://www.seleniumhq.org/download/

upload successful

Step 5: Create first selenium project in Eclipse.

All prerequisites are done! Now lets start with first selenium script. (Open www.google.com in Firefox browser)

Create project in Eclipse

File –> New –>Java Project (Give any project name like SeleniumTutorails)

Create java class:

Right click on project name folder–>New–>class (Give any class name like openURL)

Now project is created, before to start script, we need to import selenium 3.0 jars in our project.

Import Selenium 3.0 jars in Eclipse Java Project

(Refer image bellow)

Right click on project(SeleniumTutorials)–>Properties –>Java Build path–>Libraries–>Add External Jars

Now add selenium 3.0 jars which you have downloaded and then click OK.
That is it! Selenium 3.0 is configured.

Now lets start with first script to open URL.

upload successful

Open URL in Firefox (http://www.google.com)

Open Java class which you have created, (in my case opneURL)

Copy paste bellow code: (Just change gecko driver path specific to your location )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class openURL {

public static void main(String[] args) {

//set Gecko driver for FireFox browser
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//create WebDriver object for FireFox
WebDriver driver = new FirefoxDriver();

//open URL
driver.get("http://www.seleniumpoint.com");

}

}

Introduction: IDE, RC, WebDriver

What is Selenium. ?

Selenium is a brilliant open source automation tool made available by ThoughtWorks Technologies. Simply it automates the browser.

We use Selenium to automate any kind of web application, Selenium supports various browsers as well as various programming languages.Indeed, it has limitations too, it cannot automate Windows component.

Selenium provides four packages; we can use anyone according to our requirement

  1. Selenium IDE
  2. Selenium RC
  3. Selenium WebDriver
  4. Selenium Grid
SELENIUM IDE

It is Integrated Development Environment. Basically is a play and record tool works only with Firefox browser. we can record the scripts using IDE and convert it to programing languages like Java, C#, HTML etc. Like every tool, it has limitation too.

Advantages:

Very easy to install and use, its plugin to Firefox.
No programing experience is required.
We can debug and set breakpoints as well.
Recorded script can convert to programing language and utilize it in RC and WebDriver.

Disadvantages:

Works only with Firefox browser.
No logical statements and conditions like for and while loop.
No database testing.

It cannot be used in the testing for iPhone and Android applications.
Reading from external file like excel, xml, etc. not supported.
Alerts, pop up not supported.

SELENIUM RC

To overcome selenium IDE limitations, Selenium RC was initially developed. Basically it is developed to act as a proxy between application under test and selenium scripts. With RC we can write selenium script in any programing languages like java, c#, Perl, python etc. and run over all browsers.

Selenium RC components are:
Selenium Server
Selenium Server receives Selenium commands from your test program, interprets them, and reports back to your program the results of running those tests.

Client Libraries
The client libraries provide the programming support that allows you to run Selenium commands from a program of your own design. There is a different client library for each supported language. A Selenium client library provides a programming interface (API), i.e., a set of functions, which run Selenium commands from your own program. Within each interface, there is a programming function that supports each Selenese command.

Supported API : JAVA, .Net, PHP, Python, Perl, Ruby.

rc.png
upload successful

But like selenium IDE, Selenium RC also has its limitations, every time we have to start & stop the RC server to execute scripts. As server acts as a middleware between scripts and browser, it directly impacts on execution speed to get down slow. Similarly, there are many other limitations, to overcome those limitations Selenium WebDriver came in a picture. So lets understand the advantages of WebDriver over RC,

1.With Selenium WebDriver, no need to start any server to run tests.

2.WebDriver is faster than Selenium RC since it speaks directly to the browser uses the browser's own engine to control it.

3.Selenium RC is slower since it uses a JavaScript program called Selenium Core.

4.WebDriver can support the headless HtmlUnit browser. 

5.Selenium RC cannot support the headless HtmlUnit browser. It needs a real, visible browser to operate on.

6.Web driver maintains the page load synchronization by default.

SELENIUM WEBDRIVER

Selenium WebDriver is advanced version of RC, its successor of RC actually. WebDriver does same job like RC but in a different way: instead of injecting a JavaScript code into the browser to simulate user actions, it uses the browsers native support for automation (different for each browser). We have already seen WebDriver advantages over RC above. So here onwards I will cover Web Automation using Selenium WebDriver.

WebDriver is known as SELENIUM 2.0

Stable build of Selenium 2 (WebDriver) was released in July 2011.

Now SELENIUM 2.0 also enhanced to SELENIUM 3.0 this year 2016.

While migrating from Selenium 2.0 to Selenium 3.0, we have to take care bellow things:

  1. You will need to be running Java 8 to use the Java pieces of Selenium.This is the oldest version of Java officially supported by Oracle, so hopefully you are using it already!
  2. Support for Firefox is via Mozillas geckodriver.
  3. Support for Safari is provided on macOS (Sierra or later) via Apples own safaridriver.
  4. Support for Edge is provided by MS through their webdriver server.
  5. Only versions 9 or above of IE are supported. Earlier versions may work, but are no longer supported as MS no longer supports them.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×