11 2. Project organisation
jeangb edited this page 2017-06-27 17:43:16 +02:00

What is Page Object Model (POM)

POM is a design pattern to create object repository for UI elements. It allows to write more readable, maintainable and readable automated tests.
For each page (login page, home page, settings page for example) of the application under test, a class exists and references objects of the page (buttons, labels, textfields, checkboxes, containers, etc), and some page specific methods. Addresses of these objects are only referenced in these POM classes and test classes don't have to worry about how to accede these objects.

Example in riot-automated-test:


  • Some testcase snippet:

      RiotRoomPageObjects roomPageObjects = new RiotRoomPageObjects(appiumFactory.getiOsDriver1());
      // User writes something in the message bar but don't send it.
      roomPageObjects.sendKeyTextView.setValue(notSentMsg);
      [...]
      // Asserts pending call status
      roomPageObjects.getPendingCallView("Offline");
    

... uses sendKeyTextView object and getPendingCallView() method from RiotRoomPageObjects POM class.

  • RiotRoomPageObjects POM class snippet:

      [...]
      @iOSFindBy(accessibility="EncryptedRoomIcon")
      public MobileElement e2eIconImage;
      @iOSFindBy(accessibility="GrowingTextView")
      public MobileElement sendKeyTextView;
      [...]
    

Project file tree

/images/riot-automated-test_file_tree_larger.jpeg


What's inside a test class

/images/test_class_anatomy_with_annotations_cropped.jpeg

  • (1) Use the ScreenshotUtility interface. Mandatory to use the automatic screenshots functionality.
  • (2) Each test class extends the RiotParentTest. This class contains setup and teardown function allowing to start or stop the Appium drivers. These functions will be triggered according to the defined TestNG groups (see @Test annotation use (5)).
  • (3) Variables shared between tests and setup/teardown functions. In our configuration, we try to have dedicated riot users for each theme. Since our test classes are dedicated to a theme, the same user(s) are used for each test case of the class. Note that riotUserADisplayName and riotUserBDisplayName are used by the checkIfUserLogged function (12).
  • (4) Detailed test steps described in java doc.
  • (5) The TestNG framework used to handle the assertions, allows us to group and prioritize the tests thanks to its annotations @Test.
    The groups are defined to optimize the test logic. They allow to trigger only once a setup and a teardown for all the tests of a group. They are handled in the RiotParentTest class.
    In this example, "1driver_android" is used to start an Appium Android driver on the first test wearing the "1driver_android" key. This is done by calling a setup method located in RiotParentTest which has the @BeforeGroups(groups="1driver_android") annotation. When all the tests wearing the "1driver_android" key are executed, a teardown function in RiotParentTest with the @AfterGroups(groups={"1driver_android"}) annotation kills the driver(s).
    The attribute priority is used to order the test cases execution inside the class (0 for high priority). Here "priority=0" is optional because only one test is defined.
  • (6) Prepare the test case by using a private method (9). This allows to focus only on the core of the test.
  • (7) Instantiate a POM class, RiotHomePageTabObjects. An android Appium driver (from the appiumFactory singleton class) is passed to the POM constructor class. This driver is directly linked to the device under test.
  • (8) Verify the test with the Assert object from TestNG.
  • (9) Private method. It creates a room with user B and invites user A by using directly the requests to the homeserver (see utility.HttpsRequestsToMatrix class). This method is quicker than creating the room using the application under test.
  • (10) This method is executed after each test, thanks to the @AfterMethod TestNG annotation. According to the test name, it will execute the right teardown function.
  • (11) Private method. It cleans the data created to prepare the test (6).
  • (12) This method is very important to secure the test execution flow. It will make sure that the right user is logged on the right homeserver.