8

I am new to selenium testing. I want to run selenium test cases on multiple browsers against internet explorer, Firefox, opera and chrome. What approach i have to follow. Can you people please suggest me which is the best process.

Does selenium web driver supports multiple browsers or not???

We had written login script. It runs successful for Firefox, chrome and internet explorer individually. But i want to run it for those multiple browsers sequentially.

AndiDev
  • 1,272
  • 14
  • 26
user1441341
  • 495
  • 2
  • 9
  • 23
  • I automatized my test cases for multiple browsers with Parametrized approach. I used the example mentioned at [link](https://stackoverflow.com/questions/22051705/how-to-parameterize-junit-test-suite/27956177#27956177) – Knu8 Oct 19 '16 at 11:45

3 Answers3

6

web driver supports multiple browsers of course, there is also support for mobile

ChromeDriver

IEDiver

FirefoxDriver

OperaDriver

AndroidDriver

Here is an exemple to run the same tests in multiple browsers.

package ma.glasnost.test;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
        .........
DesiredCapabilities[] browserList = {DesiredCapabilities.chrome(),DesiredCapabilities.firefox(),DesiredCapabilities.internetExplorer(), DesiredCapabilities.opera()};
for (DesiredCapabilities browser : browserList)
{
    try {
        System.out.println("Testing in Browser: "+browser.getBrowserName());
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080/..."), browser);

Hope that helps.

reinierpost
  • 8,425
  • 1
  • 38
  • 70
Khalil
  • 259
  • 2
  • 8
  • Thanks for your reply @Khalil.. Could you please provide any sample example for it. – user1441341 Apr 18 '13 at 08:44
  • just give what you want to do, which browser you use and i will give an exemple. NB: if you take a look in the links that i post it you will have getting started for each browser driver. don't hisitate to ask me again for any clarification – Khalil Apr 18 '13 at 09:16
3
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Sample {
    private WebDriver _driver;

    @Test
    public void IEconfiguration() throws Exception {
        System.setProperty("webdriver.ie.driver",
        "D:/Softwares/Selenium softwares/drivers/IEDriverServer.exe");
        _driver = new InternetExplorerDriver();
        login();
    }

    @Test
    public void FFconfiguration() throws Exception {
        _driver = new FirefoxDriver();
        login();
    }

    @Test
    public void CRconfiguration() throws Exception {
        System.setProperty("webdriver.chrome.driver",
                "D:/Softwares/Selenium softwares/drivers/chromedriver.exe");
        _driver = new ChromeDriver();
        //_driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
        login();
    }

    public void login() throws Exception {
        _driver.get("http://www.google.com");
    }       
}

Before that we have to install the chrome and internet explorer drivers .exe files and run those.

haraman
  • 2,744
  • 2
  • 27
  • 50
user1441341
  • 495
  • 2
  • 9
  • 23
  • Although this code is quite good to start with multiple browser testing, a more advanced example without code duplication that will avoid driver vs. browser versions hassle, would be to use docker, Selenium Grid https://testdriven.io/blog/distributed-testing-with-selenium-grid/ – Karel Frajták Jul 16 '19 at 09:20
2

You could use the WebDriver Extensions framework's JUnitRunner

Here is an example test googling for "Hello World"

@RunWith(WebDriverRunner.class)
@Firefox
@Chrome
@InternetExplorer
public class WebDriverExtensionsExampleTest {

    // Model
    @FindBy(name = "q")
    WebElement queryInput;
    @FindBy(name = "btnG")
    WebElement searchButton;
    @FindBy(id = "search")
    WebElement searchResult;

    @Test
    public void searchGoogleForHelloWorldTest() {
        open("http://www.google.com");
        assertCurrentUrlContains("google");

        type("Hello World", queryInput);
        click(searchButton);

        waitFor(3, SECONDS);
        assertTextContains("Hello World", searchResult);
    }
}

just make sure to add the WebDriver Extensions framework amongst your maven pom.xml dependencies

<dependency>
    <groupId>com.github.webdriverextensions</groupId>
    <artifactId>webdriverextensions</artifactId>
    <version>1.2.1</version>
</dependency>

The drivers can be downloaded using the provided maven plugin. Simply add

<plugin>
    <groupId>com.github.webdriverextensions</groupId>
    <artifactId>webdriverextensions-maven-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
        <execution>
            <goals>
                <goal>install-drivers</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <drivers>
            <driver>
                <name>internetexplorerdriver</name>
                <version>2.44</version>
            </driver>
            <driver>
                <name>chromedriver</name>
                <version>2.12</version>
            </driver>
        </drivers>
    </configuration>
</plugin>

to your pom.xml. Or if you prefer downloading them manually just annotate the test class with the

@DriverPaths(chrome="path/to/chromedriver", internetExplorer ="path/to/internetexplorerdriver")

annotation pointing at the drivers.

Note that the above example uses static methods from the WebDriver Extensions Bot class to make the test more readable. However you are not tied to using them. The above test rewritten in pure Selenium WebDriver would look like this

    @Test
    public void searchGoogleForHelloWorldTest() throws InterruptedException {
        WebDriver driver = WebDriverExtensionsContext.getDriver();

        driver.get("http://www.google.com");
        assert driver.getCurrentUrl().contains("google");

        queryInput.sendKeys("Hello World");
        searchButton.click();

        SECONDS.sleep(3);
        assert searchResult.getText().contains("Hello World");
    }
AndiDev
  • 1,272
  • 14
  • 26