How to run Serenity BDD tests in Chrome Browser

HOME

Serenity BDD has strong WebDriver integration, and manages the WebDriver instances. It is not needed to create or close the WebDriver instance of the Serenity Tests.

Serenity uses a library WebDriver Manager which manages the driver for us. We don’t need to explicitly download and configure the WebDriver binaries for us.

The default browser of Serenity is Firefox.

    @Managed(driver = "chrome")
    WebDriver driver;

@Managed annotation in Serenity will manage the WebDriver instance, including opening the appropriate driver at the start of each test, and shutting it down when the test is finished. @Managed provides an option to the user to select the WebDriver driver to the run the tests in it. The possible values are firefoxchromeiexplorerphantomjsappiumsafariedge and htmlunit . There are multiple ways to manage the WebDriver. One of the way is shown below:

In the below program, the tests are running on Chrome browser. The driver name is mentioned with @Managed annotation.

@RunWith(SerenityRunner.class)
public class ChromeTest {

    @Managed(driver = "chrome")
    WebDriver driver;

    @Steps
    NavigateActions navigate;

    @Test
    public void openBrowser()
    {
        navigate.toTheHomePage();
    }

}

NavigateActions

public class NavigateActions extends UIInteractionSteps {

    @Step
    public void toTheHomePage() {
         openUrl("https://opensource-demo.orangehrmlive.com/");
    }
}

There is another way to assign Chrome to the WebDriver. This can be defined in serenity.config or serenity.properties.

serenity.config

webdriver{
    driver = chrome

}

serenity.properties

webdriver.driver = chrome

When the webdriver is defined in properties file, it is not needed to redefine it in the Test as shown below:

@RunWith(SerenityRunner.class)
public class ChromeTest {

    @Managed
    WebDriver driver;

    @Steps
    NavigateActions navigate;

    @Test
    public void openBrowser()
    {
        navigate.toTheHomePage();
    }

}

Manually Configure ChromeDriver

To run your web tests with a given driver, download the correct driver binary and place it under src/test resources. The chromedriver binary can be downloaded from here – ChromeDriver – WebDriver for Chrome (chromium.org)

It is always advisable to create a drivers directory under src/test/resources. The tests can be run on different Operating System, so create three sub-directories named as windows, mac, linux within drivers directory. Place the driver binary in these directories as shown below :

Below is the sample Serenity.config for the chrome driver.

webdriver{
    driver = chrome

}

drivers {
  windows {
     webdriver.chrome.driver = "src/test/resources/drivers/windows/chromedriver.exe"
   }
  mac {
      webdriver.chrome.driver = "src/test/resources/webdriver/mac/chromedriver.exe"
    }
    linux {
      webdriver.chrome.driver = "src/test/resources/webdriver/linux/chromedriver.exe"
    }
  }
}

How to add Chrome Options in Serenity

We can configure various chrome options in Serenity by adding them to a property call switches in serenity.config.

chrome {
        switches ="""--windows.size=1024,800, --start-maximized;--test-type;--no-sandbox;--ignore- 
                             certificate-errors; --headless;
                   --disable-popup-blocking;--disable-default-apps;--disable-extensions-file-access-check;
                   --incognito;--disable-infobars,--disable-gpu"""
  }

How to set Chrome Preferences in Serenity

chrome {
  switches ="""--windows.size=1024,800, --start-maximized;--test-type;--no-sandbox;--ignore-certificate-errors;
                   --disable-popup-blocking;--disable-default-apps;--disable-extensions-file-access-check;
                   --incognito;--disable-infobars,--disable-gpu"""

  preferences {
      download {
          prompt_for_download: false
          default_directory: "$TEMPDIR"
       }
  }

Congratulations!! We are able to configure various Chrome Options in Serenity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s