How to upload Selenium tests to StormRunner Functional

SRF enables you to upload Web Selenium tests as SRF assets.

Upload your Selenium test

Before uploading a Selenium test to SRF, ensure that you first define the SRF server, port, client ID, and client secret in your test.

Then, do any of the following, as needed:

Applitools If you want to add Applitools visual testing steps to your test, see Add Applitools code to your Selenium script post.
Ruby If you are uploading a Ruby script to SRF, continue with Prepare a Ruby .zip for upload post.
Packaging multiple scripts To package multiple scripts together, continue with Prepare a Selenium .jar file for upload post.
JUnit tests If your script includes multiple tests in a single JUnit class, SRF runs all tests of the defined class.

If you do not want all of these tests to be run in SRF, comment out the relevant lines as needed before uploading.

When you’re done preparing your test, upload Selenium tests to SRF from the SRF SCRIPTS tab.

Prepare a Selenium .jar file for upload

Package your Selenium scripts as a .jar archive to upload to SRF.

Do the following:

1. Make sure you have the following prerequisites before you start:

Test script Ensure that your script can run remotely in SRF.

Note: We recommend making a copy of your original script in the same location for reference.

Maven Make sure you have Maven installed, and that your project is a maven project.
Descriptor file Add a descriptor.json file in your project’s resources folder.

In this file, define the runnableClass value as the path to the class you want to run, including the fully-qualified class name.

2. descriptor.json file with runnableClass example

2. In your Selenium script, replace your Client ID and Secret values with the following static code. You can comment out the CLIENT_ID property.

capabilities.setCapability("SRF_CLIENT_ID", System.getenv("SRF_CLIENT_ID"));
capabilities.setCapability("SRF_CLIENT_SECRET", System.getenv("SRF_CLIENT_SECRET"));

3. Add the RemoteWebDriver location as follows:

RemoteWebDriver driver = new RemoteWebDriver(new URL(System.getenv("SELENIUM_ADDRESS")), capabilities);

4. Compile your code, including any dependencies, and make sure the hierarchy remains stable.

Example: Compiling a .jar file with dependencies

5. From your IDE or the command line, run a maven clean install command.

Your compiled .jar file will be available for upload in your /target folder.

Continue with Basic upload steps to upload your .jar file to SRF.

Prepare a Ruby .zip for upload

To upload your Ruby test to SRF, you must first package the test as a .zip file.

Do the following:

· Include a Rakefile in your zip, including a default task that describes your test. SRF runs the default task when running your uploaded test.

The Rakefile filename is case-sensitive.

· Optionally, define parameters in your script to test different features in each test run.

For example, if you have a parameter named FEATURE_PATH, add the following code to your test:

t.cucumber_opts = '--format pretty ".\' + ENV["FEATURE_PATH"] + '"'

Then, continue with Basic upload steps to upload your .zip file to SRF.

Note: SRF supports specific frameworks out of the box. If your test requires additional components, they must be included in the zip file you upload.

For example, use the –install-dir flag of a gem command to install the component locally.

Selenium code samples for upload

Use the following code as samples when preparing Selenium tests for upload.

Selenium Java code sample

package com.microfocus.srf;

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.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;

/** 
 * Micro Focus StormRunner Functional Selenium Java upload example
 */
public class SeleniumUpload
 {private static RemoteWebDriver driver;
 @BeforeClasspublic static void configToSRF()throws MalformedURLException{
 DesiredCapabilities capabilities = DesiredCapabilities.chrome();
 capabilities.setCapability("SRF_CLIENT_SECRET", System.getenv("SRF_CLIENT_SECRET"));
 capabilities.setCapability("name","My Uploaded Selenium Example");

 URL SRF_URL = new URL( System.getenv("SELENIUM_ADDRESS"));
 driver = new RemoteWebDriver(SRF_URL, capabilities);
 }

 @Testpublic void test() {
 driver.get("http://www.google.com");// Find the text input element by its name
 WebElement element = driver.findElement(By.name("q"));// Enter something to search for
 element.sendKeys("StormRunner Functional");// Now submit the form. WebDriver will find the form for us from the element
 element.submit();

 }

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

Selenium JavaScript code sample

This example opens the Advantage Online Shopping site and performs a search. It then checks a price and adds an item to the cart, checks out and pays for the item. During payment phase, the example checks the total cost against the verified price.

This example also verifies specific parameter values as defined in SRF, or uses a defined set of default values.

var webdriver = require('selenium-webdriver'),
 By = webdriver.By,
 until = webdriver.until;
var assert = require('selenium-webdriver/testing/assert');

//This example specifies values for input parameters in SRF
//Parameter names should be as shown below

var categoryToFind = process.env.categoryToFind;
var itemToFind = process.env.itemToFind;
var expectedPrice = process.env.expectedPrice;
var driver;

//In case input parameters were not specified in SRF, default values will be used:
 if (categoryToFind==undefined)
 categoryToFind="speakers";
 if (itemToFind==undefined)
 itemToFind="Bose Soundlink Bluetooth Speaker III";

 if (expectedPrice==undefined)
 expectedPrice="$269.99";
describe('DEMO Online Shopping Selenium', function () {
 before(function(done){
 var capabilities = {
 browserName: 'chrome',
 SRF_CLIENT_ID: process.env.SRF_CLIENT_ID,
 SRF_CLIENT_SECRET: process.env.SRF_CLIENT_SECRET
 };
 driver = new webdriver
 .Builder()
 .withCapabilities(capabilities)
 .usingServer(process.env.SELENIUM_ADDRESS)
 .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(categoryToFind);
 })
 .then(()=>driver.findElement(By.css("#mobileSearch > #Layer_1")).click())
 .then(done);
 });

it('should click on ' + itemToFind + ' in list of founded items', function (done) {
 var element = driver.findElement({partialLinkText:itemToFind, 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(expectedPrice);
 })
 .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('SRFdemo1234')
 .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(expectedPrice);
})
 .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(expectedPrice);
 })
 .then(done);
 });

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

Sample Ruby .zip file

The following is an example of the contents of a zip file containing a Ruby test ready for upload:

The files in this example include the following code:

your.feature file

In this example, the your.feature file includes a cucumber test:

Feature: Bing can search
Background:
 Given I am on http://bing.com
Scenario: Search for a term
 Then I will search for "SRF"
 And click the MagnifyGlass
 Then I should see "SRF"

steps.rb file

In this example, the steps.rb file contains the step definitions for the cucumber test:

require "capybara/cucumber"
Given /^I am on (.+)$/ do |url|
 visit (url)
end
Then /^I will search for "(.*?)"$/ do |searchText|
 fill_in 'q', :with => searchText
end
And /^click the MagnifyGlass$/ do
 find(:css,'input[type=submit]').click
end
Then /^I should see "(.*?)"$/ do |searchText|
 expect(page).to have_content(searchText)
end

Rakefile file

In this example, the Rakefile contains a default task, which is executed by SRF when running your test.

require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:default) do |t|
 t.cucumber_opts = '--format pretty ".\features\your.feature"'
end
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