Run Selenium tests remotely using StormRunner Functional

This article describes how to run Selenium tests in SRF, remotely from your remote web driver.

SRF supports remote testing from Selenium in all languages supported by Selenium, including Java, C#, JavaScript, Python, Ruby, and more.

Run Selenium tests remotely: basic steps

Run your Selenium tests from your remote web driver as follows:

1. Add code to your tests to access SRF and to define your environment.

2. Add capability code to your tests to define SRF such as test tags, or the names displayed in test results, as well as environment settings.

3. Run your Selenium tests remotely in SRF using your remote web driver as you would other Selenium tests.

Run Selenium jUnit tests in Firefox

If you are running your jUnit test in Firefox, remotely in SRF, use the following firefox profile API:

FirefoxProfile firefoxProfile = new FirefoxProfile();

Run Selenium tests with enhanced reports

SRF reporters are available for several types of Selenium tests.

Select an option to modify the information displayed on this page:

Run JavaScript Selenium tests with enhanced reports

To run a JavaScript Selenium test with enhanced reports using SRF’s reporter, do the following:

a. Install the reporter: https://www.npmjs.com/package/srf.reporter.mocha

b. Add the SRF_REPORTER_URL environment variable, and its value with the URL to your SRF tenant.

For example, https://ftaas.saas.hpe.com, or https://ftaas-eu1.saas.hpe.com, depending on your tenant region.

Tip: Alternately, pass the srfUrl reporter option from the command line.

3. Add and define the following environment variables:

SRF client ID and secret o SRF_CLIENT_ID

o SRF_CLIENT_SECRET

Proxy server If you are running the reporter from behind a firewall, define the following:

o HTTP_PROXY

o HTTPS_PROXY

Note: When running your test, be sure to assign the client secret value to the SRF_CLIENT_SECRETenvironment variable.

4. Use one of the following commands when running your test:

Using the SRF_REPORTER_URL environment variable

For example:

mocha test-name.js –reporter srf.reporter.mocha

Using the srfURL reporter option

For example:

mocha test-name.js –reporter srf.reporter.mocha –reporter-options srfUrl=https://ftaas.saas.hpe.com

Run Ruby tests remotely with enhanced reports

To use this feature, contact customer support to obtain the Ruby Selenium reporter.

Then, do the following to run a Ruby Selenium tests with enhanced reports:

1. Install the SRF reporter by running the following command:

gem install srfrubyreporter

2. Open and edit your Gemfile, and add the following line:

gem ‘srfreport’

3. Open and edit your *.rb file, and add the following line:

require ‘srfreport’

4. Continue with one of the following options:

o Run your Ruby test remotely from the command line

o Run your Ruby test using a remote driver

Run your Ruby test remotely from the command line

a. Add the following line to your *.rb file:

Capybara.default_driver = :srf_driver

b. Run your test from the command line with the following parameters, or define these as environment variables on your machine.

Name Value
SRF_REPORTER_URL The URL to your SRF tenant.

For example: https://ftaas.hpe.com or https://ftaas-eu1.saas.hpe.com/wd/hub, depending on your region.

SRF_CLIENT_ID

SRF_CLIENT_SECRET

Your SRF client ID and secret values.
TEST_NAME The path to your test and your test name.

For example: “MyAdvantageRubyTest”

OS_NAME The operating system you want to use to run your test.
OS_VERSION The operating system version you want to use to run your test.
BROWSER_NAME The name of the browser you want to use to run your test.
BROWSER_VERSION The browser version you want to use to run your test.

c. For example:

d. bundle exec cucumber SRF_REPORTER_URL= https://ftaas.saas.hpe.com SRF_CLIENT_ID=<placeholder> SRF_CLIENT_SECRET=<placeholder> TEST_NAME=”MyAdvantageRubyTest” BROWSER_NAME=chrome BROWSER_VERSION=65 OS_NAME=Windows OS_VERSION=10

Run your Ruby test using a remote driver

Run your Ruby test remotely using your own remote driver as follows:

1. Edit your test to include the following:

o Capability code as needed.

o The SRF_CLIENT_ID and SRF_CLIENT_SECRET values as environment variables.

For example:

Capybara.register_driver :my_driver do |app|
 caps = Selenium::WebDriver::Remote::Capabilities.chrome(
 :platform => "Windows 10",
 :testName => "MyAdvantageRubyTest",
 :browser_name =>"chrome",
 :version => "65",
 :SRF_CLIENT_SECRET => ENV['<placeholder>'],
 :SRF_CLIENT_ID => ENV['<placeholder>']
 )
 driver = Capybara::Selenium::Driver.new(app, :browser => :remote, :url => ENV['SRF_REPORTER_URL'] + "/wd/hub", :desired_capabilities => caps)
 driver
end
Capybara.default_driver = :my_driver

2. Run your test from the command line, including the following parameters:

SRF_REPORTER_URL The URL to your SRF tenant.

For example: https://ftaas.hpe.com or https://ftaas-eu1.saas.hpe.com, depending on your tenant region.

SRF_CLIENT_ID

SRF_CLIENT_SECRET

Your SRF client ID and secret values.

3. For example:

4. bundle exec cucumber SRF_REPORTER_URL= https://ftaas.saas.hpe.com SRF_CLIENT_ID=YOUR_CLIENT_ID SRF_CLIENT_SECRET=YOUR_CLIENT_SECRET

Run Java tests remotely with enhanced reports

To use this feature, contact customer support to obtain the Java Selenium reporter (srfRunner.jar).

Then, do the following to run a Java Selenium tests with enhanced reports:

1. Save the srfRunner.jar file on the computer where you are running your test.

o Use the file with dependencies if you want all libraries downloaded together.

o Use the file without dependencies if you already have required libraries stored locally, or can access them ad-hoc when running your test.

2. Update your test to define the relevant classes, options, and variables.

3. Run your Java test as you would otherwise.

Selenium code samples for remote testing

Selenium Java code sample

Prerequisites. Before running these code samples, replace the sample SRF_CLIENT_ID and SRF_CLIENT_SECRET values with your own SRF client secret values. If needed, update the SRF URL value to match that of your tenant region.

Sample without proxy

package DEMO.Selenium;
import static org.junit.Assert.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
publicclassOnlineShopping
{
 private static RemoteWebDriver driver;
 static DesiredCapabilities capabilities = DesiredCapabilities.chrome();

@BeforeClass
public static void openBrowser() throws MalformedURLException{
 capabilities.setVersion("latest");
 capabilities.setCapability("platform", "Windows 8.1");
 capabilities.setCapability("testName","Selenium Burst JAVA - Online Shopping");
 capabilities.setCapability("SRF_CLIENT_ID", "<placeholder>");
 capabilities.setCapability("SRF_CLIENT_SECRET", System.getenv("SRF CLIENT SECRET"));
 capabilities.setCapability("resolution", "800x600");

 driver = new RemoteWebDriver(
 new URL("https://ftaas.saas.hpe.com/wd/hub/"), capabilities);
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}

@Test
public void OnlineShoppingE2E() throws InterruptedException{
 Actions builder = new Actions(driver);
 driver.get("http://advantageonlineshopping.com/#");
 driver.findElement(By.xpath("//DIV[@id=\"mobileSearch\"]/INPUT[1]")).sendKeys("speakers"); //Search edit
 driver.findElement(By.cssSelector("#mobileSearch > #Layer_1")).click(); //Search button
 WebElement addToCartElement = driver.findElement(By.linkText("Bose Soundlink Bluetooth Speaker III"));
 builder.click(addToCartElement).build().perform();
 driver.findElement(By.xpath("//DIV[@id=\"productProperties\"]/DIV/BUTTON[normalize-space()=\"ADD TO CART\"]")).click(); //Add to cart button
 driver.findElement(By.xpath("//HEADER[1]/NAV[1]/UL[1]/LI[1]/A[1]")).click(); //Cart Icon
 assertEquals(driver.findElement(By.cssSelector("div#shoppingCart > table > tfoot > tr:nth-child(1) > td:nth-child(2) > span:nth-child(2)")).getText(), "$269.99"); //Checking item price
}

@AfterClass
public static void closeBrowser(){
 driver.quit();
}
}

Sample with proxy

package com.microfocus.srf.pock;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.internal.ApacheHttpClient;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
importjava.util.HashMap;

public class FinalDocumentationExample
 {private WebDriver driver;
 @Before
 public void openBrowser() throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.chrome();
 capability.setCapability("version","latest");
 capability.setCapability("SRF_CLIENT_ID","<placeholder>");
 capability.setCapability("SRF_CLIENT_SECRET", System.getenv("SRF CLIENT SECRET"));
 capability.setCapability("platform","windows 7");
 URL srfUrl =new URL("https","ftaas.saas.hpe.com",443,"/wd/hub/");

 System.out.println("Creating remote web driver with address: "+ srfUrl);

 HttpCommandExecutor executor =new HttpCommandExecutor(new HashMap<String, CommandInfo>(), srfUrl,new HttpClient.Factory()
 public HttpClient createClient(URL url){
 HttpClientBuilder builder = HttpClientBuilder.create();
 HttpHost driverProxy =new HttpHost("<proxy host placeholder>",<proxy port placeholder>);
 builder.setProxy(driverProxy);
 return new ApacheHttpClient(builder.build(), url);
 }
 });
 driver =new RemoteWebDriver(executor, capability);
}
@After
public void closeBrowser() throws IOException {
 driver.quit();
}
@Test
public void pageTitleAfterSearchShouldBeginWithDrupal() throws IOException, InterruptedException {
 driver.manage().window().maximize();
 driver.get("http://www.google.com/");//type search query
 driver.findElement(By.name("q")).sendKeys("qa automation\n");// click search
 driver.findElement(By.name("btnG")).click();}}

Selenium JavaScript code sample

Prerequisites. Before running this code sample:

1. Install the following components on your machine:

o Node JS (https://nodejs.org)

o Mocha (https://www.npmjs.com/package/mocha)

· Selenium-webdriver (https://www.npmjs.com/package/selenium-webdriver)

2. Replace the sample SRF_CLIENT_ID and SRF_CLIENT_SECRET values with your own SRF client values.

3. If needed, update the SRF URL value to match that of your tenant region.

4. If you are working behind a firewall, uncomment the proxy code and add your proxy access details.

var webdriver = require('selenium-webdriver'),
proxy = require('selenium-webdriver/proxy');
By = webdriver.By,
until = webdriver.until;
var assert = require('selenium-webdriver/testing/assert');
var driver;
describe('DEMO Online Shopping Selenium', function () {
 this.timeout(60000);
 before(function(done){
 var capabilities = {
 platform: "Windows 8.1",
 browserName: "chrome",
 //version: "58",//Instead of specific version number you can specify latest version:
 version: "latest",
 testName: "Demo_Burst_Test_Selenium_JS",
 resolution: "800x600",
 //NOTE: Web AUT which is tested in this test changes it's UI elements dependents on resolution, in case if you will use higher resolution test will fail//Supported resolutions are: 800x600; 1024X768; 1280X1024
 SRF_CLIENT_ID: "<placeholder>",
 SRF_CLIENT_SECRET: process.env.SRF_CLIENT_SECRET
 };
 driver = new webdriver
 .Builder()
 .withCapabilities(capabilities)
 .usingServer("https://ftaas.saas.hpe.com/wd/hub/")
 //.setProxy(proxy.manual({""}))
 .build();
 driver.get('http://advantageonlineshopping.com/#').then(function() {
 console.log("open Online Shopping web page");
 done();
 });
 });

 it('should search required category', function (done) {
 driver
 .wait(until.elementLocated(By.xpath("//DIV[@id=\"mobileSearch\"]/INPUT[1]"), 5000))
 .then(function(searchFieldElement){
 searchFieldElement.sendKeys("speakers");
 })
 .then(()=>driver.findElement(By.css("#mobileSearch > #Layer_1")).click())
 .then(done);
});

it('should click on "Bose Soundlink Bluetooth Speaker III" in list of founded items', function (done) {
 var element = driver.findElement({partialLinkText:"Bose Soundlink Bluetooth Speaker III", tagName:'LI'});
 driver
 .actions()
 .click(element)
 .perform();
 done();
});

it('should add item into the cart',function(done){
 driver
 .wait(until.elementLocated(By.xpath("//DIV[@id=\"productProperties\"]/DIV/BUTTON[normalize-space()=\"ADD TO CART\"]"), 5000))
 .then(function(){
 driver
 .findElement(By.xpath("//DIV[@id=\"productProperties\"]/DIV/BUTTON[normalize-space()=\"ADD TO CART\"]"))
 .click()
 .then(done);
 });
});

it('should click on cart icon',function(done){
 driver
 .findElement(By.xpath("//HEADER[1]/NAV[1]/UL[1]/LI[1]/A[1]"))
 .click()
 .then(done);
 });

 it('should check item price',function(done){
 var itemPrice = driver.findElement(By.css("div#shoppingCart > table > tfoot > tr:nth-child(1) > td:nth-child(2) > span:nth-child(2)"));
 itemPrice
 .getText()
 .then(function(elementText){
 assert(elementText).equals("$269.99");
 })
 .then(done);
 });

 it('should click on checkout button',function(done){
 driver
 .findElement(By.xpath("//DIV[@id=\"shoppingCart\"]/TABLE[1]/TFOOT[1]/TR[2]/TD[1]/BUTTON[1]"))
 .click()
 .then(done);
 });

 it('should fill existing user details',function(done){
 driver
 .findElement(By.xpath("//DIV[@id=\"orderPayment\"]/DIV[1]/DIV[1]/DIV[1]/SEC-FORM[1]/SEC-VIEW[1]/DIV[1]/LABEL[1]"))
 .click()
 .then(
 driver
 .findElement(By.xpath("//DIV[@id=\"orderPayment\"]/DIV[1]/DIV[1]/DIV[1]/SEC-FORM[1]/SEC-VIEW[1]/DIV[1]/INPUT[1]"))
 .sendKeys("SRF_DEMO")
 .then(
 driver
 .findElement(By.xpath("//DIV[@id=\"orderPayment\"]/DIV[1]/DIV[1]/DIV[1]/SEC-FORM[1]/SEC-VIEW[2]/DIV[1]/LABEL[1]"))
 .click()
 .then(
 driver
 .findElement(By.xpath("//DIV[@id=\"orderPayment\"]/DIV[1]/DIV[1]/DIV[1]/SEC-FORM[1]/SEC-VIEW[2]/DIV[1]/INPUT[1]"))
 .sendKeys("<password>")
 .then(done)
 )));
 });

 it('should click login button',function(done){
 driver
 .findElement(By.xpath("//DIV[@id=\"orderPayment\"]/DIV[1]/DIV[1]/DIV[1]/SEC-FORM[1]/SEC-SENDER[1]/A[1]"))
 .click()
 .then(done);
 });

 it('should check total price',function(done){
 var totalPrice = driver.findElement(By.css("div#userCart > div:nth-child(5) > label:nth-child(2) > span"));
 totalPrice
 .getText()
 .then(function(elementText){
 assert(elementText).equals("$269.99");
 })
 .then(done);
 });

 it('should click next button',function(done){
 driver
 .findElement(By.xpath("//DIV[@id=\"userSection\"]/DIV/DIV/BUTTON[normalize-space()=\"NEXT\"]"))
 .click()
 .then(done);
 });

 it('should specify user details for buying items',function(done){

 driver
 .findElement(By.xpath("//DIV/SEC-FORM/SEC-VIEW/DIV/LABEL[normalize-space()=\"SafePay username\"]"))
 .click()
 .then(
 driver
 .findElement(By.css("div#paymentMethod > div > div:nth-child(3) > sec-form > sec-view:nth-child(1) > div > input"))
 .sendKeys('testTest')
 .then(
 driver
 .findElement(By.xpath("//DIV/SEC-FORM/SEC-VIEW/DIV/LABEL[normalize-space()=\"SafePay password\"]"))
 .click()
 .then(
 driver
 .findElement(By.css("div#paymentMethod > div > div:nth-child(3) > sec-form > sec-view:nth-child(2) > div > input"))
 .sendKeys('testTEST1234')
 .then(done)
 )));
 });

 it('should uncheck checkbox "save cahnges to profile"',function(done){
 driver
 .findElement(By.xpath("//DIV[2]/SEC-FORM[1]/DIV[1]/INPUT[1]"))
 .isSelected()
 .then(function(elementSelectedState){
 if (elementSelectedState)
 driver
 .findElement(By.xpath("//DIV[2]/SEC-FORM[1]/DIV[1]/INPUT[1]"))
 .click()
 .then(done);
 });
 });

 it('should click on "Pay now" button',function(done){
 driver
 .findElement(By.xpath("//DIV[2]/SEC-FORM[1]/DIV[2]/LABEL[1]/SEC-SENDER[1]/A[1]"))
 .click()
 .then(done);
 });

 it('should check price in order details',function(done){

 var totalPrice = driver.findElement(By.css("div#orderPaymentSuccess > div > div:nth-child(3) > div:nth-child(4) > label > a"));
 totalPrice
 .getText()
 .then(function(elementText){
 assert(elementText).equals("$269.99");
 })
 .then(done);
 });

 after(function(done){
 driver.quit().then(done);
 });
});

Selenium C# code sample

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Configuration;
using System;
using OpenQA.Selenium.Remote;
using System.Threading;
using OpenQA.Selenium.Interactions;
using NUnit.Framework;
namespace SeleniumExample
{
 public static class WebElementExtensions
 {
 public static bool ElementIsPresent(this IWebDriver driver, By by)
 {
 var present = false;
 try
 {
 present = driver.FindElement(by).Displayed;
 }
 catch (NoSuchElementException)
 {
 }
 return present;
 }
 }
 [TestFixture, Category("Selenium_C#")]public class DEMO_Online_Shopping
 {
 DesiredCapabilities dc;
 RemoteWebDriver driver;
 [OneTimeSetUp]
 public void Init()
 {
 dc = DesiredCapabilities.Chrome();
 dc.SetCapability("platform", "Windows 8.1");
 dc.SetCapability("testName", "Selenium Burst C# - Online Shopping");
 dc.SetCapability(CapabilityType.Version, Environment.GetEnvironmentVariable("SRF_Browser_Version"));
 dc.SetCapability("resolution", "800x600");
 dc.SetCapability("SRF_CLIENT_ID", "<placeholder>");
 dc.SetCapability("SRF_CLIENT_SECRET", "<placeholder>");
 driver = new RemoteWebDriver(new Uri("https://ftaas.saas.hpe.com/wd/hub/"), dc);
  //driver = new ChromeDriver();
 }
 [Test]public void Demo_Online_Shopping()
 {
 Actions builder = new Actions(driver);
 driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 0, 10)); // 10 seconds for searching objects
 driver.Navigate().GoToUrl("http://advantageonlineshopping.com/#");
 driver.FindElement(By.XPath("//DIV[@id=\"mobileSearch\"]/INPUT[1]")).SendKeys("speakers");  //Search edit
 driver.FindElement(By.CssSelector("#mobileSearch > #Layer_1")).Click(); //Search button
 IWebElement addToCartElement = driver.FindElement(By.LinkText("Bose Soundlink Bluetooth Speaker III"));
 builder.Click(addToCartElement).Build().Perform();
 driver.FindElement(By.XPath("//DIV[@id=\"productProperties\"]/DIV/BUTTON[normalize-space()=\"ADD TO CART\"]")).Click(); //Add to cart button
 driver.FindElement(By.XPath("//HEADER[1]/NAV[1]/UL[1]/LI[1]/A[1]")).Click(); //Cart Icon
 Assert.AreEqual(driver.FindElement(By.CssSelector("div#shoppingCart > table > tfoot > tr:nth-child(1) > td:nth-child(2) > span:nth-child(2)")).Text, "$269.99"); //Checking item price
 }
 [OneTimeTearDown]public void TearDown()
 {
 driver.Quit();
 }
 }
}
Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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