0

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 to execute my test cases from one browser to another but not simultaneously.but it throw exceptions.

ManyBrowsers.java

import java.io.File;

import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

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 = new File("D:\\SeleniumTestCases\\Selenium_Drivers\\chromedriver" ) ;//PATH to CHROME DRIVER
            System.setProperty("webdriver.chrome.driver", f.getAbsolutePath());
            driver = new ChromeDriver();
            base.evaluate();
            driver.quit();
        }
    };
}
}

Example Test

import java.util.concurrent.TimeUnit;

import org.junit.Rule;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;

import com.thoughtworks.selenium.webdriven.commands.WaitForPageToLoad;

public class TestClass1 {

    protected static WebDriver driver;

    protected static String result;

    @Rule
    public ManyBrowsers browsers = new ManyBrowsers();

    @BeforeClass
    public static void setup() {
        ManyBrowsers.driver.navigate().to("http://www.floraindia.com");
        }



    @Test
    void Testcase1() {
        System.out.println("Testcase1");


        driver.findElement(By.id("kwsch")).sendKeys("Red");

        driver.findElement(By.xpath("//input[@src='image/go.gif']")).click();
        }

  @Test
  public void testGmail() throws Exception {
    driver.get("http://www.gmail.com");
    driver.findElement(By.id("Email")).clear();
    driver.findElement(By.id("Email")).sendKeys("testemail");
    driver.findElement(By.id("Passwd")).clear();
    driver.findElement(By.id("Passwd")).sendKeys("123456");
    driver.findElement(By.id("signIn")).click();
  }
  @AfterClass
    public static void teardown() {

        driver.close();

        driver.quit();

    }
vnnogile
  • 143
  • 2
  • 4
  • 12

2 Answers2

0

Create different classes for the different tests in different browsers. For example:

First class:

public class FirefoxTest
{

  WebDriver driver;

  @BeforeClass
  public void beforeClass() {
    this.driver = new FirefoxDriver();
  }

  @Test
  public void test() {
    this.driver.get("http://google.com");
  }

  @AfterClass
  public void afterClass() {
    this.driver.quit();
  }

}

Second class:

public class ChromeTest
{

  WebDriver driver;

  @BeforeClass
  public void beforeClass() {
    this.driver = new ChromeDriver();
  }

  @Test
  public void test() {
    this.driver.get("http://google.com");
  }

  @AfterClass
  public void afterClass() {
    this.driver.quit();
  }

}

If you run your project as a TestNG Test, it will create your xml file programatically which will include all of the classes, so in this case you don't have to create it manually.

peetya
  • 3,578
  • 2
  • 16
  • 30
0

You get NullPointerException because method evaluate from class ManyBrowsers is invoked only before @Test methods.

You should use class ManyBrowsers only in methods with @Test annotation.

Paweł Adamski
  • 3,285
  • 2
  • 28
  • 48
  • Hi Pawel,Thanks for reply ,yes I am getting NullPointerException,I didnt understand were to use class ManyBrowsers ,can you please do changes in my code .This will really helpful to me – vnnogile Apr 16 '15 at 05:26
  • Use code from my answer to this question: http://stackoverflow.com/questions/29486221/how-to-run-selenium-webdriver-test-cases-in-multiple-browser-one-after-other-bro/29486519?noredirect=1#comment47397997_29486519 – Paweł Adamski Apr 16 '15 at 08:17
  • I have tried your code look at this screen shots 1.class VisitGoogle http://imgur.com/QHuHEvN, 2.class ManyBrowsers http://imgur.com/andxFBa 3.Exception http://imgur.com/YyieLDa – vnnogile Apr 16 '15 at 10:39
  • Your problem is that you run test with TestNG framework, while I sad that it my solutions works under JUnit. – Paweł Adamski Apr 16 '15 at 11:06
  • Unfortunately TestNG doesn't support MethodRules, see http://stackoverflow.com/questions/6099633/does-testng-support-something-like-junit4s-rule . For now I have now idea how to implement it in TestNG – Paweł Adamski Apr 16 '15 at 12:32
  • Is it possible with .xml file ? – vnnogile Apr 16 '15 at 13:16
  • Sorry, but I'm not expert of TestNg – Paweł Adamski Apr 16 '15 at 13:27