WebDriver PageObjects & Large Amounts of Locators

丶灬走出姿态 提交于 2019-12-06 09:33:40

This is primarily opinion based but interesting question.

My suggestion would be modularize your page.

For example, you have a page with 100 locators, but surely they can't be 100 similar textboxes to let users to fill out. (If so, probably the solution here is to re-design your UI first)

If this 100 elements belong to different components, say 20 elements in top toolbar, 20 elements in list view, 20 elements in a form and 20 in bottom toolbar. Then you might want to create different classes for each of the components.

For example, here is an ordinary page object.

public class FooPage {

    private IWebDriver driver;

    public FooPage(IWebDriver driver) {
         this.driver = driver;
    }

    public IWebElement Element1 {
         get { return driver.FindElement(By.CssSelector(".something1")); }
    }

    // ...
    public IWebElement Element100 {
         get { return driver.FindElement(By.CssSelector(".something100")); }
    }

    public void AddItem(){
    }

    public void ClearList(){
    }
}

You might separate those elements into different components, for example, FooPageTopToolbar,FooPageList, etc.

public class FooPageTopToolbar {

    private IWebDriver driver;

    public FooPageTopToolbar(IWebDriver driver) {
         this.driver = driver;
    }

    // ...
    public IWebElement ToolbarElement10 {
         get { return driver.FindElement(By.CssSelector(".something100")); }
    }

    public void AddItem(){
    }
}

public class FooPageList {

    private IWebDriver driver;

    public FooPageList(IWebDriver driver) {
         this.driver = driver;
    }

    // ...
    public IWebElement ListElement10 {
         get { return driver.FindElement(By.CssSelector(".something100")); }
    }

    public void ClearList(){
    }
}

Now your FooPage will look like

public class FooPage {

    private IWebDriver driver;

    public FooPage(IWebDriver driver) {
         this.driver = driver;
    }

    public FooPageTopToolbar {
         get { return new FooPageTopToolbar(driver); }
    }

    // other components as well
    public FooPageList {
         get { return new FooPageList(driver); }
    }
}

Note this is just a demo, you might also consider get inheritance, interfaces and abstraction involved to build your page objects. For example, if TopToolbar is shared by many other pages, then you only need one class for all. If different pages have similar but slightly different toolbars, then create an abstract toolbar class of some kind.

Also, you mentioned you have different locators for different environment, the best solution in my opinion would be to add element identifiers into your source code (by "identifiers", I mean any identifiable parts, maybe HTML id, but most commonly unique class for testing purpose). In this case, you will only need one set of testing locator that are not frequently changed by UI developers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!