My first Perfecto script
This section provides instructions on how to run an Appium/Selenium test with Java in Perfecto. It assumes that you are:
- Familiar with Appium/Selenium
- Do not have any existing tests to work with
- A novice user of Perfecto
To run tests in Perfecto, you need to include:
- What driver you want to use
- Where your Perfecto instance is located
- Who you are
- What devices you want to work on
Our sample project is available on GitHub: https://github.com/PerfectoMobileSA/PerfectoJavaSample
Prerequisites
Before you get started, make sure you have installed the following:
- Java
- 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
Our sample project includes the following simple scripts: PerfectoAppium.java
and PerfectoSelenium.java
. The scripts accesses the Perfecto website and verify the title. They only serves the purpose of showing you how to connect to Perfecto.
For information on running the final scripts, see the README.md file included with the sample project.
To get started:
- Access the sample project in GitHub and copy the clone URL: https://github.com/PerfectoMobileSA/PerfectoJavaSample
- Open your IDE and check out the project from GitHub.
2 | Configure the script for Perfecto
In this step, you configure security information and the Perfecto cloud name. If needed, you can also modify the device details.
The project's pom.xml
file is institutional because it holds all configurations and dependencies. In its initial state, when you first create a project, the file is very simple. The pom.xml
file in our sample project has been updated with the required Perfecto dependencies.
Expand a step to view its content.
Generate your security token through the Perfecto UI. Search for the following line in PerfectoAppium.java
and replace <<security token>>
with your Perfecto security token.
String securityToken = "<<security token>>";
Alternatively, you can pass the token as a Maven property, as follows:
-DsecurityToken=<<SECURITY TOKEN>>
To generate a security token:
-
In the Perfecto UI at <YourCloud>.app.perfectomobile.com (where YourCloud is your actual cloud name, such as mobilecloud), 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.
- Click Close.
Use capabilities to select a device from the Perfecto lab. You can be as generic or specific as needed. Our Appium script includes the following capabilities:
model
: The device modelopenDeviceTimeout
: The timeout, in minutes, to wait for a specific device in case it is not available at the start of the script (use with caution)appPackage
: The Java package of the Android app you want to run
Appium
DesiredCapabilities capabilities = new DesiredCapabilities(browserName, "", Platform.ANY);
...
capabilities.setCapability("model", "Galaxy.*");
capabilities.setCapability("openDeviceTimeout", 2);
capabilities.setCapability("appPackage", "com.sec.android.app.popupcalculator");
Our Selenium script only includes the platformName
capability (which specifies the device operating system), as shown in the following code snippet. For more information, see Define capabilities, Use capabilities to select a device, and Supported Appium capabilities.
Selenium
DesiredCapabilities capabilities = new DesiredCapabilities(browserName, "", Platform.ANY);
...
capabilities.setCapability("platformName", "Android");
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 the code into your existing script.
As part of creating an instance of the RemoteWebDriver, you need to supply the URL of your Perfecto cloud. Search for the following line in PerfectoAppium.java
and replace <<cloud name>>
with the name of your Perfecto cloud (for example: demo
).
String cloudName = "<<cloud name>>";
Alternatively, you can pass the TestNG file name as a Maven property to execute specific TestNG files, as follows:
-DtestngXmlFile=<<testng file name>>.xml
testng_perfecto.xml
is configured by default. You can override it by passing the preferred TestNG name as a Maven property, such as -DtestingXmlFile=testng.xml
, to execute all 4 tests in parallel.
The general structure of the URL string is as follows:
"https://" + Utils.fetchCloudName(cloudName) + ".perfectomobile.com/nexperience/perfectomobile/wd/hub"
Here is the respective line from our sample script:
WebDriver driver = new RemoteWebDriver(new URL("https://" + Utils.fetchCloudName(cloudName) + ".perfectomobile.com/nexperience/perfectomobile/wd/hub"), capabilities);
Now that your script is ready to run, perform the following steps to execute the test.
-
Right-click the
pom.xml
file and select Run As > Maven build. -
In the Edit Configuration dialog box, on the Main tab:
-
In the Goals field, enter the following Maven goals:
-
Click Run.
The following image shows sample run results.
-
The first time you run this test, you need to edit the run configuration.
To run this test for the first time:
-
From the Run menu, select Run.
-
Select Edit Configurations.
-
Select the plus sign and select Maven.
-
In the Run dialog box, do the following:
-
In the Name field, enter a descriptive name for the run.
-
On the Parameters tab, in the Command line field, enter the following command, depending on whether credentials are hard-coded or passed as parameters.
-
-
Click Run.
The following image shows sample run results.
To perform subsequent runs:
- From the Run menu, select Run > <Name>.
You can pass the TestNG file name as a Maven property to execute specific TestNG files, as follows:
-DtestngXmlFile=<<testng file name>>.xml
testng_perfecto.xml
is configured by default, and it is not mandatory to pass it as a Maven property. However, you can override it by passing your preferred TestNG name as a Maven property, such as -DtestingXmlFile=testng.xml
, to execute all 4 tests in parallel.