Run Selenium tests remotely

This topic 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. For details, see:

    If you want to add Applitools visual testing steps to your test, see Add Applitools code to your Selenium script.

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

Back to top

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();

Back to top

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:

JavaClosed

JavaScriptClosed

RubyClosed

Run JavaScript Selenium tests with enhanced reports

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

  1. Install the reporter from one of the following locations:

  2. 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. For details, see below.

  3. Add and define the following environment variables:

    SRF client ID and secret

    • SRF_CLIENT_ID

    • SRF_CLIENT_SECRET

    Proxy server

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

    • HTTP_PROXY
    • HTTPS_PROXY
  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 experience.center.mocha.reporter

    Using the srfURL reporter option

    For example:

    mocha test-name.js --reporter experience.center.mocha.reporter --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:

Run your Ruby test remotely from the command line

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

    Capybara.default_driver = :srf_driver

  2. 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.

    For details, see Manage remote access.

    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.

    For example:

    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:

    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.

    For details, see Manage remote access.

    For example:

    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.

    • Use the file with dependencies if you want all libraries downloaded together.
    • 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.

Back to top

Selenium code samples for remote testing

The following examples open the Advantage Online Shopping site and perform a search. They then check a price and add an item to the cart, check out, and pay for the item. During payment phase, the examples check the total cost against the verified price.

  • To use the following examples, first launch the Selenium remote testing demo script.

  • These examples include template code for proxy settings, in case you are working behind a firewall. In such cases, include the code and add your proxy access details.

See Run Selenium tests remotely for details about how to define the capabilities you would like to use as your test environment.

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

JavaClosed

JavaScriptClosed

C#Closed


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("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


import com.hpe.srf.util.MyHttpClientFactory;
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 java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

public class SeleniumTest {
 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" ,"<placeholder>");
   capability.setCapability("platform" , "windows 7");

   URL srfGatewayUrl = new URL("https","ftaas.saas.hpe.com", 443,"/wd/hub/");
   
   System.out.println("Creating remote web driver with address: " + srfGatewayUrl);

   String proxyHost = "<YourProxyHostURL>";int proxyPort = <YourProxyPort>;

   HttpClientBuilder builder = HttpClientBuilder.create();
   HttpHost driverProxy = new HttpHost(proxyHost, proxyPort);

   builder.setProxy(driverProxy);

   HttpClient.Factory factory = new MyHttpClientFactory(builder);
   HttpCommandExecutor executor = new HttpCommandExecutor(new HashMap<String, CommandInfo>(), srfGatewayUrl, factory);


   driver = new RemoteWebDriver(executor, capability);}
 @After
 public void closeBrowser() throws IOException      {
  driver.quit();
 }
 @Testpublic 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:

  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: "<placeholder>"
   };
   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();
  }
 }
}	

Back to top

See also: