Pages

Search This Blog

Monday, September 12, 2011

How to select value in drop down using Webdriver

How to select values in drop down using Webdriver

This post will take you through the way you can select the value in a drop down. Webdriver does not have straight forward command "SELECT" which Selenium does.
In below example i will open home page of eBay India and search ipod in Consumer Electronic category. This page has a category drop down in which i will select
Consumer Electronics.



package com.web;
import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class DropDownSelection {
 private static WebDriver driver=null;
 @BeforeTest
 @org.testng.annotations.Parameters({"BROWSER"})
 public void setup(String BROWSER){
  if(BROWSER.equals("FF")){
   File profileDir = new File("C:\\nir");
     FirefoxProfile profile = new FirefoxProfile(profileDir);
     profile.setPreference("general.useragent.override", "same user agent string as above");
     driver= new FirefoxDriver(profile);
   System.out.println("Broser Selected " + BROWSER);  
  }else if(BROWSER.equals("IE")){
   System.out.println("Broser Selected " + BROWSER);
   driver = new InternetExplorerDriver();
  }else if(BROWSER.equals("HU")){
   System.out.println("Broser Selected " + BROWSER);
   driver =new HtmlUnitDriver();
   
  } else
  {
   System.out.println("Broser Selected " + BROWSER);
   driver = new ChromeDriver();
  }

 }
 @Test
 public void testDropDownSelection() throws InterruptedException{
  
  driver.get("http://www.ebay.in");
  WebElement search = driver.findElement(By.name("_nkw"));
  search.sendKeys("ipod");

   List dd = driver.findElement(By.name("_sacat")).findElements(By.tagName("option"));
   for (WebElement option : dd) {
    System.out.println(option.getText());
    if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
     option.click();
     break;
    }
   }
   driver.findElement(By.xpath("//input[@value='Search']")).submit();
   Thread.sleep(5000);
   driver.getPageSource().contains("results found");
  driver.quit();
 }

}




TestNG.xml
Using TestNG suite we will run the test case.



<?xml version="1.0" encoding="UTF-8"?>
<suite name="webDriver" parallel="tests">
 <listeners>
  <listener class-name="org.uncommons.reportng.HTMLReporter" />
  <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
 </listeners>
 <test name="WebDriverDemo Witn FF" preserve-order="true">
  <parameter name="BROWSER" value="FF" />
  <classes>
   <class name="com.web.DropDownSelection" />
  </classes>
  </test>

  </b>
</suite>



If above code does not work with you configuration just try another way given below.



 List  options = driver.findElements(By.tagName("option"));
  for (WebElement option : options) {
   if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
    option.click();
   }
      }



Thursday, July 28, 2011

Running test case in multiple browsers parallely in WebDriver

we can run test script in different browsers parallely using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox and chrome parallely.





package com.web;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelRunning {

 private  WebDriver driver=null;
 @BeforeTest
 @Parameters ({"BROWSER"})
 public void setup(String BROWSER){
  System.out.println("Browser: " + BROWSER);

  if (BROWSER.equals("FF")) {
   System.out.println("FF is selected");
   driver = new FirefoxDriver();
  } else if (BROWSER.equals("IE")) {
   System.out.println("IE is selected");
   driver = new InternetExplorerDriver();
  } else if (BROWSER.equals("HU")) {
   System.out.println("HU is selected");
   driver = new HtmlUnitDriver();
  } else if (BROWSER.equals("CH")){
   System.out.println("Google chrome is selected");
   driver = new ChromeDriver();
  }
 }
 
 @Test
 public  void testParallel()throws Exception{

  driver.get("http://www.google.com");
  Thread.sleep(5000);
  WebElement search = driver.findElement(By.name("q"));
  search.sendKeys("automation blog by niraj");
  search.submit();
  Thread.sleep(5000);
  Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"));
  driver.findElement(By.xpath("//a[contains(@href,'automationtricks.blogspot.com')]")).click();
  Thread.sleep(15000);
  Assert.assertTrue(driver.getPageSource().contains("working as Associate Manager @CSC"));
  driver.quit();

 }
}



In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.

TestNG.xml




<?xml version="1.0" encoding="UTF-8"?>
<suite name="webDriver" parallel="tests">
  <test name="WebDriverDemo Witn FF" preserve-order="true">
  <parameter name="BROWSER" value="FF" />
  <classes>
   <class name="com.web.ParallelRunning" />
  </classes>
 </test>
  <test name="WebDriverDemo with IE" preserve-order="ture">
  <parameter name="BROWSER" value="IE"></parameter>
  <classes>
   <class name="com.web.ParallelRunning"></class>
  </classes>
 </test>
 
 <test name="WebDriverDemo with HTML unit" preserve-order="true">
  <parameter name="BROWSER" value="HU"></parameter>
  <classes>
   <class name="com.web.ParallelRunning"></class>
  </classes>
 </test>
 
  <test name="WebDriverDemo with HTML unit" preserve-order="true">
  <parameter name="BROWSER" value="CH"></parameter>
  <classes>
   <class name="com.web.ParallelRunning"></class>
  </classes>
 </test>
</suite>





Wednesday, July 27, 2011

Runing test script in multiple browsers using WebDriver

we can run test script in different browsers using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox chrome.





package com.web;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class WebDriverDemo {
 

 private static WebDriver driver = null;
 
 @Test
 @Parameters( {"BROWSER"}) 
 public  void testread(String BROWSER)throws Exception{

  System.out.println("Browser: " + BROWSER);

  if (BROWSER.equals("FF")) {
   System.out.println("FF is selected");
   driver = new FirefoxDriver();
  } else if (BROWSER.equals("IE")) {
   System.out.println("IE is selected");
   driver = new InternetExplorerDriver();
  } else if (BROWSER.equals("HU")) {
   System.out.println("HU is selected");
   driver = new HtmlUnitDriver();
  } else if (BROWSER.equals("CH")){
   System.out.println("Google chrome is selected");
   driver = new ChromeDriver();
  }
  driver.navigate().to("http://www.yahoo.com");
  Thread.sleep(10000);
  
  WebElement search= driver.findElement(By.name("p"));
  search.sendKeys("automation blog by niraj");
  search.submit();
  
  Thread.sleep(5000);
  Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"),"Failed in "+ BROWSER);
  driver.close();

 }
}


In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.

TestNG.xml




<?xml version="1.0" encoding="UTF-8"?>
<suite name="webDriver">
 <test name="WebDriverDemo Witn FF" preserve-order="true">
  <parameter name="BROWSER" value="FF" />
  <classes>
   <class name="com.web.WebDriverDemo" />
  </classes>
 </test>
  <test name="WebDriverDemo with IE" preserve-order="ture">
  <parameter name="BROWSER" value="IE"></parameter>
  <classes>
   <class name="com.web.WebDriverDemo"></class>
  </classes>
 </test>
 <test name="WebDriverDemo with HTML unit" preserve-order="true">
  <parameter name="BROWSER" value="HU"></parameter>
  <classes>
   <class name="com.web.WebDriverDemo"></class>
  </classes>
 </test>
 <test name="WebDriverDemo with chrome" preserve-order="true">
  <parameter name="BROWSER" value="CH"></parameter>
  <classes>
   <class name="com.web.WebDriverDemo"></class>
  </classes>
 </test>
</suite>




Friday, July 22, 2011

Easy Way To Automate Using WebDriver

WebDriver is a clean, fast framework for Automation development of web-apps. WebDriver takes many advantages over selenium because selenium is written in java-script which causes a significant weakness: browsers impose a pretty strictly model , try to upload a file ,form control etc.
webDriver takes a different approach to solve the same problem as selenium. Rather than being running a java-script application within a browser, it uses whichever the mechanism is most appropriate to control the browser. Like for Firefox ,this means that webDriver is implemented as an extension.
The real beauty which I like most is real time environment, which means we are more closely modeling how the user interacts with the browser, and that we can type into "file" input elements. WebDriver can make use of facilities offered by the Operating System.
With the benefit of hindsight, we have developed a cleaner, Object-based API for WebDriver, rather than follow Selenium's dictionary-based approach.

Please download the respective jar related to your language from google code selenium project Download

Now follow below steps:-
1:- Extract the download file into a folder name and include the jars file ion your ClassPath
2:- Sample coding in java




import junit.framework.TestCase;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class WebDriverTest extends TestCase{
@Test
public void test_hellotest()throws Exception{
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
// Find the text input element by its name WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Niraj Kumar");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.close();
}
}