Page tree
Skip to end of metadata
Go to start of metadata

eThis section provides instructions on how to run a 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, they 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/PerfectoSampleProject


On this page:

Prerequisites

Before you get started, make sure you have installed the following:

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.

Note: For information on running the final scripts, see the README.md file included with the sample project.

To get started: 

  1. Access the sample project in GitHub and copy the clone URL: https://github.com/PerfectoMobileSA/PerfectoSampleProject
  2. 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.

Note: 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.


A | Supply the security token

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:

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

  2. In the My security token form, click Generate Security Token.
  3. Click Copy to clipboard. Then paste it into any scripts that you want to run with Perfecto. 

  4. Click Close.

B | Select a device

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 model
  • openDeviceTimeout: 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 capabilitiesUse 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:

  1. On the Perfecto landing page, under Manual Testing, click Open Device.
  2. In the Manual Testing view, do the following to generate capabilities:
    1. In the list or tile view, click a device.
    2. In the details pane on the right, click the Capabilities tab.
    3. (Mobile device only) Depending on what you want to see in your code sample, select Device attributes or Device ID.
    4. From the drop-down list, select the programming language to use.
    5. The code snippet is updated automatically.
    6. Click Copy to clipboard.
    7. Paste it into your existing script.
C | Provide the URL to connect to the Perfecto cloud

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

Note: 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);
D | Execute the test

Now that your script is ready to run, perform the following steps to execute the test.


 Steps for Eclipse
  1. Right-click the pom.xml file and select Run As > Maven build.
  2. In the Edit Configuration form, on the Main tab:

    1. In the Goals field, enter the following Maven goals:

      • If the credentials are hard coded:

        clean install

      • If the credentials are passed as parameters:

        clean
        install
        -DcloudName=${cloudName}
        -DsecurityToken=${securityToken}
        -DtestngXmlFile=testng_perfecto.xml

    2. Click Run.
      The following image shows sample run results.
 Steps for IntelliJ

The first time you run this test, you need to edit the run configuration. 

To run this test for the first time:

  1. From the Run menu, select Run.

  2. Select Edit Configurations

  3. Select the plus sign and select Maven.

  4. In the Run form, do the following:
    1. In the Name field, enter a descriptive name for the run.
    2. On the Parameters tab, in the Command line field, enter the following command, depending on whether credentials are hard-coded or passed as parameters.
      • If credentials are hard-coded:

        clean 
        install


      • If credentials are passed as parameters:

        clean
        install
        -DcloudName=${cloudName}
        -DsecurityToken=${securityToken}
        -DtestngXmlFile=testng_perfecto.xml



  5. Click Run.
    The following image shows sample run results.

To perform subsequent runs: 

  • From the Run menu, select Run > <Name>

Note: 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.

  • No labels