Executing Your First Test
by Santiago L. Schumacher
In our previous post, we staged a way to create Drivers, but we haven’t used it yet.
This time we will create some sample tests, a BaseTest.java
class, and use TestNG.xml to run them, that way we’ll be able to test if
the DriverFactory is working exactly as we expect, and we can execute concurrent or parallel tests, in different browsers, without anything breaking.
Why have a Base Test ?
You should never be initializing / quitting WebDriver instances in your Test Classes, with the main reason being Maintenance and Reusability.
If very single test class you write contains something in the lines of :
It is easy to tell this code is unmaintenable, and strongly hardcoded. How do I switch browsers ? Change the application URL ? The path to your driver file? It would require you
to navigate through each java class, make the neccesary modifications, and maybe possibly commiting your changes leading to conflicts.
To fix this we’ll create a BaseTest.
The purpose of this class (you can rename if you prefer) is to contain the logic regarding all the steps taken before/after running tests.
To be clear: The only reason to use things like @BeforeTest
,@BeforeClass
,@AfterMethod
, etc. in your Test Class is to satisfy test-specific pre or post-conditions, such as:
- Obtaining Data to execute the test from the Database/CSV/API etc.
- Navigating to a specific part of the WebApplication before starting the actual test
- Restoring the WebApplication original status before starting the test (ie: If your test disables an user, it might be a good idea to enable it again in an @AfterMethod)
Creating your BaseTest
Start by creating new package in your project if you don’t already have it - right click on src/main/java
, go to New -> Package and create:
testrunner
Inside the package create a new abstract
class (we don’t want anyone to create instances of the BaseTest!).
Copy the code below inside:
The Method Constants.getContextUrl()
contains the URL of your WebApplication, which will change depending on the Environment you’re executing against.
To use that Method effectively you will need 2 more classes, and a property file.
Create a package utils
in the root with the following Java code:
PropertyReader
Constants
Your .properties file, has to be located in the src/main/resources
folder, and for now will contains a single line:
url=${url}
The ${url} will be replaced dynamically when your test starts, thanks to Maven Profiles !
Maven Profiles: How to run tests against different Environments
All you need is a small section in your pom.xml
:
This is very powerful, as you can run mvn clean install -P YOUR_PROFILE_NAME
and use it to execute your tests against dev environment, QA, Staging, Production….
Read more about Maven Profiles:
- Official Maven Profile Documentation
- Setting Maven Profile on Eclipse IDE
- Setting Maven Profile with IntelliJ IDE
Final Step: A Sample Test
Finally we are able to open a Browser.
For every single Test and Page Object Classes, we will work in a different folder:
src/test/java
Because this is the default folder for your tests picked up by the maven-surefire-plugin. We can have tests in other folders/packages but it requires additional configuration
and is not, generally speaking, a good practice.
Create a package inside that folder called tests, and in there a Java Class called SampleTest, something along these lines:
Noticed anything strange ? Even having an empty test, with 0 lines of code, I am able to:
- Execute the Test, which instantiates a WebDriver instance (Using Chrome by default - we can change that later)
- Navigate to the URL of the application, depending on the selected Environment.
- Once the test completes, BaseTest will cleanly and quietly close your WebDriver instance.
What about you, are you seeing the same results ? Feel free to drop me an e-mail if you want to debug unforseen problems.
Thank you for sticking with me, and see you in the next post.
Subscribe via RSS