Introducing: Examples of automated test cases

After introducing Selenium, it’s time for some practical, a bit more advanced tasks. In this article, we’ll analyze our own blog for automated tests. We’ll also present exemplary tests using a Selenium WebDriver framework.

Let’s get started

After creating the above class with:

  • defined web URL
  • initiated _driver and _wait
  • created Setup and TearDown

we can move on to creating tests.

First, we create a test script for one of the most exposed elements on the blog — the header. The purpose of this test is to verify if the included text is just as expected.

In this example, we can’t get the content of a text box from a single element. That’s why we need to verify the elements’ implementation on the website. In this particular case, site’s construction causes that the header is embedded in three elements:

Because of the site’s construction, to verify the header’s content, we need to define and verify its individual components in the test script. Then, it’s necessary to perform an assertion. In the above example, we used the Fluent Assertions library, which made the expected result more readable and clear. And because we verified more than one component, we needed to create the AssertionScope. It’s also worth mentioning that the size of letters is not relevant for the “BeEquivalentTo” method — if there’s a need to verify the letters’ size in the text, we should use the “Be” method.

Checking the menu

In the next example of the test script we’ll check the site’s menu. We’ll verify the order of the menu elements display. In this case, it’s necessary to go through the entire list of menu elements. That’s why we used test cases — a specified set of input data passed in the method’s parameters. Such a notation simplifies the readability of the test. The action is launching the test method as many times as the number of defined Test Cases. Nevertheless, each launch passes other parameters to the method — first the index is 0, and the expected name of the option will be “PROJECTS”, for index = 1 an option should be named “SERVICES”, etc.

Every blog site should show five blogposts, and each of them should be openable to read its content. The test script below aims at verifying the correctness of this action. In this case, we also used test cases and the AssertionScope.

Verifying the “Contact” info

Another test relates to the “Contact” button directing to the contact form. The purpose of the script is defining and supplementing the fields with data needed to send the form and sending it. There’s also a matter of assertion connected with whether the message was sent correctly and whether a “thank you for the contact” note was displayed.

A visitor of the site can subscribe to the newsletter. The test verifies if the person who already subscribes gets adequate information about already being on the mailing list.

Lastly, an example of the fact that one should think while writing tests. Although it’s written correctly and fulfills its purpose, it’s still hard to catch what an author had in mind because of a multitude of variables and operations:

Summing up

Going through the above tests, one can notice that some elements are repeated. Apart from that, if we need to search for many elements in one test, we can get confused with its content. Using good practices, we facilitate our own work and the work of other teammates — the code is easy to understand even after a longer period of time. We avoid repetition and separate the elements that are commonly used. One of the design patterns we can use while creating tests in Selenium is Page Object Model — we’ll present it in our next piece!

human-centric software design & development. check out our website: www.iteo.com