首先,我是编码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);
}
最佳答案
waitforcontrol
kinda方法在控件类型上调用。并且控件存在是正确的,但这并不意味着控件已准备就绪(可能正在加载)
一般做法是在超级父级上调用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);
希望有帮助!