开发者

Java Annotations removing string literals from them?

开发者 https://www.devze.com 2023-04-10 08:22 出处:网络
Not sure if this is a decent question or not but here it goes.We are trying to implement a UI testing framework (selenium web-driver) and want to use a Page driven design for example

Not sure if this is a decent question or not but here it goes. We are trying to implement a UI testing framework (selenium web-driver) and want to use a Page driven design for example

class HomePage {
@FindBy(how = How.Id, id="myPageHeaderID")
private String pageHeader

In the simple example above I need to hard-code th开发者_如何学JAVAe "myPageHeaderID" string literal. One of the requirements proposed is that we be able to pull in the "myPageHeaderID" from a property for both maintenance reasons (no code deploy if something changes) and for internationalization reasons. I have been searching around and probably not doing a proper search but is there any way of doing what I am asking above?


I briefly went down this route, but due to our application it wasn't quite achievable (pages aren't always displayed in the same order once you've visited a page).

public class PageElement implements WebElementAdapter, Locatable {

  private How how;
  private String using;
  private boolean required;

  @FindBy(how = How.ID_OR_NAME, using = DEFAULT_LOCATION_STRATEGY)
  private WebElement backingElement;

  public PageElement(How how, String using using) {
    this.how = how;
    this.using = using;
    this.required = true;
  }

  /** 
   * This is how the overriding of the element location is done.  I then injected
   * these values in a spring configured bean file.
   *
   * This is needed on your config file:
   * default-lazy-init="true" default-init-method="initialize">
   */
  public final void initElement() {
    if (backingElement == null || isStale() {
      backingElement = getDriver().findElement(getLocationStrategy());
    }
  }

  public By getLocationStrategy() {
    By by = new ByIdOrName(using.replace(DEFAULT_LOCATION_STRATEGY, using));

    switch(how) {
      case CLASS_NAME:
        by = By.className(using.replace(DEFAULT_LOCATION_STRATEGY, using));
        break;
      //Do for others
    }
    return by;
  }

  public WebElement getBackingElement() {
    return backingElement;
  }
}

public interface WebElementAdapter {
  WebElement getBackingElement();
}

public interface Locatable {
  By getLocationStrategy();
}

I then created common widgets in POJOs, and injected these into page objects which were a collection of these widgets.

From there I had a simple test harness which was responsible for taking in strings (which were then executed. Basically it allowed for test cases to be written in SpEL and act on the beans which were injected.

It was what I thought a pretty neat project, but I had to shelf it to get some other things done.


Annotations are essentially metadata. Taking database metadata for example, it would be weird if Oracle database would turn into MySQL, right? Here is the article about Annotation Transformers in TestNG. Didn't try it myself, but I think it could be implemented in some way or another.


AFAIK, you can call a method from the Annotation.

@FindBy(how = How.Id, id=getProp())
private String pageHeader;

private String getProp()
{
    String prop = //whatever way you want to get the value
    return prop;
}

Doesn't that work?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号