Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
titlePlease note

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. The later provides setup related logic, including loading BrowserStack credentials from a configuraiton file.

Code Block
languagejava
titlesrc/test/java/com/browserstack/SingleTest.java
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
titleRakefile
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_sWebElement element = driver.findElement(By.name("q"));
    ENV['name'] = "parallel_test"
    ENV['CONFIG_NAME'] = "parallel"
  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
titlePlease note

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
languagejs
titleconfig/parallel.configconf.ymljson
{
  "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://6c31566b71e1ee4c5f7f5298c702c0de4c590000safari"
    },
    "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:

  1. create a story
  2. create a Test for it
  3. 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.

Image Removed

Image Removed   Image Removed

Tests can be run by execution rake's "parallel" task.

No Format
 bundle exec 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).

 {
      "browser": "internet explorer"
    }
  }
}


Test(s) can be run in parallel using the maven "parallel" task.

No Format
mvn test -P parallel
Code Block
titleExample for submission of results using "curl"
curl -H "Content-Type: multipart/form-data" -u user:password -F "file=@browserstack.zip" https://sandbox.xpand-addons.com/rest/raven/1.0/import/execution/bundle





The execution screen details will not only provide information on the test run result, but also for each step.

...

In BrowserStack you can see some info about it.

Image Removed


References

...