如何让waitForWebPageToLoad在编码的ui测试中工作?

时间:2017-01-23 20:47:27

标签: c# coded-ui-tests

首先,我是编码ui测试的初学者,我的代码技能很差,但我正在努力学习。

现在我正在为visual studio编写一些测试用例(c#)(记录选项对我来说还不够),但我无法让waitForWebPageToLoad工作。

因此,例如下面,我点击一个链接,输入一些文字,然后点击一个按钮。之后,我希望代码在继续之前等待网页加载。我现在所做的是Thread.Sleep,但这不是一个好的解决方案......

ClickLink(Repo.Link(Browser));
EnterText(Repo.Field(Browser), "12345789");
ClickButton(Repo.LeftButton(Browser));
Thread.Sleep(5000);  //<-------- This must be replaced... :)

如何让waitForWebPageToLoad功能起作用? 我有这些方法,但我无法理解如何使它们工作,有谁想帮助我理解?

void ClickButton(HtmlInputButton obj) {
    waitForWebPageToLoad(obj, 10);
    TestContext.WriteLine("Clicking button: " + obj.Name);
    Mouse.Click(obj);
}

void waitForWebPageToLoad(UITestControl parent, int waitTime) {
    waitTime = int.Parse(waitTime.ToString() + "000"); //waitTimeExtension.ToString());
    Playback.PlaybackSettings.SearchTimeout = waitTime;
    parent.WaitForControlExist(waitTime);
    parent.WaitForControlReady(waitTime);
}

1 个答案:

答案 0 :(得分:0)

在控件类型上调用waitforcontrol种方法。并且控制存在是正确的,但它并不意味着控制准备就绪(可能正在加载)

通常的做法是在超级父级上调用waitforcontrolready方法。 (说浏览器)。我已经修改了您的方法,如下所示,

 public void WaitForPageToLoad(BrowserWindow browser, int WaitTimeOut)
    {
        // Waiting on all threads enable to wait for any background process to complete
        Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
        // Wait 
        browser.WaitForControlReady(WaitTimeOut * 1000);
        // Revert the playback settings (As waiting on all threads may sometime have a toll on execution speed 
        Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.UIThreadOnly;
    }

然后只需调用方法等待,无论你在哪里进行页面加载。请记住以秒为单位传递浏览器和超时作为参数。像,

    ClickButton(Repo.LeftButton(Browser));
    WaitForPageLoad(Browser, 120);

希望有所帮助!