July 20, 2015

Simulate pressing of Multiple Keys (using chord)

One of the nice feature in selenium webdriver is it allows us to simulate pressing of multiple keys using a method called "chord"

Here is the explanation of chord method:

Simulate pressing many keys at once in a "chord". Takes a sequence of Keys.XXXX or strings; appends each of the values to a string, and adds the chord termination key (Keys.NULL) and returns the resultant string. 

Source:  http://seleniumhq.github.io/selenium/docs/api/java/index.html

In the below example i am going to show how to perform Copy and Paste using Selenium webdriver using Actions + Keys + chord.



Below is scenario:
1. Open https://accounts.google.com/signup
2. Type something in FirstName
3. Copy the content in FirstName field (CTRL+c)
4. Paste same thing in LastName field (CTRL+v


package sample;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Keys_Chord {
 
 public WebDriver driver;

 @Test
 public void testKeys_Chord() throws Exception {
  driver.get("https://accounts.google.com/signup");
  //Element definitions for FirstName and LastName
  WebElement txtFName=driver.findElement(By.name("FirstName"));
  WebElement txtLName=driver.findElement(By.name("LastName"));
  //Type "webdriver" in First Name
  txtFName.sendKeys("webdriver");
  //Create an object for Actions Class
  Actions a = new Actions(driver);
  //select the value which is typed in FirstName field
  a.sendKeys(txtFName, Keys.chord(Keys.CONTROL,"a")).perform();
  //Performing copy action using CTRl+C
  a.sendKeys(Keys.chord(Keys.CONTROL,"c")).perform();
  //Performing paste action using CTRL+V in LastName field
  a.sendKeys(txtLName, Keys.chord(Keys.CONTROL,"v")).perform();
  Thread.sleep(2000);
 }

 @BeforeClass
 public void beforeClass() 
 {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }

 @AfterClass
 public void afterClass() throws Exception 
 {
  driver.quit();
 }
}

July 17, 2015

Keys Simulation using Selenium Webdriver

Here is the post how to simulate keys actions using selenium webdriver..Keys are used simulate user keyboard actions. For example if you want Press Enter, PageUp, Page Down, ALT or Tabs keys using selenium. It can be achieved by using Keys class..

For example after entering some text in search box you need not to click submit/search button. you can simulate ENTER key actions by using Keys class.

To achieve this we have to use Advanced User Interaction API.


More details about Advanced User Interactions can be found here --  Actions

In order to use AUI we need to create an object for Actions class.

In below scenario i will show how to Use Actions class and simulate Enter Key, Page Up and Page Down keys.


package sample;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Keys_Test {
 
 public WebDriver driver;

 @Test
 public void testKeys_Test() throws Exception {
  driver.get("http://www.amazon.in/");
  WebElement txtSearch=driver.findElement(By.id("twotabsearchtextbox"));
  txtSearch.sendKeys("mobiles");
  //Create an object for Actions class
  Actions a = new Actions(driver);
  //Simulate Enter key
  a.sendKeys(Keys.ENTER).perform();
  //Simulate Page Down
  a.sendKeys(Keys.PAGE_DOWN).perform();
  //Simulate page Up
  a.sendKeys(Keys.PAGE_UP).perform();
  Thread.sleep(2000);
 }

 @BeforeClass
 public void beforeClass() 
 {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }

 @AfterClass
 public void afterClass() throws Exception 
 {
  driver.quit();
 }
}

June 30, 2015

Run WebDriver script in Safari Browser

If you want to run WebDriver script in Safari. Manually we need to install the SafariDriver browser extension.

Prerequisites:

Safari 5.1+
SafariDriver browser extension.

you can download SafariDriver browser extension from below sites:

http://selenium-release.storage.googleapis.com/index.html?path=2.45/

or

There is safari driver section in selenium site.
http://docs.seleniumhq.org/download/  ---go to Safari Driver section and download the latest version of SafariDriver browser extension.

Once you download safari browser extension it will be in this format "SafariDriver.safariextz"

Double click that file or open with Safari browser. It will ask you for Installation confirmation, click Install and once installation is done you can see Webdriver Extension. Safari--Edit--Preferences.




If you see this extension then you should be able to run script in safari browser.

Once installed, writing a test for Safari is just as straightforward as using the FirefoxDriver

Below is the sample code of to run script using safari browser.





June 26, 2015

findElement vs findElements

Here is the post how to use findelements...

findElement()

  1. Find the first element within the current page
  2. Returns a single WebElement.
  3.  Syntax: WebElement findElement(By by)
findElements()

  1. Find all elements within the current page.
  2. Returns List of WebElements.
  3. Syntax:  java.util.List findElements(By by)

In this scenario i will show search for restaurants and  get number of results on webpage and get ratings of each restaurant .


May 15, 2015

Running Webdriver with Firebug and Selenium IDE addons

Problem:

When you run webdriver script it will not have IDE and Firebug addons. How to Run Firefox with  Firebug and Selenium IDE addons??


Solution:

Download Firebug xpi and Selenium IDE file from mozilla and start the firefox profile.

Below is the solution for that:

Pre-requisites:

Download firebug.xpi and selenium-ide.xpi.

Firefox : 33
firebug version 2.0.9 (Get it from here : https://getfirebug.com/releases/firebug/2.0/) and download 2.0.9
Selenium IDe version : 2.9.0  ( http://docs.seleniumhq.org/download/) Download selenium IDE.xpi and extract it. From extracted files you have to use selenium-ide.xpi.


Output:

Once the browser is launched then you can see Firebug and Selenium IDE are available in your browser. It will help you in debugging your scripts..


Below is the code: