Last updated: Oct 15, 2019 17:23
Before you get started, make sure you have installed the following:
- Selenium WebDriver for Chrome
- An IDE of your choice, such as Eclipse or IntelliJ IDEA
- If you work with Eclipse, TestNG for Eclipse and the Maven plugin
- If you work with IntelliJ IDEA, the Maven plugin
- Maven (download and install)
1 | Get started
The starting point is
LocalSelenium.java, a short Java script with Maven dependencies. The
pom.xml file is institutional here because it holds all configurations and dependencies. In its initial state, the file is very simple.
The script accesses the Perfecto website and verifies the title.
To get started:
- Access the sample project in GitHub and copy the clone URL: https://github.com/PerfectoMobileSA/PerfectoSampleProject
- Open your IDE and check out the project from GitHub.
- Download the OS specific chromedriver into the
libsfolder of the project and update the
- Run the
LocalSelenium.javaproject as TestNG Test.
2 | Configure the script for Perfecto
In this step, we update the
pom.xml file with the required Perfecto dependencies and modify the script from Step 1 to add in security information, the Perfecto cloud name, driver details, Smart Reporting information, and test data. We also want to make sure that the script exits gracefully.
The updated script is called
PerfectoSelenium.java. The following procedure walks you through the configuration.
Expand a step to view its content.
Copy the dependencies in the
pom.xml file and paste them into the
pom.xml file of your own project. This step is essential because the imports in our final script only work when the dependencies specified in this file are available.
Generate your security token through the Perfecto UI. Then define your security token as a system variable called
securityToken. The script uses this variable to retrieve the security token:
This is the recommended way of including the security token, but you can also pass your security token from Maven or any CI tool, such as Jenkins, by passing the
-DsecurityToken=<<token>> system property while running the install goal of Maven. For more information on Maven Command Line Options, see https://books.sonatype.com/mvnref-book/reference/running-sect-options.html.
To generate a security token:
- In the Perfecto UI, click your user name and select My security token.
- In the My security token form, click Generate Security Token.
Click Copy to clipboard. Then paste it into any scripts that you want to run with Perfecto. See Use a security token in automation scripts below.
- Click Close.
Use capabilities to select a device from the Perfecto lab. You can be as generic or specific as needed. In our script, we have only included the
platformName capability (which specifies the device operating system), as shown in the following code snippet. For more information, see Define capabilities and Use capabilities to select a device.
You can use the Manual Testing view in the Perfecto UI to generate a code snippet with device-specific capabilities that you can then copy-paste into your script.
To generate capabilities (for mobile or web devices) through the Perfecto UI:
- On the Perfecto landing page, under Manual Testing, click Open Device.
- In the Manual Testing view, do the following to generate capabilities:
- In the list or tile view, click a device.
- In the details pane on the right, click the Capabilities tab.
- (Mobile device only) Depending on what you want to see in your code sample, select Device attributes or Device ID.
- From the drop-down list, select the programming language to use.
- The code snippet is updated automatically.
- Click Copy to clipboard.
- Paste into your existing script.
As part of creating an instance of the RemoteWebDriver, you need to supply the URL of your Perfecto cloud. Define the name of your Perfecto cloud as a system variable by passing your cloud name as a
-DcloudName=<<cloud name>> from the CI/Maven system property while running the install goal of Maven, or simply hard-code your cloud name in the script.
The general structure of the URL string is as follows:
"https://" + cloudName +
Here are the respective lines from our sample script:
To get the most out of running your test in Perfecto, you need to create an instance of the Smart Reporting client (ReportiumClient). This will allow you to later retrieve and analyze the test report. The reporting client is responsible for gathering basic information about the test and transmitting it to the Smart Reporting system.
In our script, we show how to use the ReportiumClientFactory class' createPerfectoReportiumClient() method. Use the PerfectoExecutionContext class to supply the link to the factory class creating the client instance. Use the withWebDriver() method to supply the link of the driver instance.
Use the build() method to create the context object's instance and supply this to the createPerfectoReportiumClient() method when creating the ReportiumClient instance.
In addition to supplying the driver link, the context supports optional settings, such as adding:
- Reporting tags: Tags are used as a freestyle text for filtering the reports in the Reporting app. Use the withContextTags() method as shown in the following code snippet.
- CI job information: Job information is used to add your test runs to the CI Dashboard view. Use the withJob() method of the PerfectoExecutionContext instance, supplying the job name and job number, when creating the ReportiumClient instance.
Our sample script uses system variables to fetch the
reportium-job-numbervalues . Both system variables are sent from CI tools like Jenkins as a Maven
-Dsystem property. For example:
The following figure illustrates how the
reportium-job-numbersystem variables get their value from Jenkins. These variables are passed as -D parameters for install goals of Maven.
Our script supports both local and CI-based executions (for more information, see Add reporting to Jenkins > Supply Maven or Ivy parameters).
Replace the existing test code in the
try bock with your own test information.
In our example, the test is separated into logical groupings of actions as logical steps. Each step is labeled and appears in the Execution Report together with the component actions. The beginning of each logical step is indicated with the stepStart() method,
To end the test, supply an indication of the final outcome of the test by generating a TestResult instance.
In our example, the createSuccess method notifies the reporting server that the test resulted in a successful status. The createFailure method notifies the reporting server that the test resulted in an unsuccessful status and supports adding a notification message that is displayed in the test report. Our script also provides a failure reason, but this is optional. To learn more about failure reasons in reporting, see Work with failure reasons.
Last, make sure to close and quit the RemoteWebDriver and retrieve the Smart Reporting URL for the generated test report.
Also in this section: