Selenium WebDriver with SpringBoot
I have written 2 blogs on how to leverage the power of SpringBoot while creating the Test Automation Framework with Selenium WebDriver (Java).
Web Test Automation with SpringBoot and TestNG (Part 1). Click here for details
Web Test Automation - Page Object Model pattern with SpringBoot, TestNG and Selenium WebDriver (Part 2). Click here for details.
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 Web Test Automation with Springboot and TestNG and Web Test Automation - Page Object Model pattern wi... we had hard coded the test server url (AUT). In this section lets read that from properties fine
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.
Let me know your thoughts about the framework and please do not forget to leave a comment with your suggestions.
No comments:
Post a Comment