Springboot, TestNG and POM

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

Run the FirstTestCase test case
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.

About This Site

The main aim of this site is to share knowledge with fellow software test specialists who are keen to grow both technically and professionally.

Categories