Wednesday 16 April 2014

How to execute selenium webdriver tes script parallel in multiple browser using testng parameter annotation.


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
 private WebDriver driver;

 @Parameters("browser")
 @BeforeMethod
 public void setup(String browser)
 {
  if(browser.equalsIgnoreCase("firefox"))
  {
   driver = new FirefoxDriver();
  }
  else if(browser.equalsIgnoreCase("iexplorer"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
   driver = new InternetExplorerDriver();
  }
  else if(browser.equalsIgnoreCase("chrome"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
   driver = new ChromeDriver();
  }
  driver.manage().window().maximize();
 }

 @AfterMethod
 public void tearDown()
 {
 driver.quit();
 }

 @Test
 public void testMultiBrowser() throws InterruptedException
 {
  driver.get("http://www.google.com");
  Thread.sleep(3000);
 }
}

2. Now we need to create TestNG.xml and write the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MultiBrowser">
       <test name="TestFirefox" verbose="10">
              <parameter name="browser" value="firefox" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="ChromeTest">
              <parameter name="browser" value="chrome" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="IETest">
              <parameter name="browser" value="iexplorer" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
</suite>

3. Now run the code from TestNG.xml

You can observer all the three browsers will open and perform the task at a time.

4 comments:

  1. Hi Praveen

    How i can choose multiple values from list box by index in JAVA.

    ReplyDelete
  2. Hi tried ur script but getting this error:-

    Script:-

    package Training;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;

    public class MultiBrowser {
    private WebDriver driver;

    @Parameters("browser")
    @BeforeMethod
    public void setup(String browser)
    {
    if(browser.equalsIgnoreCase("firefox"))
    {
    System.setProperty("webdriver.gecko.driver", "D:\\Training\\Programs\\SeleniumTutorial\\browserdrivers\\geckodriver.exe");
    driver = new FirefoxDriver();
    }
    else if(browser.equalsIgnoreCase("iexplorer"))
    {
    // Update the driver path with your location
    System.setProperty("webdriver.ie.driver", "D:\\Training\\Programs\\SeleniumTutorial\\browserdrivers\\IEDriverServer.exe");
    driver = new InternetExplorerDriver();
    }
    else if(browser.equalsIgnoreCase("chrome"))
    {
    // Update the driver path with your location
    System.setProperty("webdriver.chrome.driver", "D:\\Training\\Programs\\SeleniumTutorial\\browserdrivers\\chromedriver.exe");
    driver = new ChromeDriver();
    }
    driver.manage().window().maximize();
    }

    @AfterMethod
    public void tearDown()
    {
    driver.quit();
    }

    @Test
    public void testMultiBrowser() throws InterruptedException
    {
    driver.get("http://www.google.com");
    Thread.sleep(3000);
    }
    }


    Console Result:-
    org.testng.TestNGException:
    Cannot find class in classpath: MultiBrowser
    at org.testng.xml.XmlClass.loadClass(XmlClass.java:81)
    at org.testng.xml.XmlClass.init(XmlClass.java:73)
    at org.testng.xml.XmlClass.(XmlClass.java:59)
    at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:582)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:766)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:351)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
    at org.testng.xml.XMLParser.parse(XMLParser.java:39)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:16)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:9)
    at org.testng.xml.Parser.parse(Parser.java:170)
    at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:304)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:109)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)





    pls help!

    ReplyDelete