0

We have created web automation scripts for particular website. And now the manual QA team requested to automate mobile responsiveness as well so we are using below code to achieve mobile responsiveness.

 String currentDir = System.getProperty("user.dir");
String browserName = prop.getProperty("browser");

if (browserName.equals("chrome")) {
    WebDriverManager.chromedriver().setup();
    
    ChromeOptions chromeOptions = new ChromeOptions();
    Map<String, Object> mobileEmulation = new HashMap<>();
    mobileEmulation.put("deviceName", "Nexus 5");
    chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
    
    chromeOptions.addArguments("--remote-debugging-port=9222"); // Enable remote debugging
    
    driver = new ChromeDriver(chromeOptions);
}

We are planning to use same test classes as web script for mobile responsiveness as well. How can we run web script in one browser and mobile responsive script in another browser at the same time?

1 Answer 1

1

To execute the same test scenarios in two different browsers simultaneously, you can utilize TestNG's parallel execution feature along with Selenium's WebDriver capabilities. Here's an approach you can follow:

1 Modify your test class to include the browser name as a parameter:

@Test
public void testScenario(String browserName) {
    // Test scenario implementation
}

2 Update your testng.xml file to include the test class twice, each with a different browser parameter:

<suite name="Test Suite" parallel="tests" thread-count="2">
    <test name="Web Test">
        <parameter name="browserName" value="chrome"/>
        <classes>
            <class name="com.example.TestClass"/>
        </classes>
    </test>
    <test name="Mobile Test">
        <parameter name="browserName" value="mobile"/>
        <classes>
            <class name="com.example.TestClass"/>
        </classes>
    </test>
</suite>

3 In your test class, create a method to initialize the WebDriver based on the browser name parameter:

private WebDriver createWebDriver(String browserName) {
    if (browserName.equals("chrome")) {
        WebDriverManager.chromedriver().setup();
        return new ChromeDriver();
    } else if (browserName.equals("mobile")) {
        WebDriverManager.chromedriver().setup();

        ChromeOptions chromeOptions = new ChromeOptions();
        Map<String, Object> mobileEmulation = new HashMap<>();
        mobileEmulation.put("deviceName", "Nexus 5");
        chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

        chromeOptions.addArguments("--remote-debugging-port=9222"); // Enable remote debugging

        return new ChromeDriver(chromeOptions);
    }
    return null;
}

4 Within your test scenario, retrieve the browser name parameter and initialize the WebDriver accordingly:

@Test
public void testScenario() {
    String browserName = System.getProperty("browserName");
    WebDriver driver = createWebDriver(browserName);
    // Rest of the test scenario implementation
}

Now, when you execute the testng.xml file, TestNG will run the test class twice, once with the "chrome" browser parameter (for web testing) and once with the "mobile" browser parameter (for mobile responsiveness testing). The test scenarios will be executed simultaneously in different browsers.

Ensure that you have proper synchronization mechanisms in place if the test scenarios interact with shared resources or have dependencies between them.

This approach allows you to reuse the same test class for both web and mobile responsiveness testing while executing them in different browsers simultaneously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.