Automating Selenium Webpage Testing with Perfecto.io
As a Frontend Web Developer, it is key for you to ensure that your site works seamlessly for all users. But all your users will not act the same way. They will access your site from varied browsers and devices.
Users are not patient enough to work around your buggy frontend. If they don’t find what they are looking for or if your site causes them even a small bit of frustration, they will bounce. In fact, users form a judgment about the site they visit in the first 50ms of their landing. That is a concise time span.
You must test your site on every possible variation of browser that is out there. But, how would you do that? It is not even practical to have all the test devices physically for testing. Firstly, it will be expensive and secondly, time-consuming. How are you going to manage testing on all these devices? Manually? No way.
What you need in such a scenario is a cloud-testing framework such as Perfecto, AppPerfect, TestSigma, or UFT One. I personally like the ease, convenience and reliability that Perfecto provides, so we will take a look at it in detail.
I will demonstrate how you can spawn a test case across various devices using Perfecto’s cloud testing environment.
We will also look at some really helpful features that Perfecto provides for reporting, analysis and manual debugging.
Enter Perfecto.io
Perfecto provides a cloud-based testing platform for running your test automation scripts on various devices, both desktop and mobile. The most useful feature of the Perfecto platform is the host of devices it provides to test upon.
You can pick from 58 Android models and 54 iOS models to run your automation tests on. Additionally, you can pick from all major browsers available on Windows and Mac systems for testing.
How To Get Started With Perfecto?
You can register for a free trial for Perfecto from their homepage. The 14-day free trial provides you with 240 hours of testing time across all devices. That is good enough to execute your test cases and acquaint yourself with the powerful features of this platform.
Get Your Security Token
To run automation tests remotely on Selenium, you will need to generate a Security Token after logging into the platform. To get the security token for your account, click on your profile name at the top right of the screen and then click on ‘My Security Token’. A modal will open displaying your security token, which you can copy to include in your script.
Planning Your Automation Script
We will be scripting our test cases to use the unit testing framework, TestNG, so that they can be run as a part of your unit testing scripts.
Our Test Case
We are picking a simple case for this demonstration. The test script is as follows:
Visit https://google.com
Verify that the title equals “Google”
Picking Your Test Device
You can pick from the many options provided by Perfecto. We will be using IE 11 on Windows. We need to indicate to the Perfecto server, which device we would like to use for our testing. This can be indicated using the DesiredCapabilities class provided in the Selenium Library (org.openqa.selenium.remote.DesiredCapabilities).
Perfecto generates this code snippet for you, so that makes the job easy. To get the code snippet for your device of choice, just go to the Manual Testing Screen on the Perfecto platform, select your device and its properties from the available options, and then click on the ‘View Capabilities’ link at the bottom of the screen.
You should now see the code snippet on the right-hand side.
Ensure that the language dropdown is correctly set to Java. Here is a code snippet for IE 11 on Windows 10:
Test Reporting
Perfecto provides a reporting API to manage your automation test results. We will be using this API to generate reports for the tests performed. This file contains the code needed to setup the reporting client.
Start Coding
Our code will work in two phases: Testing & Reporting. Below is the code for the Testing part. I have added comments inline for you to understand the code:
After the test has been executed, we will write a function with TestNG @AfterMethod annotation where we will perform the reporting activities.
Here’s the code for the reporting function:
Executing Your Test Script
If TestNG is setup correctly within your IDE, you should see a Run Test icon besides the class declaration like the one below:
After you execute your test script, the TestNG Run window will show the result of your test case:
Accessing Your Report on Perfecto
You can now go to your Report Library by clicking on the ‘Test Analysis’ link in the dropdown available on the top of the screen:
This is how the test report looks like in the report list:
Clicking on the report name opens up the detailed report for your test, which shows your Test Flow, Test Status, the device parameters used and a screen recording of the test.
This Github repository contains the source code to run this test case on 11 different devices.
Leveraging TestNG To Scale Your Automation
You can create classes for specific instances of devices on which you would like to test your script, similar to what you can see in the repository mentioned above. Once that is done, you can include the following TestNG.xml in your project root to run all your test cases at once.
To run the test case on all devices, simply right-click the TestNG.xml file and select Run. Once the tests are completed, their results will be available on Perfecto Test Analysis page.
Below is a screenshot showing the results for running the test case on 11 different devices:
As you can see from the timestamps in the screenshot, the execution of the test case across 11 different devices takes about six minutes, which is pretty decent.
Move Your Testing to The Cloud
All aspects of technology are moving to the cloud, then why should testing still be done the old school way. Platforms like Perfecto provide the perfect ingredients to succeed at this move to the cloud.
If you have been testing the old school way, give an automated testing tool a try using the scripts in this repository as a template. Once you realize the speed, convenience and efficiency this brings to your tests, there is no turning back.
Disclaimer: I received a request to review Perfecto’s free test automation tool.
The fact I got it this request has not influenced the review in anyway.