In the market there are lot of test automation tutorials which describe the standard framework involving Selenium Webdriver for Java, TestNG & Maven. And the most widely used design pattern is Page Object Model (POM). In this blog, we will see an additional framework 'Spring Boot' which is used to develop production grade application in short amount of time.
I will try to explain the framework from s/w test perspective. The details of Spring Boot is not in the scope of the blog.
Let's start..
Spring Boot is an open source framework which provides Java developers with a platform to start with an auto configurable production-grade Spring application. How will it be useful for the Automation Test Specialists?
In most the standard test automation framework, properties files are used :
I will try to explain the framework from s/w test perspective. The details of Spring Boot is not in the scope of the blog.
Let's start..
Spring Boot is an open source framework which provides Java developers with a platform to start with an auto configurable production-grade Spring application. How will it be useful for the Automation Test Specialists?
In most the standard test automation framework, properties files are used :
- to store the server urls
- browser details (if you want to run the test cases using chrome or firefox etc)
- used to store the xpaths
- internationalization
To read the properties file, we have to write certain lines of code involving Java file class. What if there is a way to read the properties file without explicitly using Java file class? Spring Boot enables us to do exactly that so that we test automation specialist can focus on writing more test cases quickly.
Lets see another example :
Log4j is widely used for logging by developer and test specialists. Usage of this involves in downloading the jar, creating corresponding properties file or xml file. Won't be it good to have the feature by default if you create an automation project? Spring Boot enables this feature by default.
Step by step guide to create test automation framework with the help of Spring Boot.
The simplest way to a create Spring Boot project is to create it using Spring Initializr.
Now our Spring Application is ready. It's time to create a test cases with the help of TestNG and run it.
This concludes a basic test automation framework (minus Selenium WebDriver). Lets see that in next blogs.
And now discuss the advantage of the framework over popular framework available:
Step by step guide to create test automation framework with the help of Spring Boot.
Developing Your First Spring Boot Application
The simplest way to a create Spring Boot project is to create it using Spring Initializr.
- Go to Spring Initializr.
- Select Project -> Maven Project
- Language -> Java
- Spring Boot -> The latest version available (its 2.2.5 now)
- Enter Project Metadata: Group, Artifact
- Click 'Generate - Ctrl + Enter button' to download the project
- The project will be downloaded with the artifact name. In our example its 'firstautomation'
- Unzip the file
- Import the project as a Maven project to Eclipse (Version: Neon.3 Release (4.6.3) is used for the tutorial)
- Once the project is imported, Build the project. It will look like the following screenshot
- Go to src/main/java and check for the FirstautomationApplication.java file. Just make its available. We will not touch it unless required.
Create a simple test case with TestNG
- Add TestNG dependency in pom.xml file and build the project
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.5</version> </dependency>
- Once the TestNG dependency is added, create a test case
- Create a TestNG Test Case say FirstTestCase.java and make sure that FirstTestCase class extends AbstractTestNGSpringContextTests abstract class.
- Create a test case as below and run the test case.
@Test public void firstTest() { Assert.assertTrue(4==(2+2), "4 is not equal to 2+2"); }
- The test case will be executed successfully
Test Case code:
Now we have seen how to create a TestNG test case and run it successfully. Let's create a test framework mimicking Page Object Model automation framework.
In standard framework we do the set up for the browser session (create the base), and upon completion of the execution, we teardown the session etc.
Lets create a BaseTest.java file for the above mentioned purpose. And will create a WebDriverSession to initialize the browser etc.
BaseTest.java
Points to note above:
FirstTestCase.java looks like the following:
WebDriverBase.java
A Java class decorated with @Component is found during classpath scanning and registered in the context as a Spring bean. Note that - WebDriverBase is decorated with @Component
Once WebDriverBase.class is defined, update the BaseTest.java file with init() and quitDriver().
BaseTest.java (updated)
FirstautomationApplication.java
Finally update the FirstautomationApplication.java so that spring can scan the packages to scan for components
package base.firstautomation; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.Assert; import org.testng.annotations.Test; public class FirstTestCase extends AbstractTestNGSpringContextTests { @Test public void firstTest() { Assert.assertTrue(4==(2+2), "4 is not equal to 2+2"); } }
Test Automation Framework
Now we have seen how to create a TestNG test case and run it successfully. Let's create a test framework mimicking Page Object Model automation framework.
In standard framework we do the set up for the browser session (create the base), and upon completion of the execution, we teardown the session etc.
Lets create a BaseTest.java file for the above mentioned purpose. And will create a WebDriverSession to initialize the browser etc.
BaseTest.java
package base.firstautomation; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import base.firstautomation.FirstautomationApplication; @SpringBootTest(classes = FirstautomationApplication.class) public abstract class BaseTest extends AbstractTestNGSpringContextTests{ //Place holder for webdriversession @BeforeMethod public void setup() throws InterruptedException { logger.info("Before method started"); //function to intialize the driver } @AfterMethod public void tearDown() { logger.info("After method started"); //Function to close the driver } }
Points to note above:
- @SpringBootTest(classes = FirstautomationApplication.class) annotation is used
- BaseTest extends AbstractTestNGSpringContextTests and our test case will extend the BaseTest
- @BeforeMethod / @AfterMethod self explanatory
FirstTestCase.java looks like the following:
package base.firstautomation; import org.testng.Assert; import org.testng.annotations.Test; public class FirstTestCase extends BaseTest { @Test public void firstTest() { Assert.assertTrue(4==(2+2), "4 is not equal to 2+2"); } }
WebDriverBase.java
A Java class decorated with @Component is found during classpath scanning and registered in the context as a Spring bean. Note that - WebDriverBase is decorated with @Component
package base.firstautomation; import org.slf4j.Logger; import org.springframework.stereotype.Component; @Component public class WebDriverBase { Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass()); public void setChromeDriver(){ logger.info("######## SET UP CHROME DRIVER ########"); } public void init() throws InterruptedException { logger.info("This is init in Webdriver session"); String browserType="chrome"; switch(browserType){ case "chrome": setChromeDriver(); break; default: System.out.println("######### No Browser Type provided #################"); } } public void quitDriver(){ logger.info("Close browser"); } }
Once WebDriverBase.class is defined, update the BaseTest.java file with init() and quitDriver().
BaseTest.java (updated)
package base.firstautomation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import base.firstautomation.FirstautomationApplication; @SpringBootTest(classes = FirstautomationApplication.class) public abstract class BaseTest extends AbstractTestNGSpringContextTests{ //Place holder for webdriversession @Autowired WebDriverBase webDriverBase; @BeforeMethod public void setup() throws InterruptedException { logger.info("Before method started"); //function to intialize the driver webDriverBase.init(); } @AfterMethod public void tearDown() { logger.info("After method started"); //Function to close the driver webDriverBase.quitDriver(); } }
FirstautomationApplication.java
Finally update the FirstautomationApplication.java so that spring can scan the packages to scan for components
package base.firstautomation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan({"base.firstautomation"}) @SpringBootApplication public class FirstautomationApplication { public static void main(String[] args) { SpringApplication.run(FirstautomationApplication.class, args); } }
Run the FirstTestCase test case
After running the test case (FirstTestCase.java) the following can be seen from the logs
This concludes a basic test automation framework (minus Selenium WebDriver). Lets see that in next blogs.
And now discuss the advantage of the framework over popular framework available:
- Reduced boiler plate code
- No xml or properties file for log4j. Its available in the springboot framework
- And there is new ClassName() to create the object. It reduces lines of code if you are working a large project having several PagaObject classes.
No comments:
Post a Comment