3

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>
vnnogile
  • 143
  • 2
  • 4
  • 12

2 Answers2

2

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/");
}

}

Paweł Adamski
  • 3,285
  • 2
  • 28
  • 48
  • Thanks Pawel for reply,actually I am new to selenium webdriver so its very hard for me to understand above code ,can you please give me one example code related with this topic that would be very helpful to me. – vnnogile Apr 07 '15 at 09:02
  • I found out that it is even is easier to use JUnit MethodRule. I've edited my answer. Now it is fully working solution. – Paweł Adamski Apr 07 '15 at 09:42
  • Hi Pawel,I added above code in my code but its not working It throws exceptions. I have created new post so that others also see that post ,please reply my post link http://stackoverflow.com/questions/29627516/runing-test-cases-in-all-browser-one-after-another – vnnogile Apr 14 '15 at 12:39
  • Thank you, Pawel. This was very, very helpful! – Ismoh Dec 12 '17 at 08:28
0

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

Sameer Patil
  • 441
  • 3
  • 10