UI automation from scratch: Part 1 – your test in testNG

I like so far that we are doing all this from scratch, following only the guidelines provided by the originators. For TestNG class generation, I’d like to follow this document: http://testng.org/doc/documentation-main.html

TestNG comes with many benefits but if I were to pick one, that would be the use of DataProvider.

But before starting with TestNG classes, we need to take care of the dependencies on TestNG first. For any maven dependency, it’s a good habit to search on this page: https://mvnrepository.com

Searching on ‘testng’ results below:

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
 <groupId>org.testng</groupId>
 <artifactId>testng</artifactId>
 <version>6.10</version>
</dependency>

Let’s add it in pom.xml (and don’t forget to Source > Format from Eclipse menu). This section should replace JUnit section under dependencies.

We also need to install TestNG plugin for Eclipse by following this: http://testng.org/doc/eclipse.html (Eclipse > Help > Install New Software > work with: http://beust.com/eclipse > hit Add, give name TestNG > check on everything under TestNG > hit Next > Accept Agreement > Say yes when it’s asking to restart Eclipse)

Now we can create our AppTest in TestNG

package com.stonecress;
import org.testng.annotations.Test;

public class AppTest {
  @Test(groups = { "functest", "checkintest" })
  public void testMethod1() {
  }
 
  @Test(groups = {"functest", "checkintest"} )
  public void testMethod2() {
  }
 
  @Test(groups = { "functest" })
  public void testMethod3() {
  }
}

And you can try running it in Eclipse by right click > Run As > TestNG test

Screen Shot 2016-12-23 at 11.48.21 PM

Also try on command line

mvn test

To find

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.stonecress.AppTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@484b61fc
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.458 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

[INFO] ----------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ----------------------------------------------------------

Try these to find the meaning of test groups and features of surefire plugin working with maven

mvn test -Dgroups=functest
mvn test -Dgroups=checkintest
mvn test -Dtest=AppTest
mvn test -Dtest=AppTest#testMethod3
mvn test -Dtest=AppTest#testMethod3+testMethod2

If the last line does not work, probably your Eclipse came with surefire plugin older than 2.19. Let’s add below to the pom.xml

<project>
...
 <dependencies>
...
  <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
  </dependency>
 </dependencies>
...
 <build>
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.19.1</version>
    </plugin>
   </plugins>
  </pluginManagement>
 </build>
</project>

changgull