Springboot, Selenium and Application Properties file

Previously I have written 3 blogs on Springboot with Selenium

This blog is last chapter to the above blogs

Read the application.properties file

The application.properties file can be used to store the server url or enable or disable a flag etc. Lets see how read the serverUrl from properties file.

In the previous blogs, we had hard coded the test server url (AUT). In this section lets read that from properties file

Open the application.properties file in /src/main/resources and add the following

serverUrl=https://www.live.guru99.com

Create ReadProperties.java file and add the following:

 @Value("${serverUrl:}")
 String serverUrl;

Generate Getters and Setters for the String variable. Once its done, the ReadProperties.java file looks like below:

ReadProperties.java

package base.firstautomation;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class ReadProperties {
 
 @Value("${serverUrl:}")
 String serverUrl;
 
 public String getServerUrl() {
  return serverUrl;
 }
 
 public void setServerUrl(String serverUrl) {
  this.serverUrl = serverUrl;
 }
}

Lets go to WebDriverBase.java file and update the code to replace serverUrl.
WebDriverBase.java

package base.firstautomation;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class WebDriverBase {
 Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
  
 @Autowired
 ReadProperties readProperties;
  
 WebDriver webDriver = null;
  
 public void setChromeDriver(){
  logger.info("####### SET UP CHROME DRIVER ######");
  System.setProperty("webdriver.chrome.driver",
    System.getProperty("user.dir") + "\\src\\main\\resources\\chromedriver.exe");
  webDriver = new ChromeDriver();
  webDriver.manage().window().maximize();
 }
  
 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 #####");
  }
   
  logger.info("Load the server url: " + readProperties.getServerUrl());
  webDriver.get(readProperties.getServerUrl());
 }
  
 public void quitDriver(){
  webDriver.quit();
  logger.info("Close browser");
 }
  
 public WebDriver getWebDriver() {
  return webDriver;
 }
}

Re-Run the test case:

Test run should be successful.

If you see there is no extra import such as java.io.*; to read the file. So we can summarize that the Spring Boot framework allows us to avoid lot of boiler plate code and test engineers can spend the time to write the test cases instead of codes related to set up etc.
Another fantastic plug in we can use to reduce the no of lines of code is lombok. With the help of lombok, we can altogether avoid getter and setter codes.

Hope this blog is useful.

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