简体   繁体   English

页面对象模型返回动作而不是对象?

[英]Page Object Model Returning Action Instead of Object?

Picking up a Java-Selenium framework, and noticed that the previous owner has a lot of page object models that are defined as java classes, but instead of returning a driver.findElement() , they are returning a driver.findElement().click(); 拾取一个Java-Selenium框架,并注意到先前的所有者拥有许多定义为java类的页面对象模型,但是它们没有返回driver.findElement() ,而是返回了driver.findElement().click();

EDIT: You guys are correct it's not returning per say 编辑:你们是正确的,每句话都不会返回

public void leftnav_home_link() { driver.findElement().click(); }

I've never seen this before, but is this valid/follows the ideals of a page object model? 我以前从未见过,但这是否有效/遵循页面对象模型的理想?

What kind of potential problems can this lead into? 这会导致什么样的潜在问题?

It doesn't make sense, since according to the source code , click() on a WebElement just executes the remote driver command and returns nothing: 这没有任何意义,因为根据源代码WebElement上的click()仅执行远程驱动程序命令,不返回任何内容:

public void click() {
    execute(DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", id));
}

In terms of page objects, if the 'action' you refer to is a action to the next page, the normal thing would be to have a PageObjectX class with a normal constructor and a click method returning the next page object. 就页面对象而言,如果您所指的“动作”是对下一页的动作,那么通常的事情是拥有一个带有普通构造函数的PageObjectX类和一个返回下一个页面对象的click方法。 The click method might be implemented like this, which is not unusual: click方法可以这样实现,这并不罕见:

public class PageObjectX extends LoadableComponent {
    private final WebDriver driver;
    @FindBy(id = "go") private WebElement goB;
    PageObjectX {
        PageFactory.initElements(driver, this);
    }
    @Override
    protected void load() {
      driver.get("http://code.google.com/p/selenium/issues/entry");
    }

    @Override
    protected void isLoaded() throws Error {
      String url = driver.getCurrentUrl();
      assertTrue("Not on the issue entry page: " + url, url.endsWith("/entry"));
    }

    public PageObjectY clickGoToY() {
            driver.findElement( goB );
            return new PageObjectY();
    }
}

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

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