Search This Blog

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.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class WebDriverDemo {

 private static WebDriver driver = null;
 @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();
  WebElement search= driver.findElement("p"));
  search.sendKeys("automation blog by niraj");
  Assert.assertTrue(driver.getPageSource().contains(""),"Failed in "+ BROWSER);


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.


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


  1. Τhere's definately a lot to find out about this issue. I like all the points you made.

    Here is my web-site :: ringing sound in ear
    my web site > ringing in ears treatment

  2. Thanks Neeraj, this seems to be useful.

  3. Gοоd day! I κnow thiѕ is kind
    of off toρiс but ӏ wаs wondering ωhiсh blog ρlatform arе you using fοг this
    wеbsite? I'm getting fed up of Wordpress because I've had problems
    ωith hackеrѕ and I'm looking at alternatives for another platform. I would be awesome if you could point me in the direction of a good platform.

    Here is my site - lose fat fast

  4. Hellο therе! This post сould not bе
    written any bettеr! Reading through this post reminds
    me of my preνіous гoom mate! He always kept talking abοut this.

    I will forward thіs ωritе-up to him. Fairly certain he will haѵe a good read.
    Thanks for shагing!

    Also visіt my ωеb рagе ...

  5. It's a pity you don't haѵe a donate button!

    I'd without a doubt donate to this excellent blog! I suppose for now i'll
    sеttlе fоr booκ-maгking and aԁding
    yоuг RSS feed tо my Google аcсount.
    Ӏ looκ forwаrd to brand nеw upԁates anԁ will talκ аbоut this site with mу Faсebook grоup.

    Chat soon!

    Alsο visіt my websіte - locksmith solihull

  6. Howdy! Do you know іf theу mаke anу plugіns
    to assist wіth SEO? Ι'm trying to get my blog to rank for some targeted keywords but I'm not seeing νеry good results.

    ӏf yоu κnow of any plеase sharе.
    Manу thanks!

    Αlѕο viѕit my homepage - fix leaking pipes

  7. My programmer is trying to persuade me to move to .net from PHP.

    I have always disliked the idea because of the expenses.

    But he's tryiong none the less. I've been using Movable-type
    on several websites for about a year and am worried about switching to another platform.
    I have heard very good things about blogengine.
    net. Is there a way I can transfer all my wordpress posts into it?
    Any kind of help would be greatly appreciated!

    Review my web blog; gamblers

  8. I'm really enjoying the design and layout of your site. It's
    a νеrу easy on thе eyes which makes it much
    more еnjoyable for me to come here and visit mоre оften.
    Dіd you hіrе οut a designer to crеаte your theme?
    Exсellent ωork!

    My site ... Makes boobs bigger naturally

  9. Hey there! Ι've been following your web site for a while now and finally got the bravery to go ahead and give you a shout out from Kingwood Tx! Just wanted to mention keep up the good work!

    My blog post; how to lose belly quickly

  10. Hellο, i read your blοg οccasionally
    аnd і oωn a similar оne аnd i waѕ just curіouѕ
    if you get а lot of spam comments? If so how dо you stоp it, аny
    plugin oг anything yоu can suggest? I gеt so much lately it's driving me insane so any assistance is very much appreciated.

    Also visit my homepage; 24 hr Emergency Plumber Solihull

  11. Please, do I place the XML file in the XML file folder in Webdriver? Thanks. I'm completely new to webdriver. Thanks.

    1. If you are talking about placing TestNG.xml file then follow these steps (in Eclipse).

      1. You should have testng configured in your maven dependencies.
      2. Place the testng.xml directly in your project folder.
      3. To check if it works right click on XML and Select 'Run As' -> TestNG Suite. Done!

      I hope this helps.

  12. Your information about software testing is really interesting. Also I want to know the latest new techniques which are implemented in software testing. Can you update it in your website.

  13. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.

  14. It was very nice blog to learn about SAP HR.Thanks for sharing new things.SAP HR

  15. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

  16. Advanced Selenium Framework Training in Chennai by Vishwa

    Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

  17. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai


  18. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

  19. I'm really enjoying the design and layout of your site.Thanks.

  20. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

  21. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well iOS App Development Company
    iOS App Development Company

  22. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text
    Mobile Marketing Services
    mobile marketing companies
    Sms API

  23. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    python training in bangalore

  24. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...

    Embedded training in chennai | Embedded training centre in chennai | PLC Training institute in chennai