簡體   English   中英

在頁面對象模型設計中如何使用硒ExpectedConditions?

[英]How do you use selenium ExpectedConditions in a page object model design?

希望我不是第一個遇到此問題的人。

我正在用C#編寫一些硒測試,在嘗試對頁面對象模型設計進行驗證時遇到了一個難題,同時還需要對ExpectedConditions類進行一些明確的等待。

假設我將元素存儲在元素映射類中,該元素映射類只是使用存儲在資源文件中的XPath調用.FindElement方法的屬性...

public class PageObject {

    public IWebElement Element
    {
        get { return DriverContext.Driver.FindElement(By.XPath(Resources.Element)); }
    }
}

然后,我將繼續在各種硒方法中使用該屬性。

我遇到的問題是,我還需要檢查此元素在頁面上是否可見,並且在執行檢查之前它將出錯(例如,使用WebDriverWait,將ExpectedConditions.ElementIsVisible(by)傳遞給.until方法)。

如何干凈地將IWebElement和“按”定位符分開,並在需要的地方進行明確的等待/檢查?

TLDR-如何維護頁面對象模型設計,同時還能靈活地基於元素的“按”定位符使用顯式等待。

非常感謝,

我一直使用頁面對象,但是在類的頂部有定位符,而不是元素。 然后,根據需要使用定位器單擊按鈕等。 這樣做的好處是,我僅在需要時訪問頁面上的元素,以避免過時的元素異常等。請參見下面的簡單示例。

class SamplePage
{
    public IWebDriver Driver;
    private By waitForLocator = By.Id("sampleId");

    // please put the variable declarations in alphabetical order
    private By sampleElementLocator = By.Id("sampleId");

    public SamplePage(IWebDriver webDriver)
    {
        this.Driver = webDriver;

        // wait for page to finish loading
        new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(waitForLocator));

        // see if we're on the right page
        if (!Driver.Url.Contains("samplePage.jsp"))
        {
            throw new InvalidOperationException("This is not the Sample page. Current URL: " + Driver.Url);
        }
    }

    public void ClickSampleElement()
    {
        Driver.FindElement(sampleElementLocator).Click();
    }
}

我建議不要將定位符存儲在單獨的文件中,因為它會破壞頁面對象模型的口頭禪,這與頁面對象中的所有內容有關。 您無需打開任何文件,只需使用一個文件即可對Page X(頁面對象類)進行任何操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM