Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Loadable Components

Mikołaj Mański edited this page Sep 7, 2016 · 9 revisions

Loadable Components. Bobcat Way.

Overview

The @LoadableComponent is an annotation that aims to make writing Page Objects with less boilerplate code for ensuring that some certain condition have been met before running some method. The annotation keeps information about condition implementation which should be evaluated before running method from WebElement interface. Consider the following scenario: we want to click on Confirm button but after it's got visible. The old approach would look like this:

@PageObject
public class CheckoutSection {

  @Inject
  private BobcatWait wait;

  @FindBy(css = "button.submit")
  private WebElement button;

  //..

  public void submitForm() {
    wait.withTimeout(Timeouts.MEDIUM).until(ignored -> button.isDisplayed());
    button.click();
  }
//...

}

Suppose that you have multiple buttons like this and each one of these needs the same verification before performing an action on it. Using Bobcat's Loadable Components you can extract this condition to an annotation.

@PageObject
public class CheckoutSection {

  @FindBy(css = "button.submit")
  @LoadableComponent(condClass = VisibilityCondition.class)
  private WebElement button;

  //..

  public void submitForm() {
    button.click();
  }
//...

Where VisibilityCondition class can be implemented like this:

public class VisibilityCondition implements LoadableComponentCondition {

  @Inject
  private BobcatWait wait;

  @Override
  public boolean check(Object subject, LoadableComponent data) {
    WebElement subject = (WebElement) object;
    return wait.withTimeout(data.timeout()).until(ignored -> subject.isDisplayed());
  }
}

From now on, each time before performing some action on button field, the provided Condition will be evaluated. The key is to implement the LoadableComponentCondition interface with check() method. The subject that you are getting form method parameters is fully initialized field on which you have put the @LoadableComponent annotation - in this case the button WebElement from CheckoutSection class. What we are getting here is less code with reusable component that we can put on any field we want. If you want you can put multiple @LoadableComponent annotations on a single field.

Loadable components hierarchy

You can put @LoadableComponent annotation also on Page Object fields. This entails support for hierarchical condition checking when we are using Loadable Components in multiple nested Page Objects. Let's consider the following class structure:

Page Objects

Condition classes are provided in the @LoadableComponent annotation like this:

@PageObject
public class A {
//...
  @FindBy(css = ".someClass")
  @LoadableComponent(condClass = Condition2.class)  
  private B b;
//...
}
@PageObject
public class B {
//...
  @FindBy(css = ".someOtherClass")
  @LoadableComponent(condClass = Condition3.class)  
  private C c;
//...
}

and so on. With such structure when invoking some method on Web Element button, the entire hierarchy of conditions is evaluated from the top to the bottom. In this case Condition4 would be evaluated as the last one.

Limitations

This feature has it's limitations which should be mentioned:

  1. You cannot inject two Page Objects of the same type in one class. Exception will be thrown in such case.
  2. Loadable Components are strongly connected to class fields which they are annotating. When functionality of some component depend on other (f.e. like before providing value to some text box, a certain button should be clickable) then assuring that dependency is met shouldn't be done using Loadable Components.
  3. The entire mechanism does not support the "lazy approach". It means that each condition or condition tree will be evaluated at each time.
  4. Implementations of LoadableComponentCondition should be public and have default public constructor only.

Getting started with Bobcat

  1. Getting started

AEM Related Features

  1. Authoring tutorial - Classic
  1. AEM Classic Authoring Advanced usage
  1. Authoring tutorial - Touch UI

Clone this wiki locally