对源代码进行了简短的扫描,不确定最佳实践是什么。
例如,假设我有一个页面对象“DummyPage”,它有两个面板元素TopPanel
和BottomPanel
. 每个面板都有一些元素,这些元素由TopPanel.FindElement()
而不是找到driver.FindElement()
。如何为此申请页面工厂?
我知道这PageFactory.InitElements(ISearchContext, Object)
需要ISearchContext
,但是,我不确定如何将它用于一个类中的页面和面板元素。
public class DummyPage {
private IWebDriver driver;
public DummyPage(IWebDriver driver) {
this.driver = driver;
}
public IList<IWebElement> DummyLinks {
get { return driver.FindElements(By.CssSelector(".some-dummy-links")); }
}
public IWebElement TopPanel {
get { return driver.FindElement(By.Id("top-panel")); }
}
public IWebElement BottomPanel {
get { return driver.FindElement(By.Id("bottom-panel")); }
}
public IWebElement FooInTopPanel {
get { return TopPanel.FindElement(By.CssSelector(".something")); }
}
public IWebElement FooInBottomPanel {
get { return BottomPanel.FindElement(By.CssSelector(".something")); }
}
}
public class DummyPageWithPageFactory {
public DummyPageWithPageFactory(IWebDriver driver) {
PageFactory.InitElements(driver, this);
}
[FindsBy(How = How.CssSelector, Using = ".some-dummy-links")]
public IList<IWebElement> DummyLinks { get; private set; }
[FindsBy(How = How.Id, Using = "top-panel")]
public IWebElement TopPanel { get; private set; }
[FindsBy(How = How.Id, Using = "bottom-panel")]
public IWebElement BottomPanel { get; private set; }
//public IWebElement FooInTopPanel { get; private set; }
//public IWebElement FooInBottomPanel { get; private set; }
}
如果我使用driver.FindElement()
所有实例并连接所有定位器,我可能会面临另一种情况,即所有定位器都太长并且我不能在 C# 属性中使用变量。
[FindsBy(How = How.CssSelector, Using = "#top-panel .blah .blah .super-long-blah .something")]
[FindsBy(How = How.CssSelector, Using = "#top-panel .blah .blah .super-long-blah .something-new")]
[FindsBy(How = How.CssSelector, Using = "#bottom-panel .blah .blah .super-long-blah .something")]