Selenium,等待元素,页面对象模型,c#绑定



我们如何等待IWebElement被(重新)附加到DOM?我的场景是这样的,我从dropdown1中选择一个值,单击后,在dropdown2上发生数据绑定。所以当我的测试像从Dd1中选择"foo",然后从Dd2中选择"bar"->我将得到一个异常,有一个竞争条件,因为Dd2还没有渲染。现在,我知道,我们有WebDriverWait类,我们可以像这样使用Until方法:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(By.Id("foo"));

但是我真的不想把定位符字符串("foo")带到我的测试逻辑中,因为它似乎违背了使用页面对象模型的目的。当使用Page对象模型时,我已经有了IWebElement实例

[FindsBy(How = How.Id, Using = "ctl00_MainContentPlaceHolder_actionButtonBarControl_btnSave")]
    public IWebElement BtnSave { get; set; }

那么,你知道有什么方法可以隐式地等待IWebElement准备好进行通信吗?

嗨,我知道这有点晚了,但我有同样的问题,我解决了它通过做以下(使用您的代码):

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(By.Id(_pageModel.BtnSave.GetAttribute("id"));

然后它只是返回ID属性的值,并停止您必须用元素查找污染测试代码。希望能有所帮助。

如果你不想使用locator values意味着你可以去隐式等待而不是使用显式等待。隐式等待也使driver实例等待给定的一段时间。

Actual difference to Explicit Wait is that it will tell Web driver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.The default setting is 0.

代码:

  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

要记住的一件事是,一旦设置了隐式等待,它将在WebDriver对象实例的生命周期内保持不变。

更多信息请访问此链接http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#implicitlyWait(long,java.util.concurrent.TimeUnit)

我们所做的是传递一个像seleniumRC css=a, xpath=b一样的原始选择器文本。

然后我们有一个findElement方法来解析请求,获得适当的By并搜索元素

最新更新