Automated tests can be run on multiple environments, multiple browsers, multiple devices and multiple data sets in parallel.
These tests are less prone to human error than manual tests, which can help improve the accuracy and reliability of the testing process. Furthermore, they are repeatable, consistent and reliable which helps in reducing false positives and false negatives. In this article, I’ll talk about why you should be using automated tests in Selenium, JUnit5 and Gauge Framework.
Increases Efficiency
Automation tests can be run faster than manual tests, allowing more tests to be run in a shorter time period. This can help identify issues and bugs more quickly, saving time and resources in the long run. Automation tests can run on multiple environments, multiple browsers, multiple devices and multiple data sets in parallel which increases the overall efficiency of the testing process.
import com.thoughtworks.gauge.Step
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
class AutomationTest {
private lateinit var driver: WebDriver
@Step("Open the browser and navigate to the website")
fun openBrowser() {
System.setProperty("webdriver.chrome.driver", "/path/to/chrome/driver")
driver = ChromeDriver()
driver.get("https://www.example.com")
}
@Step("Search for <searchTerm>")
fun search(searchTerm: String) {
driver.findElement(By.name("q")).sendKeys(searchTerm)
driver.findElement(By.name("btnK")).click()
}
@Step("Close the browser")
fun closeBrowser() {
driver.quit()
}
}
This is an example of a simple test automation script written in Kotlin which is using the Selenium library to interact with a web page, and the Gauge framework to organize and run the tests. This script opens a browser, navigates to a website, performs a search, and then closes the browser.
Because this test is automated, it can run faster than if it were performed manually.
This is just a simple example, in a real-world use-case, you can have multiple scenarios, multiple test cases, and multiple test suites which can be automated and run in parallel, increasing the overall efficiency of the testing process.
It's worth noting that, if you are working with a web application, there are other frameworks and tools that can be used in conjunction with or instead of Selenium, such as Cypress, WebDriverIO, and TestCafe, each one having its own specific features and advantages.
Improves Accuracy
Automated tests are less prone to human error than manual tests, which can help improve the accuracy and reliability of the testing process. Automated tests are repeatable, consistent and reliable which helps in reducing false positives and false negatives.
import com.thoughtworks.gauge.Step
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
class AutomationTest {
private lateinit var driver: WebDriver
@Step("Open the browser and navigate to the website")
fun openBrowser() {
System.setProperty("webdriver.chrome.driver", "/path/to/chrome/driver")
driver = ChromeDriver()
driver.get("https://www.example.com")
}
@Step("Search for <searchTerm> and verify the result")
fun search(searchTerm: String) {
driver.findElement(By.name("q")).sendKeys(searchTerm)
driver.findElement(By.name("btnK")).click()
val searchResult = driver.findElement(By.xpath("//div[@class='g']"))
assert(searchResult.text.contains(searchTerm))
}
@Step("Close the browser")
fun closeBrowser() {
driver.quit()
}
}
In this example, the script not only performs a search, but also verifies the results by asserting that the returned search results contain the search term. This helps ensure the test is accurate and will catch any issues that may occur with the search functionality.
Because the script is automated, it will run the same way every time it is executed with the same inputs, ensuring the test is repeatable, consistent and reliable. This helps in reducing false positives and false negatives, which can occur with manual testing.
Additionally, you can use test reporting tools like Gauge report, Allure, TestNG, JUnit, etc. to track test results, allowing you to see if a test has failed or passed. If it has failed, it will reveal the reason behind the failure, which helps in identifying issues early in the development process and prevents them from becoming more serious problems later on.
3. Enables Frequent Testing
Automated tests can be run on a regular basis, such as every time code changes are made, which can help identify issues early in the development process and prevent them from becoming more serious problems later on. This aids in catching bugs early in the development cycle and helps in reducing the overall cost of fixing bugs.
import com.thoughtworks.gauge.Step
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.BeforeEach
class AutomationTest {
private lateinit var driver: WebDriver
@BeforeEach
fun openBrowser() {
System.setProperty("webdriver.chrome.driver", "/path/to/chrome/driver")
driver = ChromeDriver()
driver.get("https://www.example.com")
}
@Test
fun testSearch() {
driver.findElement(By.name("q")).sendKeys("searchTerm")
driver.findElement(By.name("btnK")).click()
val searchResult = driver.findElement(By.xpath("//div[@class='g']"))
assert(searchResult.text.contains("searchTerm"))
}
@AfterEach
fun closeBrowser() {
driver.quit()
}
}
This script uses JUnit5 framework and the @Test
annotation, which marks a method as a test method. This allows the test to be run automatically as part of a test suite. Additionally, the script uses the @BeforeEach
and @AfterEach
annotations that marks a method to be executed before or after each test method.
You can also use a CI/CD pipeline and integrate the automation test with it, allowing the tests to be run automatically as part of a build process and with each code change. This helps in ensuring that the code changes are tested and are of good quality before being deployed to the production.
4. Cost-effective
Automated tests can run without human intervention, which reduces the cost of testing over time. Automated tests can be run on a regular basis and can be scheduled to run at a specific time which reduces the need for human resources.
import com.thoughtworks.gauge.Step
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.BeforeEach
class AutomationTest {
private lateinit var driver: WebDriver
@BeforeEach
fun openBrowser() {
System.setProperty("webdriver.chrome.driver", "/path/to/chrome/driver")
driver = ChromeDriver()
driver.get("https://www.example.com")
}
@Test
fun testSearch() {
driver.findElement(By.name("q")).sendKeys("searchTerm")
driver.findElement(By.name("btnK")).click()
val searchResult = driver.findElement(By.xpath("//div[@class='g']"))
assert(searchResult.text.contains("searchTerm"))
}
@Test
fun testSignUp() {
driver.findElement(By.linkText("Sign Up")).click()
driver.findElement(By.name("username")).sendKeys("myusername")
driver.findElement(By.name("password")).sendKeys("mypassword")
driver.findElement(By.name("submit")).click()
val message = driver.findElement(By.xpath("//div[@class='message']"))
assert(message.text.contains("Welcome myusername"))
}
@AfterEach
fun closeBrowser() {
driver.quit()
}
}
In this example, we have two test cases. The first one is for testing search functionality and the second one is for testing the sign-up functionality. By having multiple test cases in a single script, it increases the overall efficiency of the testing process, which can help reduce the overall cost of testing.
Additionally, you can use test parameterization where you can run the same test case with multiple inputs and data sets. This increases overall test coverage and helps find more bugs, reducing the overall cost of testing.
Further, you can use cloud-based test environments such as SauceLabs, BrowserStack, and TestingBot, allowing you to run your tests on a variety of browsers and operating systems without having to maintain your own test infrastructure. This can reduce the costs associated with maintaining and scaling test infrastructure.
Overall, automated tests can be cost-effective by reducing the need for human resources, increasing the efficiency of the testing process, and by using cloud-based test environments.
5. Increases Coverage
Automated tests can cover a wide range of scenarios, inputs and use cases which are difficult to cover manually, thus increasing the overall coverage of the testing. Automated tests can cover different browsers, different devices, different versions of the operating system, different data sets and different scenarios which increases the overall coverage of the testing.
import com.thoughtworks.gauge.Step
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
class AutomationTest {
private lateinit var driver: WebDriver
@BeforeEach
fun openBrowser() {
System.setProperty("webdriver.chrome.driver", "/path/to/chrome/driver")
driver = ChromeDriver()
driver.get("https://www.example.com")
}
@ParameterizedTest
@CsvSource(value = ["searchTerm1, expectedResult1", "searchTerm2, expectedResult2", "searchTerm3, expectedResult3"])
fun testSearch(searchTerm: String, expectedResult: String) {
driver.findElement(By.name("q")).sendKeys(searchTerm)
driver.findElement(By.name("btnK")).click()
val searchResult = driver.findElement(By.xpath("//div[@class='g']"))
assert(searchResult.text.contains(expectedResult))
}
@AfterEach
fun closeBrowser() {
driver.quit()
}
}
In this example, we are using the JUnit5 @ParameterizedTest
annotation and @CsvSource
to run the same test case with multiple inputs, increasing the overall test coverage. The test will run three times with different input values, and each time it will check if the output matches the expected result.
This way you can test the same functionality with multiple data sets, which helps in finding more bugs and increases the overall test coverage. Additionally, you can also use data-driven testing frameworks like TestNG, JUnit5, etc. to run the test cases with multiple inputs, increasing the overall test coverage.
6. Improves Consistency Automated tests run in the same way every time, ensuring the same issues are not missed repeatedly. Automated tests are repeatable, consistent and reliable which reduces false positives and false negatives.
import com.thoughtworks.gauge.Step
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
class API_AutomationTest {
@Step("GET request to <endpoint> and verify the response")
fun testAPI(endpoint: String) {
val client = OkHttpClient()
val request = Request.Builder()
.url(endpoint)
.get()
.build()
val response = client.newCall(request).execute()
val json = response.body()?.string()
assert(json!!.contains("\"userId\": 1"))
}
}
This script uses the Gauge framework and the @Step
annotation, which marks a method as a step in the test scenario. This allows the test scenario to be written in a readable and understandable format, making the flow of the test easy to understand.
You can also use the gauge's concept of data-driven testing, where you can run the same test scenario with multiple inputs, increasing the overall test coverage and potentially finding more bugs.
Additionally, you can use the gauge's reporting capability, which provides a clear and concise report of the test scenarios, test results and the test execution time.
7. Enables Continuous Integration and Continuous Delivery
Automated testing is a key enabler for continuous integration and delivery, which is a critical aspect of modern software development. Automated tests can be integrated with the CI/CD pipeline and can be run automatically on every code change, ensuring the code changes are tested and are of good quality before being deployed to the production.
You can use a CI/CD pipeline and integrate the script with it, allowing the tests to be run automatically as part of a build process, and with each code change.
For example, you can use Jenkins, Travis, CircleCI, etc. as a CI/CD tool. Then you can set up the job to build the project, run the test cases and deploy the application to the production if the tests pass.
This is a critical aspect of modern software development, and it helps in achieving a faster and more reliable software delivery.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh './gradlew build'
}
}
stage('Test') {
steps {
sh 'gauge run specs/'
}
}
stage('Deploy') {
steps {
sh './deploy.sh'
}
}
}
}
This Jenkinsfile defines a pipeline with three stages: Build, Test and Deploy.
In the Build stage, the pipeline runs the command './gradlew build'. This command will build the project and generate the necessary artifacts.
In the Test stage, the pipeline runs the command 'gauge run specs/'. This command will execute all the gauge test cases in the specs folder.
In the Deploy stage, the pipeline runs the command './deploy.sh'. This command will deploy the application to the production environment.
You can set up the Jenkins job to run this Jenkinsfile and it will build, test and deploy the application if the tests pass.
With this example, you can see how you can use the Gauge tests in a Jenkinsfile to enable continuous integration and continuous delivery. This allows you to automate the testing process.
Increases efficiency by allowing multiple test cases to be run in a single script.
Improves accuracy by reducing human error and providing consistent results.
Enables frequent testing by making it easy to run tests on a regular basis.
Is cost effective by reducing the overall cost of testing and increasing test coverage.
Increases coverage by allowing the same test case to be run with multiple inputs and data sets.
Improves consistency by ensuring that the software is working as expected.
Enables Continuous integration and Continuous delivery by allowing the tests to be run automatically as part of a build process, and with each code change, ensuring the code changes are tested and are of good quality before being deployed to the production.
Overall, automation testing helps in improving the overall quality of the software, reducing the overall cost of testing, increasing the test coverage, improving consistency and enabling continuous integration and continuous delivery which results in faster and more reliable software delivery.