Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running selenium webdriver test cases against multiple browsers

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.

like image 508
user1441341 Avatar asked Apr 18 '13 07:04

user1441341


People also ask

How do I run Selenium test cases in different browsers?

Create an XML which will help us in parameterizing the browser name and don't forget to mention parallel="tests" in order to execute in all the browsers simultaneously. Execute the script by performing right-click on the XML file and select 'Run As' >> 'TestNG' Suite as shown below.

Can Selenium run multiple browsers?

Selenium is able to run tests in parallel, but CrossBrowserTesting makes it even simpler - allowing you to test across multiple browsers and mobile devices with just a few extra lines of code.

How can I test multiple browsers at once?

BrowserStack Live is a mobile application and browser testing tool. You can test your website on 2000+ browsers, thereby making it one of the comprehensive browser compatibility tests. You can test your website on Android and iOS real devices using their cloud platform.


2 Answers

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.

like image 87
Khalil Avatar answered Oct 24 '22 08:10

Khalil


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");
    }
like image 29
AndiDev Avatar answered Oct 24 '22 06:10

AndiDev