SELENIUM WEBDRIVER


 

SELENIUM WEBDRIVER

In order to develop Selenium RC or WebDriver scripts, users have to ensure that

they have the initial configuration done. Setting up the environment involves the Following steps

  • Download and Install Java
  • Download and Configure Eclipse
  • Configure Firebug and Fire Path
  • Configure Selenium RC
  • Configure Selenium WebDriver

DOWNLOAD & INSTALL JAVA

We need to have JDK (Java Development Kit) installed in order to work with Selenium WebDriver/Selenium. Let us see how to download and install Java.

Step 1: Navigate to the URL: http://www.oracle.com

Step 2: Go to "Downloads" section and select "JDK Download".

Step 3: Select "Accept License Agreement" radio button.

Step 4: Select the appropriate installation. In this case, it is 'Windows 7-64' bit. Click the appropriate link and save the .exe file to your disk.

Step 5 : Run the downloaded exe file to launch the Installer wizard. Click 'Next' to continue.

Step 6 : Select the features and click 'Next'.

Step 7 : The installer is extracted and its progress is shown in the wizard.

Step 8 : The user can choose the install location and click 'Next'.

Step 9 : The installer installs the JDK and new files are copied across.

Step 10 : The Installer installs successfully and displays the same to the user.

Step 11 : To verify if the installation was successful, go to the command prompt and just type 'java' as a command. The output of the command is shown below.

If the Java installation is unsuccessful or if it had NOT been installed, it would throw an "unknown command" error.


DOWNLOAD & CONFIGURE ECLIPSE

Step 1 : Navigate to the URL: http://www.eclipse.org/downloads/ and download

Step 2 : Click the 'Download' button.

Step 3 : The download would be in a Zipped format. Unzip the contents.

Step 4 : Locate Eclipse.exe and double click on the file.

Step 5 : To configure the workspace, select the location where the development has to take place.

Step 6 : The Eclipse window opens as shown below.


CONFIGURE SELENIUM WEBDRIVER

Now let us look at how to configure Selenium WebDriver. We will understand how to develop scripts with Selenium WebDriver in later chapters, however for now, we will understand just the configuration part of it.

Step 1 : Navigate to the selenium downloads section http://www.seleniumhq.org/download/ and download Selenium WebDriver

Step 2 : The downloaded file is in Zipped format and one has to unzip the contents to map it to the project folder.

Step 3 : The Unzipped contents would be displayed as shown below. How to map it to the project folder and how to start scripting would be dealt in the WebDriver chapter.


Create java project in eclipse

Convert the jave project into a webdriver project

Download the selenium stand alone server and configer the seleniumstand aloneserver to the jave project

 

To configure  right click on the java project

 

  • Go to build path click on configure build path 
  • Click libaried tab and select class path 
  • Add external jar 
  • Select the selenium stanad server apply and close

 

Let us understand how to work with WebDriver. For demonstration, we would use http://www.calculator.net/. We will perform a "Percent Calculator" which is located under "Math Calculator". We have already downloaded the required WebDriver JAR's. Refer the chapter "Environmental Setup" for details.

 

Step 1 : Launch "Eclipse" from the Extracted Eclipse folder.

Step 2: Select the work space  by clicking bowser button.

Step 3 : Now create a 'New Project' from 'File' menu.

Step 4: Enter the Project Name and Click 'Next'.

Step 5 Go to build path click on configure build path Go to Libraries Tab and select all the JAR's that we have downloaded.

Step 6 : The Package is created

Step 7 : Now right-click on the package and select 'New' >> 'Class' to create a 'Class'.

Step 8 : Now name the class and make it the main function.  

Step 9 : The class outline is shown as below.

Step 10 : Now it is time to code

Step 11 : The output of the above script would be printed in Console.

 

To import

  • Click on the error nad select the import statement
  •  Ctrl+shift+O
  •  while typing program gt it from help menu use ctrl+space

Steps for Login Automation using Selenium WebDriver  

 

Before performing automation testing for the login functionality, there are a couple of basic steps that need to be followed for the test case to be written:  

  1. Create a Selenium WebDriver instance
  2. Configure browser if required
  3. Navigate to the required web page
  4. locate the relevant web element  

Create a Selenium WebDriver instance  

To launch the website in the desired browser, set the system properties to the path of the driver for the required browser. 

Syntax: 

System.setproperty(“webdriver.Chromedriver”,”path of the chrome driver”);

webDriver driver=new chromeDriver();

 1.  Configure the Web browser  

    Usually, the web page will be in a minimized format when the test case is run. Maximize the browser for a clear picture of the test cases executed. Use the command below to do the syntax:driver.manage().window().maximize();  

2.  Navigate to the web URL  

Open the browser with the desired URL. Use the command below to open the URL in the desired instantiated 

syntax:driver.get(“http:// demo.actitime.com/login.do”)  

3.  Element  Locating the Web

To use the command to locate the webelement

  • findElement(By.id("id"));
  •  findElement(By.name("Name"));
  • findElement(By.linkText("LinkText")); 
  • findElement(By.className("Element Class")); 
  • indElement(By.tagName("HTML Tag Name"));
  • findElement(By.partialLinkText("partialLinkText"));

  Since it return in Web element

webElement username=Driver.findElement(by.id(“username”);

username.sendkey(“userid”); 

password.sendkeys(‘’pwd’);

 login.click();

logout.click();  


LOGIN

package mypackage;


import org.openqa.selenium.By;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;


public class ActiTIMELogin {


public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://demo.actitime.com/login.do");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 

 

WebElement un=driver.findElement(By.id("username"));

un.sendKeys("admin");

                           

WebElement pwd=driver.findElement(By.name("pwd"));

pwd.sendKeys("manager");

 

WebElement login=driver.findElement(By.id("loginButtonContainer"));

login.click();

}

}

SHORTCUT

driver.findElement(By.id("username")).sendKeys("admin");

driver.findElement(By.name("pwd")).sendKeys("manager");

driver.findElement(By.id("loginButtonContainer")).click();

 


NAVIGATION 

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class NavigateTo {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://demo.actitime.com/login.do");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

driver.navigate().to("https://www.seleniumeasy.com/test/");

}

}


THREAD.SLEEP

Thread.sleep we need to specify “Expected condition”on the element to be located.

Syntax: Thread.sleep(5000);



CLEAR

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class Clear {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://demo.actitime.com/login.do");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

driver.findElement(By.id("username")).sendKeys("ramakrishna");

driver.findElement(By.id("username")).clear();

                   

driver.findElement(By.id("username")).sendKeys("admin");

driver.findElement(By.name("pwd")).sendKeys("manager");

 

driver.findElement(By.id("loginButton")).click();   

}

}

 

LOGOUT

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class Logout {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://demo.actitime.com/login.do");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

driver.findElement(By.id("username")).sendKeys("admin");

driver.findElement(By.name("pwd")).sendKeys("manager");

 

driver.findElement(By.id("loginButton")).click();   

                   

driver.findElement(By.id("logoutLink")).click();

}

}