Blog ·

Real-world Selenide: buying air tickets

testing airtickets.ru

Hi!

Sometimes you want to see a framework used in real battle conditions, not just hello-worlds with kittens. So why not!

In the Selenide Examples series, today we will test a completely real website, airtickets.ru, with real purchases, money and cards. Just like in real life.

For a change, let’s try the PageObject pattern, in several variations:

What we test

The test buys round-trip air tickets from St. Petersburg to Moscow, entering all the necessary values, and gets to the “Buy” button after filling in the credit card details.

Here is how it looks:


How it works

Let’s look at how this test works inside.

The project on GitHub

What is the PageObject pattern

PageObject is a way of working with a web UI where the selectors (locators) of page elements are defined in a separate class. This is done so that when the HTML structure changes, you only need to update the selectors in one place instead of searching for them all over the code.

Which pages we have

As you can see in the video, we work with 5 pages.

The test itself (class TestBuyingTickets) describes the logic of the whole test. The method names make it clear what each line does. Let’s look at some of them in more detail:

The first line of the test

opens the page and, in the same line, “puts” all the data about the elements and logic of the first page into the variable firstPageDirection.

  FirstPageDirection firstPageDirection = open("http://www.airtickets.ru/...", FirstPageDirection.class);

Now

we can call the method we need, because Selenide will try to find the selectors we need in the browser:

  • set the departure city:
    firstPageDirection.addDirectionFrom("Санкт", "LED");
  • set the arrival city:
    firstPageDirection.addDirectionTo("Москва", "MOW");
  • …

It’s worth explaining why we need the while loop. Sometimes, after choosing flights and clicking “Next” on the second page, a rare message may pop up saying that the flights are not available, suggesting you choose other flights. This loop checks that this message did not appear. If it does appear, the loop repeats choosing a ticket for as long as the message keeps appearing.

The end of the test

is a check that the “Buy” button is displayed. In Selenide, this is very easy thanks to its powerful condition checking. The method name itself tells what it checks:

  fifthPage.getButton().shouldBe(Condition.visible);

Which page objects do we have here?

The first three pages are implemented as “Selenide-style” PageObjects. That is, we create a separate class for each page (FirstPageDirection, SecondPageVariants, …) and in each of them describe the logic we need as separate methods. The selectors of the necessary elements are written inside the methods.

For example, adding the departure point on the first page (class FirstPageDirection):

  public void addDirectionFrom(String city, String airport) {
    $("#from").setValue(city);
    $("#autocomplete").$(withText(airport)).click();
  }

Selectors

By default, $ (finding an element by selector) takes a CSS selector as an argument. So $("#from") means “find the element with ID “from”.

You can make sure that the “From” input really has this ID among its attributes: open the service page in any browser, hover over the “From” departure input, and choose “Inspect” in the context menu (right mouse button). The browser will open the HTML code of the page and highlight the input we need. Among the attributes of this element, there is id=“from”.

Page object methods

Next, we enter a value into this input with the method Selenide.setValue(String). Why is it written in one line? Because the first part, $("#from"), gives us a SelenideElement, which has the method setValue(String). You can also pass xpath as the argument of $(); then the call looks like this: $(By.xpath(....)).

In FirstPageDirection you can also see helper methods that are needed only for working with the first page: getting the current month from System. This is needed to check that the service automatically fills in either the current month or the next one as the departure month. The check is done with asserts in the test. For example, this line:

Assert.assertThat("Месяц отправления не равен текущему месяцу или следующему месяцу", 
  firstPageDirection.getMonthDeparture(), anyOf(is(firstPageDirection.getCurrentMonth()), is(firstPageDirection.getNextMonth())));

By the way, I highly recommend learning assertThat from hamcrest (which has long been part of JUnit). The difference between assertThat and assertTrue, assertEquals etc. is readability, and also that when a check fails, you always get messages like “got this, but expected that” in the logs. Very convenient.

Working with collections

The PageObject of the second page (SecondPageVariants) uses the Selenide method $$. It returns a list of all elements found by the given selector.

The task is to choose a random flight from the ones offered. To do that, we find all the flights on the page:

List<SelenideElement> radioList = priceTable.$$(".radio");

Here again we use a CSS selector, but this time we search for elements by the class attribute. We get a list of all .radio elements on the page and choose a random one:

SelenideElement radio = radioList.get((int)(Math.random() * (radioList.size()-1)));

(I simplified the description a bit, but it’s not hard to figure out how the logic really works.)

Classic page object with blocks

The fourth page (FourthPagePassengerInfo) is implemented as a classic PageObject with blocks. Blocks are separate groups of elements on a page that, for easier handling, can be moved into a separate PageObject (using encapsulation).

On the fourth page, we have two frames, for filling in passenger information and contact details. At the beginning of the class, we say that we are interested in two frames on the page and set selectors for them with the Selenium annotation FindBy:

  @FindBy(xpath = "(.//*[@class='frame'])[1]")
  private FramePassengerInfo passengerInfo;

  @FindBy(xpath = "(.//*[@class='frame'])[2]")
  private FrameContactPersonInfo contactPersonInfo;

As you can see, the type of the frames is neither WebElement nor SelenideElement, but a block for each frame, which you can find in the package ForthPagePassengerInfoElements.

Each block is described as a classic PageObject, with the @FindBy annotation and its own methods. To show that each frame is a block, it must extend ElementsContainer (in the class declaration: public class FramePassengerInfo extends ElementsContainer).

Summary

  1. I hope those who previously wrote tests in plain Selenium saw that Selenide lets you write tests with much less code.

  2. The PageObject pattern, like any pattern, is just an idea of how to write code. You can declare the selectors at the beginning with the @FindBy annotation (the classic way), or you can write a PageObject the way Andrei Solntsev suggests: a page contains methods, and the selectors are written inside them. You can even create a singleton that holds a map with all the selectors of your project, and in the test logic pull selectors from the map and pass them as the argument to Selenide’s $ (this is exactly the solution I used on my project, where there are lots of elements with similar names and few unique classes and IDs). The main thing is to try to define the selector for a specific element in one place, so that you, or the automation engineer who comes after you, can easily find that place and change the selector if the developers decide to change the HTML structure.

Vasily Kovalchenko