Is it possible to rename tests in BrowserStack based on the name of the scenarios specified in gherkin?

83 Views Asked by At

I have a project written in Selenide and Cucumber with TestNG. I'm running tests from my TestRunner, and the tests are written in Gherkin syntax. I'm integrated with BrowserStack and everything works correctly - build is running properly in BrowserStack. But i have a problem with test names. Currently every testname is the same - runners.TestRunner-runScenario. I read the documentation and see that there is a sessionName parameter that I can name in capabilities. The problem is that I am running several tests one after the other at the same time and this parameter would have to change depending on the currently running test. For exapmle, with this test:

Feature: Login tests

  Scenario: Correct login
    Given The user is on the "site"
    When the page is loaded
    And the user enter the email "test" and password "test" and click "Sign in" button
    Then the user should be logged in

  Scenario: Incorrect login
    Given The user is on the "site"
    When the page is loaded
    And the user enter the email "xxx" and password "xxx" and click "Sign in" button
    Then the user should see the error

I want to get the name of each test from Scenario (eg.Correct login, Incorrect login) and use it in BrowserStack as a test name each time, this test is running. Is it possible?

My TestRunner is simple:

@Test
@CucumberOptions(
        features = "src/test/resources/features",
        glue = {"stepDefinitions"},
        plugin = {"pretty", "html:target/cucumber-report.html", "io.qameta.allure.cucumber7jvm.AllureCucumber7Jvm"}
)
public class TestRunner extends AbstractTestNGCucumberTests {
    static {
        System.setProperty("IS_CUCUMBER_TEST", "true");
    }
}

Edit after Abhimanyu Koul's answer:

This is a great solution to use JavascriptExecutor. I changed tearDown method:

@After
public void tearDown(Scenario scenario) {
    String scenarioName = scenario.getName();
    setBrowserStackTestName(scenarioName);
    Selenide.closeWindow();
}

and added setBrowserStackTestName method:

private void setBrowserStackTestName(String testName) {
    JavascriptExecutor jsExecutor = (JavascriptExecutor) WebDriverRunner.getWebDriver();
    jsExecutor.executeScript("browserstack_executor: {\"action\": \"setSessionName\", \"arguments\": {\"name\":\"" + testName + "\"}}");
}

But there is another problem with that. Now in BrowserStack name of the test is changing, but only for one moment - in the end of test. But when status is showing and starts to run another test, test name is back to default name: runners.TestRunner-runScenario

1

There are 1 best solutions below

1
Abhimanyu Koul On

You can explore the option of using the browserstack_executor command which allows to update the session name at runtime. You can refer to the reference documentation for a better understanding of the required syntax.

You can use the scenario.getName() method in conjunction with the browserstack_executor command in the teardown method to set the session name on the BrowserStack Platform at runtime.

Reference Code ->

@After
public void teardown(Scenario scenario) throws Exception {
    Thread.sleep(2000);
    String scenarioName = scenario.getName();
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("browserstack_executor: {\"action\": \"setSessionName\", \"arguments\": {\"name\":\"" + scenarioName + "\" }}");
    driver.quit();
}

Note: You can use the browserstack_executor command outside of the teardown method as well.