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:
- 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).
- 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()
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.
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:
Click here to go to the next post, where we show how to pass data parameters to the test, from the testng.xml file.