Start | | Maven | Quickstart | |
---|---|
Run | | TestNG | Command Line | IDE Support | Java JDK | Troubleshooting | |
Report | | Configuration | Environment Switching | Spark HTML Reports | Logging | |
Advanced | | Before / After Hooks | JSON Transforms | Contributing | |
The Framework uses Spring Boot Test, Cucumber , Rest Assured and Selenium client implementations.
Spring <dependencies>
:
<dependecies>
...
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${spring-rabbit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
...
</dependecies>
Cucumber & Rest Assured <dependencies>
:
<dependecies>
...
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${restassured.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${cucumber.version}</version>
</dependency>
...
</dependecies>
Selenium <dependencies>
:
<dependecies>
...
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium-version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>${selenium-version}</version>
</dependency>
...
</dependecies>
Recommended
By using the TestNG Framework we can utilize the Cucumber Framework
and the @CucumberOptions
Annotation Type to execute the *.feature
file tests
Right click the
WikipediParallelRunner
class and selectRun
@CucumberOptions(
features = {
"src/test/resources/feature"
},
plugin = {
"pretty",
"json:target/cucumber/report.json",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"
})
public class WikipediaParallelRunnerTest extends AbstractTestNGCucumberTests {
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
Normally you will use your IDE to run a *.feature
file directly or via the *Test.java
class. With the Test
class,
we can run tests from the command-line as well.
Note that the mvn test
command only runs test classes that follow the *Test.java
naming convention.
You can run a single test or a suite or tests like so :
mvn test -Dtest=WikipediaParallelRunnerTest
Note that the mvn clean install
command runs all test Classes that follow the *Test.java
naming convention
mvn clean install
To minimize the discrepancies between IDE versions and Locales the <sourceEncoding>
is set to UTF-8
<properties>
...
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
...
</properties>
The Java version to use is defined in the maven-compiler-plugin
<build>
...
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
...
</plugins>
</pluginManagement>
...
</build>
The AbstractTestDefinition
class is responsible for specifying each Step class as @SpringBootTest
and
its @ContextConfiguration
@ContextConfiguration(classes = {FrameworkContextConfiguration.class})
@SpringBootTest
public class AbstractTestDefinition {
}
The FrameworkContextConfiguration
class is responsible for specifying the Spring @Configuration
, modules to scan,
properties to use etc
@EnableRetry
@Configuration
@ComponentScan({
"com.cmccarthy.api", "com.cmccarthy.common",
})
@PropertySource("application.properties")
public class FrameworkContextConfiguration {
}
There is only one thing you need to do to switch the environment - which is to set <activeByDefault>
property in the
Master POM.
By default, the value of
spring.profiles.active
is defined in theapplication.properties
file which inherits its value from the Master POM property<activeByDefault>
<profiles>
...
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
...
</profiles>
You can then specify the profile to use when running Maven from the command line like so:
mvn clean install -DactiveProfile=github-headless
Below is an example of the application.properties
file.
spring.profiles.active=@activatedProperties@
The Framework uses Extent Reports Framework to generate the HTML Test Reports
The example below is a report generated automatically by Extent Reports open-source library.
The Framework uses Allure Reports to generate the HTML Test Reports
The example below is a report generated by Allure Reports open-source library.
To generate the above report navigate to the root directory of the module under test and execute the following command
mvn allure:serve
or mvn allure:generate
(for an offline report)
The Framework uses Log4j2 You can instantiate the logging service in any Class like so
private final Logger logger=LoggerFactory.getLogger(WikipediaPageSteps.class);
you can then use the logger like so :
logger.info("This is a info message");
logger.warn("This is a warning message");
logger.debug("This is a info message");
logger.error("This is a error message");
The Logback logging service is initialized from the Hooks.class
As the Cucumber Hooks are implemented by all steps we can configure the @CucumberContextConfiguration
like so :
@CucumberContextConfiguration
public class Hooks extends AbstractTestDefinition {
private static boolean initialized = false;
private static final Object lock = new Object();
@Autowired
private HookUtil hookUtil;
@Autowired
private DriverManager driverManager;
@Before
public void beforeScenario(Scenario scenario) {
synchronized (lock) {
if (!initialized) {
if (!driverManager.isDriverExisting()) {
driverManager.downloadDriver();
}
initialized = true;
}
}
driverManager.createDriver();
}
@After
public void afterScenario(Scenario scenario) {
hookUtil.endOfTest(scenario);
WebDriverRunner.closeWebDriver();
}
}
Rest Assured IO is used to map the Response
Objects to their respective POJO
Classes
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.0</version>
</dependency>
cd ~/install directory path/spring-cucumber-testng-parallel-test-harness
mvn clean install -DskipTests
Spotted a mistake? Questions? Suggestions?