Pages

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.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>




19 comments:

  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

    ReplyDelete
  2. Thanks Neeraj, this seems to be useful.

    ReplyDelete
  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

    ReplyDelete
  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е ... youtu.be

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
    Replies
    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.

      Delete
  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.

    ReplyDelete
  13. I'm really enjoying the design and layout of your site.Thanks.
    บาคาร่า
    จีคลับ
    gclub

    ReplyDelete
  14. 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

    ReplyDelete
  15. Hey,Thanks for sharing.very interesting.I have bookmarked your blog because very clear information is given.Keep updating more
    big data courses in bangalore with placement

    ReplyDelete

  16. Fantastic blog, i am impressed by the information provided it was very helpful hopping for some more informative posts thank you .
    typeerror nonetype object is not subscriptable

    ReplyDelete
  17. Excellent blog information shared was very informative and valuable looking forward for next blog thank you.
    Data Analytics Course Online

    ReplyDelete