Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 16, 2019 10:46 pm GMT

Simplifying Automation with Visual Validation

When automating scenarios, we have to be very careful to capture all of the various verification points that a tester would process when executing a test. Ive written about this issue in Adding a Peripheral View to Test Automation, and stressed how important it is to make sure we script all necessary assertions into a given scenario. But Ive found a better way to do this! Visual UI testing.

As an automation engineer, I feel immense pressure to think outside of the box and ensure no assertions are omitted. Visual validation relieves this stress and makes my automation script a lot faster and simpler.

Let me show you what I mean.

Heres an application that Ive built for some of my UI automation workshops. It is a simple grid showing books on test automation. By searching in the textfield, the user can filter the books to just the ones that match the query.

Grid with 8 books. Search field to filter boooks

WITHOUT VISUAL VALIDATION

Without visual validation, I would write a scenario to make sure the book(s) that Im searching for are the only ones visible after my search completes. In this particular scenario, I wrote the following to make sure that when I search for Automation, the two books that I expect are the ones that are shown.

Search for Automation returns 2 books

  @Test  public void testSearchByPartialTitle() {    String title1 = "Test Automation in the Real World";    String title2 = "Experiences of Test Automation";    page.search("Automation");    assertTrue("Book not found: " + title1, page.isBookVisible(title1));    assertTrue("Book not found: " + title2, page.isBookVisible(title2));    assertEquals("Number of visible books is incorrect", 2, page.getNumberOfVisibleBooks());  }

To support this scenario, I needed to add two methods to the Page Object class in my framework: isBookVisible and getNumberOfBooks (plus an additional supporting method as well as element locators):

  public boolean isBookVisible(String title){    List<WebElement> books = findVisibleBooks();    for(WebElement book : books) {      if(title.equalsIgnoreCase(book.findElement(titleAttribute).getText())){        return true;      }    }    return false;  }  public int getNumberOfVisibleBooks() {    return findVisibleBooks().size();  }  private List<WebElement> findVisibleBooks() {    return driver.findElements(visibleBooks);  }

Even with all of this code, I am only validating that the title of the book is correct. Theres so much more on the screen that could be wrong like the authors, price, book coverand that doesnt even begin to cover the overall look and feel!

WITH VISUAL VALIDATION

To fully verify the books properties, our methods would need to be a lot more robust. But instead of adding more code, I can actually remove code and gain even more coverage with visual validation.

Heres my revised test method. Notice that Ive replaced the three lines of assertions from above with three lines of visual validation, and with this swap, I get much more coverage for my test. This swap also eliminates the need for the Page Object code shown above, so I no longer need to maintain that. Thats 19 lines of code plus the 2 lines of element locators that I can delete. Now, this scenario verifies the entire page, ensuring not only that my books (and all of their properties) appear after search, but that the look and feel is perfect as well.

  @Test  public void testSearchByPartialTitle() {    page.search("Automation");    eyes.open(driver, "Automation Bookstore",  "testSearchByPartialTitle");    eyes.checkWindow();    eyes.close();  }

I can even make this test method smaller by abstracting out the visual validation code, especially since this will be reused across a bunch of my tests. In a Base test class, I can move the three lines of visual validation code into a utility method. This way, any test class that inherits from this class will have access to this:

   protected void validateWindow() {        eyes.open(driver, "Automation Bookstore",  Thread.currentThread().getStackTrace()[2].getMethodName());        eyes.checkWindow();        eyes.close();    }

Then in the tests themselves, we are down to 5 lines of code, with the actual body only being 2 lines!

  @Test  public void testSearchByPartialTitle(){    page.search("Automation");    validateWindow();  }

This works beautifully by covering much more validation making sure everything we want to be there is actually there, making sure nothing we dont want to be there is not, and ensuring the overall look and feel of the page is what is expected. All with less code and less stress! Whats also great about this is it doesnt have to be a full replacement to the functional validations. I can easily keep any assertions I need (e.g. backend validation) and still use the visual validation for the rest. I can essentially have it all!

Originally posted on angiejones.tech


Original Link: https://dev.to/techgirl1908/simplifying-automation-with-visual-validation-4an6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To