vnnogile
vnnogile

Reputation: 153

How to run Selenium webdriver test cases in multiple browser one after other browser get finish using TestNg and eclipse

I want to run selenium webdriver test cases in all multiple browser but not in parallel.Is it possible to run test cases in all multiple browser without using xml and selenium grid.Can we do it by using annotation and java classes.I wanted that my test cases should execute in firefox first and after completion of execution in firefox it should start execution in chrome and so on.

I have tried this code but execution is parallel by using xml.

CrossBrowserScript.java

import java.util.concurrent.TimeUnit;

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.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CrossBrowserScript {

    WebDriver driver;

    /**
     * This function will execute before each Test tag in testng.xml
     * @param browser
     * @throws Exception
     */
    @BeforeTest
    @Parameters("browser")
    public void setup(String browser) throws Exception{
        //Check if parameter passed from TestNG is 'firefox'
        if(browser.equalsIgnoreCase("firefox")){
        //create firefox instance
            driver = new FirefoxDriver();
        }
        //Check if parameter passed as 'chrome'
        else if(browser.equalsIgnoreCase("chrome")){
            //set path to chromedriver.exe
            System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");
            //create chrome instance
            driver = new ChromeDriver();
        }
        //Check if parameter passed as 'IE'
                else if(browser.equalsIgnoreCase("ie")){
                    //set path to IE.exe
                    System.setProperty("webdriver.ie.driver",".\\IEDriverServer.exe");
                    //create IE instance
                    driver = new InternetExplorerDriver();
                }
        else{
            //If no browser passed throw exception
            throw new Exception("Browser is not correct");
        }
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test
    public void testParameterWithXML() throws InterruptedException{
        driver.get("http://demo.guru99.com/V4/");
        //Find user name
        WebElement userName = driver.findElement(By.name("uid"));
        //Fill user name
        userName.sendKeys("guru99");
        //Find password
        WebElement password = driver.findElement(By.name("password"));
        //Fill password
        password.sendKeys("guru99");
    }
}

testngCrossBrowser.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3"  parallel="tests" >
  <test name="ChromeTest">
  <parameter name="browser" value="Chrome" />
    <classes>
       <class name="parallelTest.CrossBrowserScript">
       </class>
    </classes>
  </test>
  <test name="FirefoxTest">
  <parameter name="browser" value="Firefox" />
    <classes>
       <class name="parallelTest.CrossBrowserScript">
       </class>
    </classes>
  </test>
  <test name="IETest">
  <parameter name="browser" value="IE" />
    <classes>
       <class name="parallelTest.CrossBrowserScript">
       </class>
    </classes>
  </test>
 </suite>

Upvotes: 3

Views: 19046

Answers (2)

Sameer Patil
Sameer Patil

Reputation: 461

Try changing thread-count="3" to thread-count="1". This should execute your tests in sequence as mentioned by you in TestNG file.

Upvotes: 0

Paweł Adamski
Paweł Adamski

Reputation: 3415

With JUnit you can create MethodRule (http://junit.org/apidocs/org/junit/rules/MethodRule.html) which will run tests in all browsers.

Example:

public class ManyBrowsers implements MethodRule {

public static WebDriver driver;

@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            //RUN FIREFOX
            driver = new FirefoxDriver();
            base.evaluate();
            driver.quit();

            //RUN CHROME
            File f = //PATH to CHROME DRIVER
            System.setProperty("webdriver.chrome.driver", f.getAbsolutePath());
            driver = new ChromeDriver();
            base.evaluate();
            driver.quit();
        }
    };
}

}

Example test:

public class VisitGoogle {

@Rule
public ManyBrowsers browsers = new ManyBrowsers();

@Test
public void test() {
    ManyBrowsers.driver.navigate().to("https://www.google.com/");
}

}

Upvotes: 2

Related Questions