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

Last updated: Sep 14, 2017 13:00

Managing the test flow with the popular TestNG framework.

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

  • manage test (before/preparation, after/cleanup, and test hierarchy*)
  • manage test data (using a data provider, with an excel adapter)
  • run execution in parallel
  • provide a library of reports (easy to use)

Here's how you can get started:

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

1. In order to add the TestNG framework, right click on your project src folder, then click TestNG -> Create TestNG Class (image below).

 


2. In the TestNG box (image below):

  • Set the source folder to your project src folder.
  • Set the package name (e.g. "testNG").
  • Set the class name (something with your app name and "Test").
  • Check the BeforeClass and AfterClass boxes.
  • Add an XML suite file name (e.g. "testng.xml").
  • Click Finish.

 

3. Taking care of the RemoteWebDriver and clean up:

  • In the new TestNG class, add a RemoteWebDriver class member, as shown below:
public class PerfectoAppTest {
    //Here we added the driver
    private RemoteWebDriver driver;
  @Test
  public void f() {
  }
  @BeforeClass
  public void beforeClass() {
  }
  @AfterClass
  public void afterClass() {
  }
}


 

  • In the beforeClass() method, which is annotated by the @BeforeClass annotation, we will set the relevant MCM and credentials. Then we will add the needed capabilities, to choose the right device: (in the example below we will pick the device by its model).
@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);
 }

 

  • In the afterClass() method, which is annotated by the @AfterClass annotation, we will download the execution report, then do the clean up by adding the call to driver.quit()
@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. Writing the test:

  • In the f() method, which is annotated by the @Test annotation, we will write the actual test we want to run.
  • In this post I will use the Page Object Model framework, which we've created in an earlier series of posts.
  • The script will launch the device browser and go to Perfecto's test website
  • Then 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.
@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);
}

Now we have a full basic flow, which will create a RemoteWebDriver, run the test and eventually close the driver and download the report.

In order to Run the Test, we need to right click on the testNG class in the solution explorer, then choose "Run as TestNG" (image below).

 

After the test run is completed, we can see the results in the TestNG perspective, as shown in the image below:

 

Link to full framework files.

Click here to go to the next post, where we show how to pass data parameters to the test, from the testng.xml file.