简体   繁体   English

Java Annotations从其中删除字符串文字?

[英]Java Annotations removing string literals from them?

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 我们正在尝试实现UI测试框架(Selenium Web驱动程序),并希望使用例如页面驱动的设计

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

In the simple example above I need to hard-code the "myPageHeaderID" string literal. 在上面的简单示例中,我需要对“ myPageHeaderID”字符串文字进行硬编码。 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. 提出的要求之一是,出于维护原因(如果发生某些更改,不部署代码)和国际化原因,我们都可以从属性中提取“ myPageHeaderID”。 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. 然后,我在POJO中创建了通用小部件,并将它们注入到页面对象中,这些对象是这些小部件的集合。

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. 从那里,我有了一个简单的测试工具,该工具负责接收字符串(然后执行该字符串。基本上,它允许用SpEL编写测试用例并作用于注入的bean)。

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? 数据库元数据为例,如果Oracle数据库变成MySQL会很奇怪,对吗? Here is the article about Annotation Transformers in TestNG . 这是有关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. AFAIK,您可以从注释中调用方法。

@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? 那行不通吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM