TestNG | Add a TestNG framework to a Selenium Mobile project

TestNG provides methods for test management by dividing the preparation, test, and cleanup methods, and allowing to run executions in parallel. With TestNG, you can:

  • Manage tests (before/preparation, after/cleanup, and test hierarchy*)
  • Manage test data (using a data provider, with an excel adapter)
  • Run executions in parallel
  • Provide a library of reports (easy to use)

Following is a link to the full framework files: https://github.com/PerfectoMobileSA/NXC_POM_Guide/tree/master/NXC_Automation

Important: This document includes references to a third-party product, TestNG. The user interface and usage of third-party products are subject to change without notice. For the latest published information about TestNG, see https://testng.org/doc/documentation-main.html.

To add a TestNG framework to a Selenium Mobile project:

First, install the Test NG plugin, or add it from Maven according to the instructions in this link.

  1. To add the TestNG framework, right-click your project src folder, and then click TestNG > Create TestNG Class.

  2. In the TestNG dialog box, do the follwoing:

    1. Set the source folder to your project src folder.
    2. Set the package name (e.g. "TestNG").
    3. Set the class name (something with your app name and "Test").
    4. Check the BeforeClass and AfterClass boxes.
    5. Add an XML suite file name (such as "testng.xml").
    6. Click Finish.
  3. Take care of the RemoteWebDriver and clean up:

    1. In the new TestNG class, add a RemoteWebDriver class member, as follows:

      Copy
      public class PerfectoAppTest {
          //Here we added the driver
          private RemoteWebDriver driver;
        @Test
        public void f() {
        }
        @BeforeClass
        public void beforeClass() {
        }
        @AfterClass
        public void afterClass() {
        }
      }
    2. In the beforeClass() method, which is annotated by the @BeforeClass annotation, set the relevant MCM and credentials. Then add the needed capabilities to choose the right device. In the following example, the device is picked by its model.

      Copy
      @BeforeClass 
      public void beforeClass(){
          System.out.println("Run started");

          String browserName = "mobileOS";
          DesiredCapabilities capabilities = new DesiredCapabilities(browserName, "",
                                                 Platform.ANY);

          String user = URLEncoder.encode("myPerfectoUsername", "UTF-8");
          String password = URLEncoder.encode("myPassword", "UTF-8");

          capabilities.setCapability("model", "iPhone-6");

          // Uncomment this block if you want to use    
          // a device which is opened in the recorder window     /* 
          try { 
              EclipseConnector connector = new EclipseConnector();
              String eclipseExecutionId = connector.getExecutionId();                             
              capabilities.setCapability("eclipseExecutionId", eclipseExecutionId);                     
              }
              catch (IOException ex){
                       ex.printStackTrace();
              }
          */

          this.driver = new RemoteWebDriver(new URL("https://" + user + ':' + password +                                                                                         
              '@' + host + "/nexperience/wd/hub"), capabilities);
       }
    3. In the afterClass() method, which is annotated by the @AfterClass annotation, download the execution report, then perform the clean up by adding the call to driver.quit().

      Copy
      @AfterClass
      public void afterClass() {
          try{
              // Close the browser
              driver.close();
                  
              // Download a pdf version of the execution report
              PerfectoUtils.downloadReport(driver, "pdf", "C:\\temp\\report.pdf");
              }
              catch(Exception e){
                  e.printStackTrace();
              }
          driver.quit();
      }
  4. Write the test: In the f() method, which is annotated by the @Test annotation, write the actual test you want to run.

    The following example uses the Page Object Model framework. The script will launch the device browser and go to Perfecto's test website. Next, the script will try to log in using a valid username and password. Finally, the script will look for the "Welcome back" message, which is displayed upon a successful login.

    Copy
    @Test
    public void f() {
        boolean res = false;
        NXCBaseView view = new NXCBaseView(this.driver);
        try{
            view.init().login("john", "Perfecto1");
            res = driver.findElement(By.xpath("//*[contains(text(),'Welcome " + "back'])")).isDisplayed();
         }
         catch(Exception e){}

        Assert.assertTrue(res);
    }

    This full basic flow will create a RemoteWebDriver, run the test, and eventually close the driver and download the report.

  5. To run the test, right-click the TestNG class in the solution explorer and then select Run as TestNG.

    After the test run is completed, you can see the results in the TestNG perspective.