17.1 User Interface Map

Selenium WebDriver API uses locator information to find elements on the page. Defining the locator information in the classes may result in duplication of element location information in various classes. Because of such duplication, it becomes increasingly difficult to maintain tests as the size of the test suite increases. Any change in element locator information requires updating of the element locator information in all the classes wherever it has been defined. Identifying classes which require an update and then updating them is a very time consuming and difficult task. In short, change identification and change implementation become a nightmare.

One way to solve this problem is to use the Page Object Model framework (Agile Automation Framework). In this framework, each page is used as a reusable class. All the elements of the page are defined in the same page class without any duplication. In order to avoid locator information duplication within the page class, the locator information is defined at the top of the page using @FindBy method.

Another way to resolve this problem is to create a repository which defines locator information of all the elements. This element repository is termed as Object or UI Map. A user interface (UI) Map helps in creating a one-point maintenance framework for elements' locator information.

UI Maps are easier to maintain. Maintaining all locator information in one place also helps to avoid duplication of elements' locator information. Whenever a change to a specific elements' locator information is required, it can be easily identified and changed in the UI Map. Making changes in one file is much simpler and easier rather than making changes in multiple files (class files or tests). Since changes are to be done in one file, change identification and change implementation are easier. Also, the UI Map can be version controlled.


17.1.1 | Creating Selenium Extension for UI Map using a properties file

In the following section, we will discuss how to extend Selenium WebDriver to develop a UI Map for amazon.in site login page. Firstly, a UI Map cab be any flat file or .properties file. Locator information can be defined as key-value pairs in the UI Map file. This UI Map cab be imported in all the page class files which need to interact with application front-end.


17.1.2 | Defining Locator information in UI Map

Let us first create a UIMap.properties file. The locator information can be added to this file as key-value pairs.  Assume the format for the defining key-value pairs to be: 

Defining Locator information in UI Map

Now let us define the locator information for amazon.in login page.






16.2 Annotation

16.2 | Custom Annotations

Java provides the flexibility to define custom annotations as per need. An annotation is defined with the at-sign (@) preceding the interface keyword. The following steps show how to create and use custom annotations.

  • Open Eclipse
  • Select the package where annotation needs to be created.
  • Right-click and select New → Annotation as shown in figure

Creating a new annotation file
Creating a new annotation file

  • Specify annotation name say RegressionTest.
The following code shows the annotation code with no annotations and parameters.

RegressionTest
RegressionTest

Above annotation can be directly used in code as:

UsingAnnotation
Use of Annotation

  • Annotate the annotation as required.
The following code shows how to create an annotation which defines the test type being automated (functional or regression). This annotation also defines the retention policy and the applicability of the TestDetails annotation.

Annotate the Annotation

  • Define the parameters of annotation, if any.
The following code below shows how to define two parameters for test information test developed by and test modified by.

Parameters of annotation
Parameters of annotation

  • Use the annotation while developing code wherever required.
The following code uses the annotation TestDetails for test 'testPurchaseOrder'.

Use of Annotation
Use of Annotation

16.1 Annotation

An annotation is a form of metadata that provides data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code which is annotated. The annotation has a number of uses and few of them are as follows: 

  • Annotation can be used by the compiler to detect errors or suppress warnings.
  • Annotations can be examined at runtime.
  • Software tools can be written that process annotation information to generate code, XML files, etc.

An important use of annotation in test automation is to define the test type (functional, regression) and test functional area (customer accounts, funds transfer, etc., for a banking application). Once this information is defined as an annotation in tests, the tool can be written to selectively execute a specific type of test. Also, the tool can provide info about the test coverage in each functional area.

16.1 | Built-in Annotations

Java provides some built-in annotations. Few of them are listed below:

  • @Override: Verifies that the specific method is an override. It causes a compile error if the method is not found in one of the parent classes or implemented interfaces.
  • @Deprecated: Marks the specific method as obsolete. It causes a compile-time error if the specific method is used.
  • @SuppressWarnings: Instructs the compiler to suppress the compile-time warnings which match the annotation parameters.

Section 'Interfaces' shows the use of @Override annotation.

Annotations can also be used to annotate annotations. Listed as follows are few annotations that define the behavior of an annotation:

  • @Retention: Specifies how the annotation is stored- whether in code only or compiles into the class or available at runtime through reflection.
  • @Documented: Marks another annotation for inclusion in the documentation.
  • @Target: Marks another annotation to restrict the kind of java elements the annotation may be applied to.
  • @Inherited: Marks another annotation to be inherited to a subclass of annotated class.