我如何在eclipse中创建代码生成器,可以生成有关类属性的代码

时间:2015-09-28 18:53:05

标签: java eclipse selenium junit

我是一名自动化测试开发人员,我使用selenium +页面对象模型+ Junit进行测试。这导致我遇到问题,在每个测试类中我都声明了这样的属性:

By miscellaneousContainerFinder = By.cssSelector(".MISCELLANEOUS_ROW");
By goodbyeTextContainerFinder = By.cssSelector(".GOOD_BYE_TEXT_ROW");
By driversBarriersContainerFinder = By.cssSelector(".DRIVERS_BARRIERS_ROW");

但更多属性,其中15-20个。令我头疼的是我需要为这些“By”的简单方法创建,例如:

    public String getEmailComment() {

        try {
            return descriptionTabContainer.findElement(emailCommentFinder).getText();
        } catch (NoSuchElementException e) {
            return null;
        }
    }

或点击或键入WebElement的方法。所以我想到了eclipse可以为属性生成getter / setter的方式,我知道我可以编辑这些代码的生成方式,并插入我喜欢的任何代码。但是,我如何添加新方法,同时指定它们将具有的名称和返回值?

我想选择生成这样的方法 - 生成getter / setter,生成clickMethod,生成sendKeys方法,生成getText方法。

这可能还是我在做梦?

提前致谢。

2 个答案:

答案 0 :(得分:0)

一个非常有趣的问题,类似的事情发生在我身上,而且是关于我为此做点什么的。

以下是使用模板的方法: 转到窗口>首选项>编辑器>模板

并创建此模板:

private By ${word_selection}Finder = By.cssSelector("");//TODO add selector

public String get${word_selection}(){
    try {
            return descriptionTabContainer.findElement(${word_selection}Finder).getText();
    } catch (NoSuchElementException e) {
            return null;
    }
}

现在,如果您编写一些变量,请将其标记并单击ctrl + space选择您的模板名称,即可获得您的代码。

答案 1 :(得分:0)

您可以为每个定位器自动生成代码:

https://github.com/naukri-engineering/SeleniumCodeGenerator

特点:

它有助于减少/消除构建页面对象和编写字段级验证的自动化代码时的重复编码工作。

•易于集成(只需要java和ant)

•可以在Windows,Linux和Mac OS上运行•生成清洁代码并消除拼写错误

reduce