Selenium with Serenity
Your guide to run Selenium Webdriver tests with Serenity on BrowserStack.
Introduction
BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with Serenity on BrowserStack is simple. This guide will help you:
- Run your first test
- Understand your tests with BrowserStack
- Integrate your tests with BrowserStack
- Mark tests as passed or failed
- Debug your app
Prerequisites
- You need to have BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
-
Maven is installed on your machine, Maven environment variables are set, and Maven bin is added to the system path
$PATH
. Check out the official website to download the latest version of Maven. - Git is installed on your machine.
Run your first test
- Edit or add capabilities in the W3C format using our W3C capability generator.
-
Add the
seleniumVersion
capability in your test script and set the value to4.0.0
.
To run Selenium tests with Serenity on BrowserStack Automate, follow the below steps:
- Clone the serenity-browserstack repository using the following command:
git clone https://github.com/browserstack/serenity-browserstack.git cd serenity-browserstack
- Verify that the following Serenity libraries are included in the
pom.xml
of the cloned repository:pom.xml<dependency> <groupId>net.serenity-bdd</groupId> <artifactId>serenity-core</artifactId> <version>3.2.1</version> </dependency>
- Run the following command to install the dependencies:
mvn install
-
Set your BrowserStack Username and Access Key in the
serenity.properties
file, inside theserenity-browserstack
directory, as follows:
This file also contains the capabilities that define the browsers to run the test.serenity-browserstack/serenity.propertieswebdriver.driver = provided webdriver.provided.type = mydriver webdriver.provided.mydriver = com.browserstack.BrowserStackSerenityDriver serenity.driver.capabilities = mydriver webdriver.timeouts.implicitlywait = 5000 serenity.use.unique.browser = false serenity.dry.run=false serenity.take.screenshots=AFTER_EACH_STEP browserstack.user=YOUR_USERNAME browserstack.key=YOUR_ACCESS_KEY browserstack.server=hub.browserstack.com #You can add any capability with a prefix 'bstack_' as below #For example to use browserstack.console as verbose use below capability bstack_build=browserstack-build-1 bstack_debug=true bstack_browserstack.console=verbose #You can add any capability with a prefix 'environment.{environment}.' as below #For example to use browser_version as 68 use below capability #Check valid capabilities here - https://www.browserstack.com/automate/capabilities environment.single.name=serenity_single_test environment.single.browser=chrome # environment.single.browser_version=68 environment.local.name=serenity_local_test environment.local.browser=chrome environment.local.browserstack.local=true environment.parallel_1.name=serenity_parallel_test environment.parallel_1.browser=chrome environment.parallel_2.name=serenity_parallel_test environment.parallel_2.browser=firefox environment.parallel_3.name=serenity_parallel_test environment.parallel_3.browser=safari # Similarly, other custom capabilities can be added for other environments also
- Verify the parallel test cases are written for running in parallel with Serenity within
serenity-browserstack/src/test/java/com/browserstack/cucumber/
directory. The following files for each environment already exist in the sample repository.ParallelChromeTest.java@RunWith(CucumberWithSerenity.class) @CucumberOptions(features="src/test/resources/features/single.feature") public class ParallelChromeTest extends BrowserStackSerenityTest { }
ParallelFirefoxTest.java@RunWith(CucumberWithSerenity.class) @CucumberOptions(features="src/test/resources/features/single.feature") public class ParallelFirefoxTest extends BrowserStackSerenityTest { }
- Run your first test using the following command::
mvn verify -P parallel
- Navigate to your Automate Dashboard and view the tests running in parallel on multiple browsers.
- You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.
- You have to pre-pend
bstack_
with any capability that you choose and enter it in yourserenity.properties
file.
Understand the details of your test
When you run the mvn verify -P parallel
command, the test case written in the single.feature
file is executed on several browsers, namely Chrome, Firefox, and Safari.
When the test is triggered, it:
- Opens the
bstackdemo.com
website. - Adds a product to the cart.
- Verifies whether the product is added to the cart.
- Marks the test as passed or failed based on whether the product is available in the cart.
Feature: BrowserStack Demo
Scenario: Add product to cart
Given I am on the website 'https://www.bstackdemo.com'
When I select a product and click on 'Add to cart' button
Then the product should be added to cart
The BStackDemoPage.java
file inside the /src/test/java/com/browserstack/cucumber/pages
directory defines the test functions. These test functions are used in the test script file.
@DefaultUrl("https://www.bstackdemo.com")
public class BStackDemoPage extends PageObject {
private String selectedProductName;
public BStackDemoPage() {
this.selectedProductName = "";
}
@FindBy(xpath = "//*[@id=\"1\"]/p")
WebElementFacade firstProductName;
@FindBy(xpath = "//*[@id=\"1\"]/div[4]")
WebElementFacade firstProductAddToCartButton;
@FindBy(css = ".float-cart__content")
WebElementFacade cartPane;
@FindBy(xpath = "//*[@id=\"__next\"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]")
WebElementFacade productCartText;
public void setSelectedProductName(String selectedProductName) {
this.selectedProductName = selectedProductName;
}
public String getSelectedProductName() {
return selectedProductName;
}
public void selectFirstProductName() {
String firstProduct = firstProductName.getText();
setSelectedProductName(firstProduct);
}
public void clickAddToCartButton() {
firstProductAddToCartButton.click();
}
public void waitForCartToOpen() {
element(cartPane).waitUntilVisible();
}
public String getProductCartText() {
return productCartText.getText();
}
}
The BStackDemoSteps.java
file inside the /src/test/java/com/browserstack/cucumber/steps/
directory defines the test script. It uses the functions defined in the BStackDemoPage.java
file.
import com.browserstack.cucumber.pages.BStackDemoPage;
public class BStackDemoSteps {
BStackDemoPage demoPage;
@Given("^I am on the website '(.+)'$")
public void I_am_on_the_website(String url) throws Throwable {
demoPage.open();
Thread.sleep(2000);
}
@When("^I select a product and click on 'Add to cart' button")
public void I_select_a_product_and_add_to_cart() throws Throwable {
demoPage.selectFirstProductName();
demoPage.clickAddToCartButton();
Thread.sleep(2000);
}
@Then("the product should be added to cart")
public void product_should_be_added_to_cart() {
demoPage.waitForCartToOpen();
assertEquals(demoPage.getSelectedProductName(), demoPage.getProductCartText());
}
}
Integrate your tests with BrowserStack
Your Serenity test case has been integrated to run on BrowserStack using the snippet shown below. A class has been defined which contains the methods to configure and create the connection with BrowserStack. You can find the script in the Serenity BrowserStack sample repository. The module reads from serenity.properties
file where you need to put the BrowserStack Hub URL and credentials.
package com.browserstack;
import com.browserstack.local.Local;
import java.util.Map;
import java.util.HashMap;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.util.SystemEnvironmentVariables;
public class BrowserStackSerenityTest {
static Local bsLocal;
@BeforeClass
public static void setUp() throws Exception {
EnvironmentVariables environmentVariables = SystemEnvironmentVariables.createEnvironmentVariables();
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
if (accessKey == null) {
accessKey = (String) environmentVariables.getProperty("browserstack.key");
}
String environment = System.getProperty("environment");
String key = "bstack_browserstack.local";
boolean is_local = environmentVariables.getProperty(key) != null
&& environmentVariables.getProperty(key).equals("true");
if (environment != null && !is_local) {
key = "environment." + environment + ".browserstack.local";
is_local = environmentVariables.getProperty(key) != null
&& environmentVariables.getProperty(key).equals("true");
}
if (is_local) {
bsLocal = new Local();
Map<String, String> bsLocalArgs = new HashMap<String, String>();
bsLocalArgs.put("key", accessKey);
bsLocal.start(bsLocalArgs);
}
}
@AfterClass
public static void tearDown() throws Exception {
if (bsLocal != null) {
bsLocal.stop();
}
}
}
BROWSERSTACK_USERNAME
and BROWSERSTACK_ACCESS_KEY
with your credentials and they would be read by the file above.
Mark tests as passed or failed
BrowserStack provides a comprehensive REST API to access and update information about your tests. Shown below is a sample code snippet which allows you to mark your tests as pass or fail based on the assertions in your Serenity test cases.
// Mark test as pass / fail
public static void mark() throws URISyntaxException, UnsupportedEncodingException, IOException {
URI uri = new URI("https://YOUR_USERNAME:YOUR_ACCESS_KEY@api.browserstack.com/automate/sessions/<session-id>.json");
HttpPut putRequest = new HttpPut(uri);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add((new BasicNameValuePair("status", "<passed/failed>")));
nameValuePairs.add((new BasicNameValuePair("reason", "The string reason for pass/fail goes here")));
putRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClientBuilder.create().build().execute(putRequest);
}
You can find the full reference to our REST API.
Debug your app
BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.
Text Logs
Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.
Visual Logs
Visual Logs automatically capture the screenshots generated at every Selenium command run through your Serenity tests. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.
Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug
capability to true
in your serenity.properties
file:
bstack_browserstack.debug=true
Sample Visual Logs from Automate Dashboard:
Video Recording
Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.
browserstack.video
capability to false
.
# set this in your serenity.properties file
bstack_browserstack.video = false
In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. You can find the complete details to enable all the debugging options
Next steps
Once you have successfully run your first test on BrowserStack, you might want to do one of the following:
- Run multiple tests in parallel to speed up builds
- Test privately hosted websites
- Generate a list of capabilities that you want to use in tests
- Find information about your Projects, Builds and Sessions using our REST APIs
- Set up your CI/CD: Jenkins, Bamboo, TeamCity, Azure, CircleCI, BitBucket, TravisCI, GitHub Actions
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!