WATIN如何处理确认对话框

Quick and easy WatiN recipe: How to handle a confirm dialog
快速和容易WatiN食谱:如何处理确认对话框

Quick and easy WatiN recipe: How to handle a confirm dialog
快速和容易WatiN食谱:如何处理确认对话框
Introduction
导言
This is a bite-sized how-to guide, or recipe, that demonstrates how to use WatiN to aut

tomate a web page with a confirm dialog.
这是一个非常小的使用指南,或食谱,演示如何使用WatiN自动化的确认对话框中的网页。
We'll start by creating a simple HTML page and write two tests that will exercise the dialog.
我们将首先创建一个简单的HTML页面,写两个测试将行使对话框。
Background
背景
WatiN is a .NET web automation testing tool. More information can be found at
WatiN是的。NET Web自动化测试工具。更多信息可在
.

We'll be using the Microsoft Team Test in Visual Studio 2008 Team System, but any Unit Test framework will do.
我们将使用微软的Visual Studio 2008团队测试团队系统,但任何单元测试框架就可以了。
Creating the Page
创建页面
In order to test a confirm dialog, we'll need a web page. Our sample page is very simple and very testable:
为了测试一个确认对话框,我们需要一个网页。我们的范例网页很简单,很可测试:

<html>
<body>
<input id="myButton1" type="button" value="this is a button"
onclick="confirmMe(); return false;"><br>
<script>
function confirmMe() {
var answer = confirm ("Are you having fun?")
if (answer)
document.getElementById("myButton1").value="Clicked OK";
else
document.getElementById("myButton1").value="Clicked Cancel";
}
</script>
</body>
</html>


As you can see, we have a button that calls a function. The function pops up a confirm dialog that sets the button's caption depending on whether the user clicked OK or Cancel.
你可以看到,我们有一个按钮,调用一个函数。该函数弹出一个确认对话框,设置按钮的标题取决于用户是否单击确定或取消。
Now, we need to test the page!
现在,我们需要测试的页面!
Writing the Tests
写作测试
We are going to create two test methods, one for testing the OK button, and the other for testing the Cancel button. First, create your Unit Test project and set a reference to your WatiN binary, WatiN.Core.dll
我们将创建两个测试方法,测试确定按钮之一,是检验取消按钮等。首先,创建单元测试项目,并设置对您的WatiN二进制,WatiN.Core.dll参考
. Again, we're using the Microsoft Unit Test framework, so we'll create a new Test Project and add a Unit Test.
。同样,我们使用的是Microsoft单元测试框架,因此我们将创建一个新的测试项目,并添加一个单元测试。

using WatiN.Core;
using WatiN.Core.DialogHandlers;
[TestMethod()]
public void ConfirmOKTest()
{
using (IE ie = new IE(";))
{
ConfirmDialogHandler handler = new ConfirmDialogHandler();
using (new UseDialogOnce(ie.DialogWatcher, handler))
{
ie.Button("myButton1").ClickNoWait();
handler.WaitUntilExists();
handler.OKButton.Click();
}
ie.WaitForComplete();
Assert.AreEqual("Clicked OK", ie.Button("myButton1").Text);
}
}


If you'd rather see the test in VB:
如果您希望看到在VB测试:

<TestMethod()>Public Sub ConfirmOKTest()
Dim ie As New IE(";)
Dim handler As New ConfirmDialogHandler()
Using udo As New UseDialogOnce(ie.DialogWatcher, handler)
ie.Button("myButton1").ClickNoWait()
handler.WaitUntilExists()
)
handler.OKButton.Click()
End Using
ie.WaitForComplete()
Assert.AreEqual("Clicked OK", ie.Button("myButton1").Text)
End Sub
For our little recipe, the test is fairly simple. We make sure we are using the two WatiN classes we'll need: Core for the base functionality and IE automation, and DialogHandler to actually control the dialog.
对于我们的小配方,测试相当简单。我们要确保我们使用的是两个WatiN类我们需要:为基本功能和IE自动化的核心,并DialogHandler实际控制对话框。
The IE object is disposable, and we want to make sure it is released when we are done with it. We'll create a new IE instance named ie and set the starting URL to the page we created above.
IE的对象是一次性的,我们希望确保它被释放时,我们对此做。我们将创建一个新的IE实例命名的,即并设置起始URL的网页,我们上面创建。
Next, we need to add a handler. This is the object that actually manages the button clicks in the dialog box that we'll pop up. Once again, this object is disposable and it only needs to stay in scope while we're actually using it. Specifically, we are going to use a ConfirmDialogHandler and tell WatiN we're only going to handle it one time with a new UseDialogOnce, instantiated with our browser's built-in DialogWatcher and the ConfirmDialogHandler we just created.
接下来,我们需要添加一个处理程序。这是实际管理的对象对话框中的按钮点击,我们会弹出。再次,这个对象是一次性的,只是需要继续留在我们的范围,而真正使用它。具体来说,我们将使用一ConfirmDialogHandler,并告诉WatiN我们只去处理它与新UseDialogOnce有一段时间,与我们的浏览器的内置DialogWatcher和ConfirmDialogHandler我们刚刚创建的实例。
Our button has an ID of myButton1, so we'll reference that and click the button without waiting for the browser to respond to our click. Our dialog handler will take care of that for us.
我们的按钮有一个myButton1身份证,因此我们将引用,并点击浏览器不必等待回应我们的点击按钮。我们的对话将采取的处理,对我们的照顾。
ie.Button("myButton1").ClickNoWait(); 

Now, we just wait until the confirm dialog exists and tell the handler which button we want to click. For this test, we'll click OK.
现在,我们只是等待,直到确认对话框,告诉存在处理的按钮,我们要点击。这个测试中,我们将单击确定。
handler.WaitUntilExists();
handler.OKButton.Click();

Now the dialog has been handled, so we wait for IE to finish processing our clicks. We can check whether our action was successful, which would result in the caption of the button changing from "this is a button" to "Clicked OK".
现在对话框已处理的,所以我们对IE等待我们的点击完成处理。我们可以检查我们的行动是否成功,这将导致在变化,从“这是一个按钮”到“按钮标题点击数确定”。
ie.WaitForComplete();
Assert.AreEqual("Clicked OK", ie.Button("myButton1").Text);

Naturally, we'll want to check the behavior of the Cancel button, too. Our test is almost identical, but instead of the OK button and our OK button caption change, we'll check for the Cancel action.
当然,我们将要检查的取消按钮的行为,也。我们的测试几乎是相同的,但并没有确定按钮,我们确定按钮的标题的变化,我们将检查的取消行动。
[TestMethod()]
public void ConfirmCancelTest()
{
using (IE ie = new IE(";)){
ConfirmDialogHandler handler = new ConfirmDialogHandler();
using (new UseDialogOnce(ie.DialogWatcher, handler))
{
ie.Button("myButton1
1").ClickNoWait();
handler.WaitUntilExists();
handler.CancelButton.Click();
}
ie.WaitForComplete();
Assert.AreEqual("Clicked Cancel", ie.Button("myButton1").Text);
}
}
This test clicks Cancel and checks that the button caption has been changed to "Clicked Cancel".
此测试点击取消,而按钮标题已改为“单击取消检查”。
Easy as pie!
简单得很!
Points of Interest
兴趣点
This was a pretty simple solution that can handle surprisingly complicated scenarios. WatiN makes it extremely easy for my team to automate testing of our web applications.
这是一个非常简单的解决方案,能够处理复杂的情况令人吃惊。 WatiN,大大方便了我的团队自动化的Web应用程序测试。

你可能感兴趣的:(WATIN如何处理确认对话框)