Part 3 - Hands-On Test Automation Project with Java + Cucumber + Selenium + Spring + Docker

Part 3 - Hands-On Test Automation Project with Java + Cucumber + Selenium + Spring + Docker

This article is a continuation of Part 2 - Dependency Injection. The implementation of Part 2 can be found in my demo-spring-selenium project tagged as v2.0. You can download this release to use it as a starting point for this part of the article.

Part 3 - Configurations

This part will go through more details on ways of running the tests and doing some configurations related to it.

Command Line Execution

We need to think about how we would like to run our tests. Running inside the IDE may be fine to execute it locally, but we might also need to run it through the command line if we want to run on a server. 

Cucumber runs the feature files via TestNG and needs a dedicated test runner class to actually run the feature files. Create the class CucumberRunner.java on the package demo.spring.selenium with the following content:

@CucumberOptions(
    glue = "demo/spring/selenium/stepdefinitions",
    features = "src/test/resources/features")
public class CucumberRunner extends AbstractTestNGCucumberTests {
}

Cucumber needs that we inform on the "glue" attribute the folder where the step definitions are. We also need to inform on the "features" attribute the folder where the features are. We should be able to run your test by running the CucumberRunner class. 

No alt text provided for this image
No alt text provided for this image

However, this is not yet a solution to run in a server, we need an option to run it through command line. We should configure Gradle for this. Inside the build.gradle file, add the following to the end of the file:

test {
    useTestNG()
    scanForTestClasses = false
    testLogging.showStandardStreams = true
}

To run our project through command line we just access the root of the project through the terminal and type the following:

./gradlew test

Tags

When we are creating a test automation project we should also think about all the different ways we want or may need to execute our tests. For example, Cucumber allows us to create tags. Tags are a great way to organize our features and scenarios. They can be used for running a subset of scenarios or restricting hooks to a subset of scenarios (https://cucumber.io/docs/cucumber/api/#tags). 

In our project, we just need to add an extra line to the "test" block in the build.gradle file as shown below.

test {
    useTestNG()
    systemProperties(System.properties)
    scanForTestClasses = false
    testLogging.showStandardStreams = true
}

Remember we added the tags "@login" and "@smoke" to our login.feature? 

The following command should be used to run only the scenario tagged with "@smoke", which is just the first scenario.

./gradlew test -Dcucumber.filter.tags="@smoke"

The following command should be used to run only the scenario tagged with "@login", which is both scenarios.

./gradlew test -Dcucumber.filter.tags="@login"

Multiple Properties Files

There are also some projects that we need not just one property file, but we need one property file for different sets of configurations, for example, different testing environments (i.e., development, test, release). Spring has a feature called Spring Profiles that provides a way to segregate parts of our application configuration and make it available only in certain environments. 

Create an application-test.yml file with the same structure of your application.yml file, but change the property value. Your application.yml file will be the default profile and the application-test.yml will be considered a "test" profile. As a pattern, Spring understands the profile name as the value that appears after "-" on the file name. In case we want to create a third profile called "release", you just need to create the file application-release.yml.

application-test.yml

host: https://the-internet.herokuapp.com

If we run our tests without setting the profile, it will run and use the default values set on the application.yml file. To set a profile different from the default we need to pass it as an environment variable, for example:

SPRING_PROFILES_ACTIVE=test ./gradlew test

We are done with Part 3 of this article. :)

Remember to commit your changes to Git:

git add .
git commit -m "Adding Configurations"
git push

The implementation of this part can be found in my demo-spring-selenium project tagged as v3.0. You can download this release to check how it looks and/or use it as a starting point for Part 4 of this article.

Thanks for reading! I hope you found it helpful.

If you enjoyed it, I appreciate your help in spreading it by sharing it with a friend.

Excellent article. Well done.

Bijan Patel

Mobile Test Automation Lead @ NatWest Group | Certified Tosca Architect | Full Stack Test Automation Expert | Selenium & Appium Expert | Avaloq Certified | IC Agile Certified | Trainer

4 年

Great articles

要查看或添加评论,请登录

Soraia Reis Fernandes的更多文章

社区洞察

其他会员也浏览了