What is Website Test Automation?
Automation testing helps to relieve the bulk of tedious and repetitive human tasks. These days the majority of the software made available on the internet is introduced in the form of web-apps. Users use browsers like Chrome, Firefox, etc. to use these web-apps. Most users think of browsers as just a way to surf the web pages and display information. However, those with knowledge of web-app development know-how capable browsers are and how an ideal cross browser testing tool plays a vital role in developing browser-friendly web-apps.
With the right test automation framework or tool, QAs can automate repetitive browser actions like interacting with web elements or filling out long HTML forms. Automating tests help teams save time and effort, get faster and accurate results, increase software efficiency, and focus on innovation.
Incorporating web test automation doesn’t eliminate the need for planning test cycles. As a matter of fact, planning becomes even more crucial when automating test cases. One needs to have clarity on which tests are critical and need to be prioritized. Also, all automated test results must be closely analyzed by human eyes to derive maximum benefit.
It’s also important to bear in mind that automated testing doesn’t eliminate manual testing completely. In every testing pipeline, the latter has an important role to play. Refer to this Manual Testing tutorial to understand why manual testing can’t be avoided altogether.
Table of ContentsBenefits of Website Test Automation
Automated testing is extremely beneficial for validation in various phases of development. This helps teams uncover potential bugs faster, save time, and enhance communication among the developers, testers, and designers.
Although it may take more time to set up the test automation framework, the time is made up because test coverage rates are much wider, which means each test validates a greater number of features and functions. Since automation moves faster, the entire test suite takes less time to execute. Once automated tests are initiated, testers can focus on other tasks and evaluate test results once all the test cases are executed.
Automated tests help achieve excellent test coverage in shorter timelines. This helps deliver a quality product, thereby reducing the probability of bugs appearing post-release. This helps to save project costs.
The repetitive nature of automation helps reuse test scripts. This is extremely useful when new patches are developed, and the software needs to be tested as a whole again.
As discussed above, automation helps boost test coverage in restricted timelines. This helps QAs test newly developed features within tighter deadlines and release robust applications faster.
How to run website test automation with Selenium
Selenium is indisputably the most popular framework used for automated website testing across multiple browsers. Having said this, let’s go through seven fundamental steps to get started with test automation.
Let us see how we can use Selenium commands to test “Add To Cart” functionality using the BStackDemo application.
Prerequisites for setup:
1. Eclipse or any other IDE installed.
2. Install the TestNG library in Eclipse from the Eclipse marketplace and add it to the Project’s configuration.
3. Create a maven project from the File option on Eclipse.
Choose maven-archtype-quickstart 1.1 as an archetype.
Enter a valid group id and artifact id and click on Finish.
4. Add Selenium Java dependency and TestNG dependency in the pom.xml file and save it to download the dependencies.
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.8.0</version> <scope>test</scope> </dependency>
Expand Maven Dependencies folder under the project to check if all the dependencies are downloaded.
5. Create a package under src/test/java named “com.bstack.testcases” and under that package, create a test case as “AddToCart”
Refer to these articles on Selenium with Java or how to execute Selenium projects to understand the basic configuration for Selenium testing.
Below are the 7 steps required for a basic Selenium test which includes creating the WebDriver instance, using the WebDriver instance to perform actions on the web and finally asserting the test.
1. Create a WebDriver instance and initialise it
Initialising the WebDriver interface is the first step in writing a Selenium test script. One needs to create the instance of the WebDriver interface using the constructor of a specific web browser. This instance then helps to invoke other methods.
WebDriver driver; driver=new ChromeDriver();
2. Navigating to the desired web application URL
The next step is to navigate to a specific web page that needs to be tested. This is performed by invoking the get method of the WebDriver interface.
driver.get(“https://bstackdemo.com/”);
3. Locating specific Web Elements on the Web page and storing it in WebElement variable
To interact with a web-page, one needs to locate specific HTML elements first. Only then one can perform the desired operations. Refer to this article on locators in Selenium to understand different locator strategies like Xpath, id, name, etc. The findElement method of the WebDriver interface is used to locate elements.
WebElement firstAddToCartBtn = driver.findElement(By.xpath("(//div[@class='shelf-item__buy-btn'])[1];
4. Performing actions on HTML Elements
Once the desired element is identified, the next step is to interact with them using interaction methods. Interactions include entering text, clicking buttons, clearing text fields, etc.
firstAddToCartBtn.click();
5. Anticipating browser response
Once an operation is performed, for example, if a click operation is performed on the submit button, then one needs to wait for a second or two to get the server response.
To test server response, one needs to program the waiting time in the test scripts. WebDriver API supports two techniques for anticipating wait response: Implicit and Explicit Wait. Read this detailed article on Wait commands in Selenium to learn their implementation.
6. Asserting the test
In the previous steps, we have just performed the steps to execute the test. To verify that the given test is Pass or Fail, one needs to assert it against an expected result. In this case, we will assert that after adding the product to cart, product should be added to cart and this can be verified by checking the visibility of Checkout button.
WebElement checkoutBtn=driver.findElement(By.cssSelector("div.buy-btn")); Assert.assertTrue(checkoutBtn.isDisplayed());
If the product is added successfully to cart, the checkout button should be visible and the above assert should return value true. In either case it should return false.
7. Terminating the test
Once all the test cases are executed, the test can be concluded by invoking the quit method. The quit method closes all active pages, terminates the browser, and ends the connection with the server that interacts with the browser.
driver.quit();
Complete Code:
package com.bstack.testcases; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class AddToCart { WebDriver driver; @Test public void addToCartTest() throws InterruptedException { driver=new ChromeDriver(); driver.get("https://bstackdemo.com/"); Thread.sleep(2000); WebElement firstAddToCartBtn=driver.findElement(By.xpath("(//div[@class='shelf-item__buy-btn'])[1]")); firstAddToCartBtn.click(); WebElement checkoutBtn=driver.findElement(By.cssSelector("div.buy-btn")); Assert.assertTrue(checkoutBtn.isDisplayed()); driver.quit(); } }
To get started with the basics of Selenium testing, have a look at the video below. It discusses in detail what Selenium is, how you can set up and write your first test, what to look for in frameworks, and how to pick the best framework for you.
Best Practices for Automated Web Testing
Here are some of the key best practices that you should follow for Automated Web Testing:
The Role of Real Browsers & Devices in Website Test Automation
Comprehensive and accurate website test automation, especially when it includes cross browser testing requires real browsers and devices. Testers need to check how the software renders and operates in real user conditions, for which they need to test on multiple unique browser-device-OS combinations.
To do so, QAs must run automated website tests on real browsers, devices, and operating systems. The software will have to render perfectly on each device, browser, and browser version, considering their technical variances and idiosyncrasies. Given device fragmentation, QA teams need access to a massive on-premise digital lab (constantly updated with newer devices) to perform satisfactory automated cross browser compatibility testing.
Not every organization has the finances or the human resources to set up and maintain such a lab, and they don’t have to. They can use BrowserStack, a cloud-based testing platform offering access to a real device cloud of 3000+ real browsers and devices for website test automation. Be it manual testing, automated Selenium testing, automated visual testing, or Cypress testing, testers can utilize BrowserStack’s infrastructure to get 100% accurate results in real-world circumstances. They can also use Puppeteer testing and Playwright testing.
Additionally, testers can also access multiple features designed to make testing faster and more convenient – integration with popular CI/CD tools like Jenkins, Travis, Circle CI to ensure a robust automation pipeline.
Run Selenium Tests on Real Browsers
Automating test cycles has become a basic necessity for teams operating in an agile ecosystem. However, all automated tests must be run on real devices and browsers, because there is no other legitimate way to monitor website behavior in real-world scenarios. Not only does this enable more effective use of time and effort, but it also ensures that speed does not come at the cost of the accuracy of results.
ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6wnJtlkarBsLnAraCopg%3D%3D