About - Selenium

Definition

Development and Versions

Pre-requisites

Languages supported

Manual vs Automation

Choosing Selenium

Selenium - Components

Selenium IDE

Selenium RC

Selenium WebDriver

Selenium Grid

QTP

Selenium vs QTP

Selenium - IDE

Features

Install and configure

Selenium - Webdriver

About WebDriver

install and configure

First test case

Web element

Element Locators

Example of locators

Chropath

Web driver commands

Eclipse and TestNG

About Eclipse

Choose and download

About TestNG

Testng installation on eclipse

Testng Annotations

Pass Fail and Skip test cases

Run test cases in batch

testng.xml - Explanation

testng grouping

Running tests parallely

dependsOnMethods

dependsOnGroups

Prioritization

Passing parameters

Data providers

Read data from excel

Ant - XSLT reports

Testng listeners

Cross browser testing

Page Object Model

Selenium - First Test Case
 
After configuring Selenium at project level, now we will see how to write a first program with Selenium WebDriver in Java inside eclipse. We have already seen how to create a java class in Eclipse in the previous topic. Walking through the process again. d) You need to create a Java Project "SeleniumDemo" in eclipse as follows:

Inside eclipse, go to File -> Java Project. Enter project name as "SeleniumDemo" and click on OK.

Right click on the src folder of project and create a package as "com.ananda.selenium.demo" click on OK

e) Right click on the above package, create a java class by clicking New -> Java Class and enter "SeleniumDemo1.Java" and click OK.





The first test case program would look as follows:

package com.ananda.selenium.demo;

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

public class SelenniumDemo1 {

public static void main(String[] args) throws InterruptedException {

WebDriver driver= new FirefoxDriver();
driver.get("http://google.com");
Thread.sleep(2000);
driver.close();

}

}
There is no firefox installed on you computer


The program should be run as Java Application as it has "main" method.

In the above example, if you try to run, and if you don't have Firefox installed on your computer the following error message will be shown in Console logs.

There is no firefox installed on you computer.


If you don't set the system properties for Driver , you will receive the the following exception

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at



Add the following code to solve the problem. Code after adding system property in the application looks like this:
package com.ananda.selenium.demo;

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

public class SelenniumDemo1 {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C://Program Files/Google/Chrome/Application/chrome.exe");
//System.setProperty("webdriver.gecko.driver", "C://Program Files\Mozilla Firefox");
WebDriver driver= new FirefoxDriver();
driver.get("http://google.com");
Thread.sleep(2000);
driver.close();

}

}


Similarly you need to change the code based on the browser you want to set up. May be for Microsoft Internet edge,opera or safari etc.


WebDriver is an interface with useful methods/commands in it, which are used to write code for working with browser operations;

Let me explain the above code:

a) WebDriver is an interface which is used to initialize Driver objects like ChromeDriver, FirefoxDriver and IE browser etc...

b) Once driver is initialized a blank browser will be opened.

c) driver.get() method takes an argument which is an URL of the website. Once this method is called browser opens a website with the URL specified.

d) Thread.sleep() method takes an argument which is a time in milliseconds use to hold or stop the process of current thread for a specified millisecond in our case its 2 seconds.

e) driver.close() method closes the browser.
Ad here

I conduct online tutions on Manual Testing, Selenium and the following. For more details contact anandaibmindia@gmail.com
Manual Testing - 1600/-           Selenium - 1600/-
           Jsp and Servlet -2000/-           Java training - 2000/-