~/beginnersguidefortesting The TestNG Ladder →

beginnersguidefortesting

This will give beginners an idea on how to build and learn TestNG.

setup

validate Java and Maven are correctly set up

Command line:

shell
java -version
mvn -version

TestNG order of execution

  1. @BeforeSuite — the annotated method will be run before all tests in this suite have run.
  2. @BeforeTest — the annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
  3. @BeforeClass — the annotated method will be run before the first test method in the current class is invoked.
  4. @BeforeMethod — the annotated method will be run before each test method.
  5. @Test — marks a class or a method as part of the test.
  6. @AfterMethod — the annotated method will be run after each test method.
  7. @AfterClass — the annotated method will be run after all the test methods in the current class have been run.
  8. @AfterTest — the annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
  9. @AfterSuite — the annotated method will be run after all tests in this suite have run.

The annotations above will also be honored (inherited) when placed on a superclass of a TestNG class.

Additional annotations:

  1. @DataProvider
  2. @Listeners
  3. @Parameters

parameters

@Parameters describes how to pass parameters to a @Test method. value is the list of variables used to fill the parameters of this method.

Parameters:

  1. Parameters from testng.xml
  2. Parameters with DataProviders
  3. Parameters in reports
rule that survives every refactor

Do not declare test data as a class-level local variable — maintenance of the test data is a challenge if we have 100+ tests spread all over multiple packages.

Additional documentation can be accessed using: https://testng.org/doc/documentation-main.html. testng.xml helps to execute a suite of tests.

With the knowledge gained from the above two packages, we shall spin off a Selenium TestNG framework.

step 1

Before designing a framework, develop a test that achieves a simple goal.

class name="com.guide.beginners.testng.theinternet.individualtestclass.Step1"

Demonstrate test design and the rough draft of code to check if it works.

step 2

class name="com.guide.beginners.testng.theinternet.individualtestclass.Step2"

Demonstrates separating data and page objects from the tests. Here we're still declaring with private to class all variable components of the test, and using additional TestNG annotations to create a chain of events.

step 3

class name="com.guide.beginners.testng.theinternet.individualtestclass.Step3"

Test data is now totally isolated and is passed in via seleniumsuite.xml. Refer the file path: src\test\resources\testngsuite\seleniumsuite.xml.

@Optional specifies that the current parameter is optional. TestNG will pass in a specified default value, or null if none is specified.

step 4

Extracting driver setup to base in framework: package com.guide.beginners.testng.theinternet.frameworktestng.base

We are using:

Each test class is a scenario, and each class may have one or more @Test cases.

running in parallel

As per TestNG's official documentation, you can instruct TestNG to run your tests in separate threads in various ways.

1. parallel suites

This is useful if you are running several suite files (e.g. java org.testng.TestNG testng1.xml testng2.xml) and you want each of these suites to be run in a separate thread. You can use the following command line flag to specify the size of a thread pool:

shell
java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml

2. parallel tests, classes and methods

The parallel attribute on the <suite> tag can take one of the following values:

testng.xml
<suite name="My suite" parallel="methods" thread-count="5">
<suite name="My suite" parallel="tests" thread-count="5">
<suite name="My suite" parallel="classes" thread-count="5">
<suite name="My suite" parallel="instances" thread-count="5">

Note: the @Test attribute timeOut works in both parallel and non-parallel mode. If ever we want to run parallel, we cannot use parallel mode at the level of methods.

command line execution

shell
mvn clean test -DsuiteXmlFiles=CrossBrowserParallelSuite.xml

setting up Selenium Grid

One should be in the path: /src/test/resources/binaries

1. using standard configuration

Hub without hubconfig.json:

hub
java -jar selenium-server-standalone.jar -role hub

Node without nodeConfig.json:

node
java -jar selenium-server-standalone.jar -role webdriver -hub http://192.168.0.5:4444/grid/register/

2. using custom configuration

Hub:

hub
java -jar selenium-server-standalone.jar -role hub --hubConfig hubconfig.json

Node:

node
Java -Dwebdriver.chrome.driver="chromedriver.exe" -Dwebdriver.ie.driver="IEDriverServer.exe" -Dwebdriver.gecko.driver="geckodriver.exe" -jar selenium-server-standalone.jar -role node -nodeConfig nodeconfig.json

This project pins the standalone Selenium server jar to a specific version:

hub
java -jar selenium-server-standalone-3.141.59.jar -role hub --hubConfig hubconfig.json