setup
- Java JDK — download, plus a Windows install walkthrough.
- Maven — download, plus a Windows install walkthrough.
validate Java and Maven are correctly set up
Command line:
java -version mvn -version
TestNG order of execution
@BeforeSuite— the annotated method will be run before all tests in this suite have run.@BeforeTest— the annotated method will be run before any test method belonging to the classes inside the<test>tag is run.@BeforeClass— the annotated method will be run before the first test method in the current class is invoked.@BeforeMethod— the annotated method will be run before each test method.@Test— marks a class or a method as part of the test.@AfterMethod— the annotated method will be run after each test method.@AfterClass— the annotated method will be run after all the test methods in the current class have been run.@AfterTest— the annotated method will be run after all the test methods belonging to the classes inside the<test>tag have run.@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:
@DataProvider@Listeners@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:
- Parameters from
testng.xml - Parameters with DataProviders
- Parameters in reports
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.
package com.guide.beginners.testng.samplejava— should share insight on how TestNG works.package com.guide.beginners.testng.theinternet.individualtestclass— should share insight on usage of TestNG with Selenium.
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:
@BeforeClassand@AfterClassto setup/teardown theWebDriver.@BeforeMethodfor instantiating or launching the test URL, setting up the test environment, or building the context to test.
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:
java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml
2. parallel tests, classes and methods
parallel="methods"— TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads, but they will respect the order that you specified.parallel="tests"— TestNG will run all the methods in the same<test>tag in the same thread, but each<test>tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same<test>and guarantee they will all run in the same thread, while taking advantage of TestNG using as many threads as possible to run your tests.parallel="classes"— TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.parallel="instances"— TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads. Additionally, the attributethread-countallows you to specify how many threads should be allocated for this execution.
The parallel attribute on the <suite> tag can take one of the following values:
<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
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:
java -jar selenium-server-standalone.jar -role hub
Node without nodeConfig.json:
java -jar selenium-server-standalone.jar -role webdriver -hub http://192.168.0.5:4444/grid/register/
2. using custom configuration
Hub:
java -jar selenium-server-standalone.jar -role hub --hubConfig hubconfig.json
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:
java -jar selenium-server-standalone-3.141.59.jar -role hub --hubConfig hubconfig.json