Tuesday, July 31, 2018

30 Days of Automation in Testing | Testing Made Faster

Speeding up automated checks & execution time

The problem(s):

  1. Too many functions happening at the same time
  2. A lot of verbose syntax and unnecessary UI waits anticipating lags from the back-end.

  3. Too much of the Step > verifyThis() > doThis() > Step_

  4. Another thing that choked the script was adding assertion / verification checks for each step of the test.
    What ended up happening was an unnecessary delay in test execution often leading to false-negatives or script failures.

  5. Spaghetti code
  6. As newbs to automation, the mistake I've seen is poorly written tests with a superfluous amount of steps and repetitive code to accomplish the simplest of tasks.
    I was guilty of this for a while. Until I learned how to abstract my scripts into distinct code-blocks.

The Solution:

So what worked for me was changing how I approached test composition with a strong focus on making it legible so that anyone can look at the test and know what's going on.

Before, a lot of times, a test (written in JS, for example) would look something like this:

driver.setUp(); driver.getUrl(“http://example.com”); driver.findElementById(’//a[@text=“register”]’).clearText(); driver.findElementById(’//input[@id=“textBox”]’).clearText(); driver.findElementById(’//input[@id=“textBox”]’).sendKeys(“verbose code”); driver.findElementById(’//button["@id=“submit”]’).click(); var pageTitle = driver.findElementByTag(’//h1[@class=“pageHeader”]’).getText(); assert pageTitle == “Success Page” driver.tearDown();

As you can see, the test is a bit hard to read and not very descriptive of what is going on. Having applied the Single Responsibility Principle, I rewrote the test to look something like this:

Using Katalon

WebUI.openBrowser( pageUrl );
WebUI.click( registrationLink );
onRegistrationForm.CompleteAndSubmitForm();
onProfilePage.VerifyInfo();

Notice the following:

  • pageUrl - I didn't need to explicitly write out the site url, I can declare a variable and reference it

  • registrationLink - as stated, I declare my variable elsewhere and call it here

  • onRegistrationForm || onProfilePage - these are classes I create in a separate file and import it as part of a function (package).

  • SubmitForm () || VerifyInfo () - Each class has a set of methods. A class can have many functions, but there should not be any occurrences of multiple classes on the same file (_hence SRP_). The exception being helpers .. but that's another topic.

  • I also reduced unnecessary element checks on the test and have them happening as separate actions in the aforementioned classes. The end result makes it easier to maintain the test by fixing only the function that needs it.

Conclusion:

While the "Solution" example was written in Groovy, I've applied similar concepts when writing tests in JS or Python. I like that the test is readable and that anyone can see what I'm testing in seconds.

It makes it easy to pair the test scenario with the acceptance criteria to ensure the proper workflows are being tested.

It also makes it super-simple for anyone inheriting my project to see what I'm doing and pick-up where I left off. An import thing for teams.

No comments:

Post a Comment