Overview
In this tutorial, we will create some tests in TestNG in order to validate a simple browser interaction using Sauce Labs for cloud testing.
Please 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 browsers. Thus, the overall test run status will be affected by the results made for all the browsers.
Instead of this approach, a different one could be creating a Test Execution per each browser; 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 Java
- Install all dependencies using "mvn"
Description
This tutorial is based on Sauce Labs's own tutorial for TestNG.
You may start by cloning the repository https://github.com/saucelabs-sample-test-frameworks/Java-TestNG-Selenium.
git clone https://github.com/saucelabs-sample-test-frameworks/Java-TestNG-Selenium
There are two tests, which extend a TestBase helper class where the target browsers/capabilities are enumerated along with the logic for obtaining the Sauce Labs credentials.
The tests use the Page Objects pattern, implemented by the following class.
Test 1: Verify the link works on the "GuineaPigPage"
package com.yourcompany.Tests; import com.yourcompany.Pages.GuineaPigPage; import org.openqa.selenium.InvalidElementStateException; import org.openqa.selenium.WebDriver; import org.testng.Assert; import org.testng.annotations.Test; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.rmi.UnexpectedException; /** * Created by mehmetgerceker on 12/7/15. */ public class FollowLinkTest extends TestBase { /** * Runs a simple test verifying link can be followed. * * @throws InvalidElementStateException */ @Test(dataProvider = "hardCodedBrowsers") public void verifyLinkTest(String browser, String version, String os, Method method) throws MalformedURLException, InvalidElementStateException, UnexpectedException { //create webdriver session this.createDriver(browser, version, os, method.getName()); WebDriver driver = this.getWebDriver(); this.annotate("Visiting GuineaPig page..."); GuineaPigPage page = GuineaPigPage.visitPage(driver); this.annotate("Clicking on link..."); page.followLink(); this.annotate("Asserting that we are on a new page..."); Assert.assertFalse(page.isOnPage()); } }
Test 2: Verify comments can be added on the "GuineaPigPage"
package com.yourcompany.Tests; import com.yourcompany.Pages.GuineaPigPage; import org.openqa.selenium.InvalidElementStateException; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.testng.Assert; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.rmi.UnexpectedException; import java.util.UUID; /** * Created by mehmetgerceker on 12/7/15. */ public class TextInputTest extends TestBase { /** * Runs a simple test verifying if the comment input is functional. * @throws InvalidElementStateException */ @org.testng.annotations.Test(dataProvider = "hardCodedBrowsers") public void verifyCommentInputTest(String browser, String version, String os, Method method) throws MalformedURLException, InvalidElementStateException, UnexpectedException { this.createDriver(browser, version, os, method.getName()); WebDriver driver = this.getWebDriver(); String commentInputText = UUID.randomUUID().toString(); this.annotate("Visiting GuineaPig page..."); GuineaPigPage page = GuineaPigPage.visitPage(driver); this.annotate(String.format("Submitting comment: \"%s\"", commentInputText)); page.submitComment(commentInputText); this.annotate(String.format("Asserting submitted comment is: \"%s\"", commentInputText)); Assert.assertTrue(page.getSubmittedCommentText().contains(commentInputText)); } }
Please 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.
Before running the tests themselves, you need to export some environment variables with your Sauce Lab's username along with the respective access key, which you can obtain from within the User Settings section in your Sauce Lab's profile page.
export SAUCE_USERNAME=<your Sauce Labs username> export SAUCE_ACCESS_KEY=<your Sauce Labs access key>
Test(s) then can be run in parallel using Maven's "test" task.
mvn test
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).
curl -H "Content-Type: multipart/form-data" -u user:password -F "file=@target/surefire-reports/testng-results.xml" "https://yourjiraserver/rest/raven/1.0/import/execution/testng?projectKey=CALC"
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 on a "browser" basis.
For each browser, a different "context" will appear along with the respective result.
In Sauce Labs you can see some info about it.