Search code examples
seleniumcucumberselenium-gridcucumber-java

Selenium grid 4: How to run a test with differents browsers and OS


Beginner on selenium grid, I just created a small test that allows to navigate on the home page of stackoverflow For this I placed the selenium server on my disk, I opened 2 terminals as shown in the documentation here.

https://www.selenium.dev/documentation/grid/getting_started/#hub-and-nodes

The test starts well, and now I would like to run the same test under firefox while modifying the OS (for example: windows). How do I do this? Do I have to create another test file in my project? And then how to run the test with multiple configurations? I can’t find an answer to these questions.

My configuration:

-Linux Ubuntu 20.04

-Google and chrome 95

-the last version selenium server : 4.1.1

Here's the code of the test:

public class StepGoStackoverflow {
    RemoteWebDriver driver;
    String nodeUrl;

    @Given("I'm on google search page")
    public void i_m_on_google_search_page() {
        try {
            nodeUrl = "http://localhost:4444";

            ChromeOptions options = new ChromeOptions();
            // options.addArguments("--headless");
            options.addArguments("start-maximized");

            driver = new RemoteWebDriver(new URL(nodeUrl), options);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
            driver.get("https://www.google.com");
        } catch (MalformedURLException error) {
            error.printStackTrace();
        }
    }

    @When("I enter the name of the site")
    public void i_enter_the_name_of_the_site() {
        WebElement webElementList = driver.findElement(By.id("L2AGLb"));
        webElementList.click();
        driver.findElement(By.name("q")).sendKeys("stackoverflow", Keys.ENTER);
    }

    @Then("I'm navigated on the home page of stackoverflow")
    public void i_m_navigated_on_the_home_page_of_stackoverflow() {
        driver.findElement(By.xpath("//a[@href='https://stackoverflow.com/']")).click();
        driver.close();
    }
}

EDIT: I forgot to give the snipet of Gerkhin:

Feature: search the home page of stackoverflow

  Scenario: Go to the site stackoverflow
    Given I'm on google search page
    When I enter the name of the site
    Then I'm navigated on the home page of stackoverflow

Thank you


Solution

  • I can see that you are using Cucumber so I edited your question to add corresponding tag (since that matters as we're taking about parameterization).

    Grid part

    The idea of the Selenium Grid is that you install Grid components at a cluster where the nodes represent different OSs and each runtime/OS has one or more drivers and browsers installed.

    So you configure nodes so that they know where are drivers installed and how to run browsers (and what browser can each node execute: e.g. Chrome, Firefox) and each node registers at the grid component called hub.

    On one side a hub knows which nodes are running at which OS and what browsers they can operate with. On another hand hub acts as remote web driver (it exposes webdriver protocol to external clients aka automated tests).

    You can find an overview of the grid here and find configuration flags and aspects here.

    Code part

    On the code side you parameterize your tests so that each test is executed from scratch but with different capabilities set up. In your example you only use ChromeOptions which would make hub to dispatch your calls to a node that can run Chrome browser.

    Your challenge is to make your code configure different capabilities each run so that Grid will look up appropriate node in the cluster for your test (like "ah-ha.. you want to run Firefox on Linux which means I'll dispatch your calls to node C")

    Cucumber

    Since you are using Cucumber in your example I assume that you tend to incorporate it into final solution. If so, you need to start from learning parameterization practices in cucumber: See here, here and here; setting up context in Cucumber, and sharing the state in Cucumber with Dependency Injection with Guice or PicoContainer.

    Combine everything, add a handful of parallelization and you will gain the most value from your framework.