Showing posts with label WebDriver. Show all posts
Showing posts with label WebDriver. Show all posts

Webdriver implicit and explicit wait for element locator.

Today most of web application using Ajax, When page is load some element load in a different time of interval or some time request send without page loading if the element not present then it through element not found exception or not visible element etc.. To handle this Webdriver provides two type of wait “implicit” and “explicit” wait

Implicit Wait: implicit wait provide to load DOM object for a particular of time before trying to locate element on page. Default implicit wait is 0. We need to set implicit wait once and it apply for whole life of Webdriver object. Add below line of code in test for implicit wait. However implicit wait slow down execution of your test scripts if your application responding normally.

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

Road to verify css properties value using Webdriver

To get css values of any locator, we will create java script function with the use of “getDefaultComputedStyle”function. We will execute java script function using webdriver and fetch css properties value.
getDefaultComputedStyle () gives  default computed value of all css properties of an element.
Here I have created a sample example in which I am verifying Google home page menu bar css properties color, height and width.

package com.test;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class WebdriverCSSValue {

   private WebDriver driver;
   private String baseUrl;

   @BeforeSuite
   public void setUp() throws Exception {
         driver = new FirefoxDriver();                    
         baseUrl = "https://www.google.co.in/";
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
   }

   @Test
   public void testCSSvalueVerifying() {
         driver.get(baseUrl + "/");
              
         // js scripts
         String js = "return window.document.defaultView.getComputedStyle(" +
                                       "window.document.getElementById('gbx3'))";
         String jsColor = js+".getPropertyValue('color')";
         String jsHeight = js+".getPropertyValue('height')";
         String jswidth = js+".getPropertyValue('width')";

         // execution of js scripts to fetch css values
         JavascriptExecutor jsexecuter = (JavascriptExecutor) driver;
         String color = (String) jsexecuter.executeScript(jsColor);
         String height = (String) jsexecuter.executeScript(jsHeight);
         String width = (String) jsexecuter.executeScript(jswidth);
              
         //assertion
         Assert.assertTrue(color.equals("rgb(34, 34, 34)"));
         Assert.assertTrue(width.equals("986px"));
         Assert.assertTrue(height.equals("29px"));
              
         // print css values
         System.out.println(color);
         System.out.println(height);
         System.out.println(width);
   }
  
   @AfterSuite
   public void tearDown() throws Exception {
         driver.quit();
  }

Ruby WebDriver – procedure to create test scripts in RSpec

Setup of Ruby and Selenium 2:
1. Download ruby setup file from url http://rubyinstaller.org/downloads . Install the executable file into your system and set Ruby as Environmental variable into your system.
2. Open command prompt and check “ruby  -v “ to verify ruby installation and path setup.
3. After ruby installation run below command for selenium2 installation.
gem install selenium-webdriver
4. Run below command for Rspec.
gem install rspec
5. For more detail about RSpec go to link “http://rspec.info/

Road to integration of WebDriver test script with TestLink

TestLink Installation:  Test link installation you first need to go this link click here

TestLink Setup for Automation:
1. First you need to enable Automation (API keys) of your test link project.
Open your testlink project, click on check box of “Enable Test Automation (API keys)” and click on save button.

2. Generate Key: This key provide interface between webdriver test scripts and testlink. You must generate this  key. To generate this click on >> My Settings  menu and click on “Generate a new key” button under “API interface” section.


3. Create test link project.
4. Create test link plan and add test plan to created test link project.
5. Create test suite and test cases under created test link project.

Road to handle file download popup in webdriver using Sikuli

In this post I will show you how to integrate webdriver test script with Sikuli automation tool to handle window popup utility like file download and upload etc.
Here I take example of a file download window popup utility, which we can not handle by using webdriver functions. Some people use different-2 automation tool like AutoIt, java Robot class to handle this.
Here is an example of webdriver sikuli integration to handle below selenium jar file download window popup utility.

Some Webdriver API functions in java and its implementation

Following are the some API functions and it implementation
1. How to click on element: By using click() function we perform click operation on page element:

WebElement el  =  driver.findElement(By.id("ElementID"));
el.click();

2. How to enter text into page element: By using sendkeys() function we enter text in page element such as text field, area etc:

WebElement el  =  driver.findElement(By.id("ElementID"));
el.clear(); //use to clear input field
el. sendKeys (“User name”);

3. How to count total number rows in table:

List rows = driver.findElements(By.className("//table[@id='tableID']/tr"));
int totalRow = rows.size();


Road to handle popup window using text in Webdriver

Some times we have not title of pop window so in this case we can identified some unique text (content ) of popup window which is not in other window. Using this text we can switch to pop window. Below are the logic and implemented code.

Road to capture clip of element locator in webdriver java

In this post I am going to show you how to capture clip of page element using webdriver.
Below I have written a “CaptureElementClip.java“java webdriver test script of a google application where I capture google menu clip and save into project.

package com.webdriver.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class CaptureElementClip {

        private WebDriver driver;
        private String baseUrl;

        @BeforeSuite
        public void setUp() throws Exception {
                    driver = new FirefoxDriver();
                    baseUrl = "http://google.com";
                    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
         }

        @Test
        public void testGoogle() throws IOException {

                    //open application url
                    driver.get(baseUrl);

                    //take screen shot
                    File screen = ((TakesScreenshot) driver)
                                        .getScreenshotAs(OutputType.FILE);
                                        
                    //get webelement object of google menu locator
                    WebElement googleMenu = driver.findElement(By.id("gbz"));
                    Point point = googleMenu.getLocation();

                    //get element dimension
                    int width = googleMenu.getSize().getWidth();
                    int height = googleMenu.getSize().getHeight();
                   
                    BufferedImage img = ImageIO.read(screen);
                    BufferedImage dest = img.getSubimage(point.getX(), point.getY(), width,
                                                                                 height);
                    ImageIO.write(dest, "png", screen);
                    File file = new File("Menu.png");
                    FileUtils.copyFile(screen, file);
          }

          @AfterSuite
          public void tearDown() throws Exception {
                    driver.quit();
           }
}

After executing above test a “Menu.png” file is generated in root folder of your project.

Road to data driven testing webdriver C# with Nunit

In this post I will show you how to implement data driven testing in webdriver C# using Nunit.
I put all data in xml file and fetch data during execution time of test. For data I have created data.xml file list country.

Road to screen recording in webdriver with C#

Last couple of days, I am searching screen recording by using webdriver with C# similar to “Monte Media Library” in java and finally I got the solution. Here, I am posting my finding so that it can help others.
For screen capturing Microsoft provide” Microsoft Expression Encoder 4” which can be download and install from link “http://www.microsoft.com/en-in/download/confirmation.aspx?id=27870

Default installation directory is “C:\Program Files\Microsoft Expression”.

Steps to create and run webdriver test.

1. You need to make sure that you have setup all required dll for of webdriver and Nunit.
2. Download and install”Microsoft Expression Encoder 4” from above mentioned url.
3. Add “Microsoft.Expression.Encoder.dll” from “C:\Program Files\Microsoft Expression\Encoder 4\SDK” folder to your webdriver project.

Road to setup and execute webdriver test scripts on android emulator

Prerequisites:

Following prerequisites you need to set up before starting.
1. Android SDK must be installed and setup path in your machine and virtual device should be created. For more detail how to setup and create virtual device visit my post “Road to create virtual android device( emulator ) in windows
2. Download selenium server and “android-server-2.32.0.apk” from link “Android server” and save in your machine.
3. Launched your avd device (emulator)

Installation of Webdriver APK into emulator: 

1. Put android “android-server-2.32.0.apk” file under “platform-tools” of installed android directory.
2. Run below command on console to check available devices.
adb  devices
Like below message displayed on your console.
D:\Android\android-sdk\platform-tools>adb devices
List of devices attached
emulator-5554   device

3. Go to “platform-tools” folder of installed android directory and run below command to install android server into mentioned emulator (device) id.
adb -s {{emulator-id}} -e install -r  android-server-2.32.0.apk

exp:
adb -s emulator-5554  -e install -r  android-server-2.32.0.apk 
Android driver installed into mentioned driver and you can see into your emulator

Road to execute JavaScript in Webdriver

Some time we need to execute JavaScript function of application using webdriver or need to inject a piece of java script code to perform some action in application.

Webdriver provide a “JavascriptExecutor “class for executing java script, Here is step and code for the same.
First you need to create “JavascriptExecutor” class object.

JavascriptExecutor js = (JavascriptExecutor) driver;

Now call “executeScript()” method by passing java script code as a argument.
js .executeScript(“java script code”)

Here are some more examples.
String readyState = (String)js.executeScript("return document.readyState");

String title = (String)js.executeScript("return document.title");

String domain = (String)js.executeScript("return document.domain");

WebElement el   = (WebElement)js.executeScript("return document.getElementById('rentc');");

js.executeScript("return document.getElementById('submit').click();");

Road to switch window in webdriver

In this post I am going to show you that how to switch windows in webdriver(selenium 2). Below are some circumstances where we need to some trick to switch window.

1. Suppose you have only two windows one is parent and other is child window (opened window), your new window and parent window has similar title.

In Java:
Set windows = driver.getWindowHandles();
Iterator iter = windows.iterator();                                    
String parented  = iter.next();
driver.switchTo().window(iter.next());

In C#
String parentId = driver.WindowHandles.FirstOrDefault();
String childId = driver.WindowHandles.LastOrDefault();
driver.SwitchTo().Window(childId );

In above example I store parent Id for further switch to parent window.

Road to data driven testing in webdriver java TestNg Part 1

In this post I am going to see you, how to perform data driven testing in java webdriver. In this post I read data from external excel file and use those data in test script.
For this I use java “poi” jar file to read excel file. Using poi java library I created below java class to read data from excel file and return data as a array list.

Road to execute java webdriver test scripts using maven build tool

In this post, I will explain how to execute webdriver java testNG test script using ant maven tool for this first we need to setup following in our machine.
  • Java should be installed on machine.
  • maven should be installing and setup path in system environment variable. 
Maven Project: 

Create maven project for eclipse by using post “” as mentioned in if you have not experience how to create maven eclipse project.
Create webdriver testNG test script and put into “src/test/java”  source folder. Also create testNg suite file for same test script and put into desire directory as I put into “src/test/resources” folder.
Your maven eclipse project looks like as below:


I have created a webdriver test scripts and put into “com.test.example” package under “src/test/java” source folder below is my webdriver test script “SearchCountry.java”.
package com.test.example;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class SearchCountry {
                   
                     private WebDriver driver;
                     private String baseUrl;
                     
                      @BeforeSuite
                      public void setUp() throws Exception {
                        driver = new FirefoxDriver();
                        baseUrl = "http://www.wikipedia.org/";
                        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                      }

                      @Test
                      public void testSearchCountry() throws Exception {
                        driver.get(baseUrl + "/");
                        driver.findElement(By.xpath("//a[contains(@title, \"English — Wikipedia\")]")).click();
                        driver.findElement(By.id("searchInput")).clear();
                        driver.findElement(By.id("searchInput")).sendKeys("India");
                        driver.findElement(By.id("searchButton")).click();
                        String searchedResult = driver.findElement(By.xpath("//h1[@id='firstHeading']/span"))
                                                             .getText();
                        Assert.assertTrue(searchedResult.equals("India"));
                      }

                      @AfterSuite
                      public void tearDown() throws Exception {
                        driver.quit();              
                      }
}
  
Below is testng suite “TestNGSuite.xml” file which I have created for above test script.

Road to execute java webdriver testNg suite using ant build.

In this post, I will explain how to execute webdriver java test script using ant build tool.for this first we need to setup following in our machine.
  1. Java should be installed on machine.
  2. Selenium2 jar file.
  3. TestNG jar file.
  4. Ant should be installing and setup path in system environment variable.
Webdriver Test scripts.
I have created two webdriver test scripts in java on Wikipedia web application as mentioned below.
NavigateURL.java

package com.webdriver.test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class NavigateURL {
                   
                    private WebDriver driver;
                    private String baseUrl;
                     
                    @BeforeSuite
                    public void setUp() throws Exception {
                       driver = new FirefoxDriver();
                       baseUrl = "http://www.wikipedia.org/";
                       driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                     }

                     @Test
                     public void testUntitled() throws Exception {
                       driver.get(baseUrl + "/");
                       driver.findElement(By.xpath("//a[contains(@title, \"English — Wikipedia\")]")).click();
                       driver.findElement(By.linkText("Contents")).click();
                       driver.findElement(By.cssSelector("a[title=\"Featured content – the best of Wikipedia\"]")).click();
                       driver.findElement(By.cssSelector("a[title=\"Find background information on current events\"]")).click();
                       driver.findElement(By.linkText("Random article")).click();
                     }

                     @AfterSuite
                     public void tearDown() throws Exception {
                       driver.quit();               
                     }
}
SearchCountry.java


package com.webdriver.test;

import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class SearchCountry {
                   
                    private WebDriver driver;
                     private String baseUrl;
                     
                      @BeforeSuite
                      public void setUp() throws Exception {
                        driver = new FirefoxDriver();
                        baseUrl = "http://www.wikipedia.org/";
                        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                      }

                      @Test
                      public void testUntitled() throws Exception {
                        driver.get(baseUrl + "/");
                        driver.findElement(By.xpath("//a[contains(@title, \"English — Wikipedia\")]")).click();
                        driver.findElement(By.id("searchInput")).clear();
                        driver.findElement(By.id("searchInput")).sendKeys("India");
                        driver.findElement(By.id("searchButton")).click();
                        String searchedResult = driver.findElement(By.xpath("//h1[@id='firstHeading']/span"))
                                                             .getText();
                        Assert.assertTrue(searchedResult.equals("India"));
                      }

                      @AfterSuite
                      public void tearDown() throws Exception {
                        driver.quit();              
                      }
}


Now we create testNg suite file as in my case, below is testng suite file created for the above test scripts