Page History
Overview
In this tutorial, we will create a test in Cucumber for Ruby TestNG in order to validate a simple mobile application browser interaction using Appium and BrowserStack for cloud testing.
The test (specification) is initially created in Jira as a Cucumber Test complemented with a Pre-Condition; later on it is exported using the UI or the REST API and run in BrowserStack mobile devices.
Info | ||
---|---|---|
| ||
Within this tutorial, only one Test Execution will be used; it will contain one Test Run with all the results for the different used devicesbrowsers. Thus, the overall test run status will be affected by the results made for all the devicesbrowsers. Instead of this approach, a different one could be creating a Test Execution per each device. The steps would need to be slightly different. namelly the submission process would need to use the standard or multipart Cucumber REST API endpoints, for each result file corresponding to each devicebrowser; this would require some adaptions in order to produce a XML report per each used browser. This approach would give the ability to take advantage of Test Environments (more info in Working with Test Environments). |
Requirements
- Install Ruby (or JRuby)Java
- Install install all dependencies using "bundle install", on the "android" sub-foldermvn"
Description
This tutorial is based on BrowserStack's own tutorial for Appium/Cucumber/RubyTestNG.
You may start by cloning the repository repository https://github.com/browserstack/cucumber-ruby-appium-app-testng-browserstack .
No Format |
---|
git clone https://github.com/browserstack/cucumber-ruby-appium-apptestng-browserstack cd android |
We'll use the "android" example folder as basis and the "parallel" task that runs the tests in parallel.
We have to make some changes in order to make Cucumber generate a distinct JSON report per each device.
Rakefile was "hacked" in order to process the devices configured for the "parallel" task and related configuration file (i.e. config/parallel.config.yml).
The number of parallel jobs must be equal to the number of configured devices.
The test, which validates a simple search using Google, is implemented in a SingleTest class which extends BrowserStackTestNGTest helper class. The later provides setup related logic, including loading BrowserStack credentials from a configuraiton file.
Code Block | ||||
---|---|---|---|---|
| ||||
package com.browserstack;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SingleTest extends BrowserStackTestNGTest {
@Test
public void test() throws Exception { | ||||
Code Block | ||||
| ||||
require 'rake' require 'parallel' require 'cucumber/rake/task' require 'yaml' Cucumber::Rake::Task.new(:cukesingle) do |task| ENV['CONFIG_NAME'] ||= "single" task.cucumber_opts = ["--format=pretty -f json -o results.json", 'features/single.feature'] end task :default => :single Cucumber::Rake::Task.new(:local) do |task| task.cucumber_opts = ["--format=pretty -f json -o results.json", 'features/local.feature', 'CONFIG_NAME=local'] end task :single, [:device] do |task,args| device = (args[:device] || "").gsub(' ','_') cuke_task = Cucumber::Rake::Task.new cuke_task.cucumber_opts = ["--format=pretty -f json -o device_#{device}.json", 'features/single.feature']driver.get("https://www.google.com/ncr"); cuke_task.runner.run end task :parallel do |t, args| @num_parallel = 2 Parallel.map([*1..@num_parallel], :in_processes => @num_parallel) do |task_id| ENV["TASK_ID"] = (task_id - 1).to_s ENV['name'] = "parallel_test" ENV['CONFIG_NAME'] = "parallel" WebElement element = driver.findElement(By.name("q")); element.sendKeys("BrowserStack"); TASK_ID = (ENV['TASK_ID'] || 0).to_ielement.submit(); CONFIG_NAME = ENV['CONFIG_NAME']Thread.sleep(5000); CONFIG = YAML.load(File.read(File.join(File.dirname(__FILE__), "./config/#{CONFIG_NAME}.config.yml"))) caps = CONFIG['browser_caps'][(task_id-1)] ENV['DEVICE'] = caps['device'] Rake::Task["single"].invoke(caps['device']) Rake::Task["single"].reenable end end task :test do |t, args| Rake::Task["single"].invoke Rake::Task["single"].reenable Rake::Task["local"].invoke Rake::Task["parallel"].invoke endAssert.assertEquals("BrowserStack - Google Search", driver.getTitle()); } } |
Info | ||
---|---|---|
| ||
If you wish to map the test method to an existing Test issue, or to create links to requirements whenever importing the results, please have a look at the tutorial Testing using TestNG in Java which provides the necessary instructions in order to setup TestNG for this purpose. |
You need to configure the BrowserStack user/key along with desired browser capabilities/devices. We need to update the config/single.conf.json
and config/parallel.conf.json
files.
Code Block | ||||
---|---|---|---|---|
| ||||
{ "server": "hub-cloud.browserstack.com", "user": "youruser<user>", "key": "yourkey<key>", common_caps: "buildcapabilities": "cucumber-browserstack"{ "browserstack.debug": true browser_caps: - }, "environments": { "chrome": { "devicebrowser": "Google Pixel""chrome" }, "appfirefox": "bs://6c31566b71e1ee4c5f7f5298c702c0de4c590000"{ "namebrowser": "parallel_testfirefox" -}, "devicesafari": "Google{ Nexus 6" "appbrowser": "bs://6c31566b71e1ee4c5f7f5298c702c0de4c590000"safari" }, "nameie": "parallel_test" |
In this tutorial we're using a wikipedia sample application from BrowserStack, that must be uploaded beforehand to BrowserStack. The hashed app id must be configured accordingly on the previous configuration file.
Instead of using the provided "single.feature" file, we'll use JIRA and Xray as master of information.
In other words, in JIRA we'll:
- create a story
- create a Test for it
- create a Pre-Condition and associate it to the previous Test
Although it's not needed, we will also create a blank Test Execution with the Test and we'll use it as basis in order to run and report our test results.
{
"browser": "internet explorer"
}
}
} |
Test(s) can be run in parallel using the maven Tests can be run by execution rake's "parallel" task.
No Format |
---|
mvn bundletest exec-P rake parallel |
The previous task will generate a Cucumber JSON report per each target device.
No Format |
---|
zip browserstack.zip device_*.json |
These files can be bundled in ZIP file and submitted to Xray using the "bundle" REST API endpoint (either by invoking the REST API directly or by using one of the free add-ons for Jenkins/Bamboo).
...
title | Example for submission of results using "curl" |
---|
...
parallel |
After successfully running the tests and generating the TestNG XML report (e.g. testng-results.xml), it can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution).
TestNG's tests are mapped to Generic Tests in Jira, and the Generic Test Definition field contains the namespace, the name of the class, and the method name that implements the Test case.
...
The execution screen details will not only provide information on the overall test run result, but also for each stepon a "browser" basis.
For each devicebrowser, a different "context" will appear along with the respective step resultsresult.
In BrowserStack you can see some info about it.