Java注释从中删除字符串文字?

时间:2011-09-30 16:35:49

标签: java annotations

不确定这是否是一个不错的问题,但在这里它。我们正在尝试实现UI测试框架(selenium web-driver),并希望使用页面驱动设计,例如

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

在上面的简单示例中,我需要对“myPageHeaderID”字符串文字进行硬编码。提出的要求之一是我们能够出于维护原因从属性中提取“myPageHeaderID”(如果出现更改,则不进行代码部署)以及出于国际化原因。我一直在寻找并且可能没有进行适当的搜索但是有什么方法可以做我上面提到的问题吗?

3 个答案:

答案 0 :(得分:0)

我简要介绍了这条路线,但是由于我们的应用程序,它并不是很容易实现(一旦你访问了一个页面,页面并不总是以相同的顺序显示)。

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();
}

然后我在POJO中创建了常用小部件,并将这些小部件注入到页面对象中,这些对象是这些小部件的集合。

从那里我有一个简单的测试工具负责接收字符串(然后执行。基本上它允许测试用例写入SpEL并对注入的bean进行操作。

这是我认为一个非常整洁的项目,但我不得不搁置它以完成其他一些事情。

答案 1 :(得分:0)

注释本质上是元数据。以数据库元数据为例,如果Oracle数据库变成MySQL,那会很奇怪,对吗?以下是TestNG中关于注释变形金刚article。没有自己尝试,但我认为它可以以某种方式实现。

答案 2 :(得分:-1)

AFAIK,你可以从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;
}

这不起作用吗?