[Selenium] 通过第三方脚本 处理windows Pop-Up Dialog的一种解决方案
昨天晚上闲着没事做,个人觉得Selenium对pop-up dialog的支持不太好,所以自己就打算通过第三方的脚本去实现这个东西,下面是其中的一个方案,It does work, and Hope it can helpsome guys,主要是基于Windows 底层的UIautomationClient,UIAutomationProvider &UIautomationTypes等,大概的Code 如下:using System;
using System.Collections.Generic;
using System.Linq;
using Internal.Mita.Foundation;
using Internal.Mita.Foundation.Controls;
/// <summary>
/// This class represents the save file dialog
/// </summary>
public class SaveDialogWrapper
{
public SaveDialogWrapper(UIObject dialogUIObject)
{
this.dialogUIObject = dialogUIObject;
FileNameEdit = new Edit(SilverLightElements.GetUIObject(
dialogUIObject.Descendants, Edit));
SaveBtn = SilverLightElements.GetUIObject(
dialogUIObject.Descendants, Save);
CancelBtn = SilverLightElements.GetUIObject(
dialogUIObject.Descendants, Cancel);
}
#region Methods
/// <summary>
/// User click the cancel button
/// </summary>
public void CancelFile()
{
CancelBtn.Click();
}
/// <summary>
/// User type in file name and then click the save file button
/// </summary>
/// <param name="filePath"></param>
public void SaveFile(string filePath)
{
FileNameEdit.SetValue(filePath);
SaveBtn.Click();
}
/// <summary>
/// User click the save file button, the file name is by default, the file will
/// be saved in disk C:
/// </summary>
/// <returns></returns>
public string SaveDefaultFile()
{
string filePath = @"C:\{0}";
string fileName = FileNameEdit.Value;
fileName = fileName.Replace(" ", "");
filePath = string.Format(CultureInfo.InvariantCulture, filePath, fileName);
string temp = filePath;
FileNameEdit.SendKeys(string.Format("^(a){0}", temp));
SaveBtn.Click();
return filePath;
}
/// <summary>
/// This method is for future use. It is not used now.
/// </summary>
/// <returns></returns>
private DownLoadCompletedDialog LaunchSaveCompleteDialog()
{
WindowOpenedWaiter waiter = null;
UICondition condition = UICondition.CreateFromClassName(
UIHelper.DownLoadClassName).AndWith(
UICondition.CreateFromName(UIHelper.DownloadComplete));
waiter = new WindowOpenedWaiter(condition);
bool isDialogOpened = false;
SaveBtn.Click();
for (int i = 0; i < ExportContants.DownLoadRetry && !isDialogOpened; i++)
{
waiter.Reset();
waiter = new WindowOpenedWaiter(condition);
isDialogOpened = waiter.TryWait();
}
DownLoadCompletedDialog download = new DownLoadCompletedDialog(waiter.Source);
return download;
}
#endregion
#endregion
#region Member Variables
private static readonly UICondition Edit =
UICondition.Create("@ClassName='Edit'");
private static readonly UICondition Save =
UICondition.Create("@Name='Save'");
private static readonly UICondition Cancel =
UICondition.Create("@Name='Cancel'");
private static readonly UICondition CloseStr =
UICondition.Create("@Name='Close'");
private Edit FileNameEdit = null;
private UIObject SaveBtn = null;
private UIObject CancelBtn = null;
private UIObject CloseBtn = null;
private UIObject dialogUIObject = null;
#endregion 你用了 Mita!
这个应该不被允许吧 即使能放出来 外面的也无法用到。 请问你还有其他解决方案吗?不是基于mita的。 你可以去调用Windows底层的API,直接对对鼠标定位,点击和键盘输入等操作~:) 你好,请问你的msn或者qq多少? 直接用AutoIt3,方便实用的windows本地自动工具,脚本很简单,不难。java里直接调用脚本就解决问题了,啥问题都没。
要是直接用win api调用,麻烦,你要自己写个程序,到时由java调用之,但是你要用VC还是VB还是其他调用呢,相当于你开发一个小工具程序了。有时间和兴趣的到可以试试,因为这样时间才充满乐趣,哈哈~~
VB调用要api要简单些,VC调用更快,而且可以写一个无窗口程序,很适合这里的要求。 原帖由 欺负人 于 2010-1-22 13:43 发表 http://bbs.51testing.com/images/common/back.gif
你可以去调用Windows底层的API,直接对对鼠标定位,点击和键盘输入等操作~:)
如果只是操作鼠标和键盘的话,java里有Robot类你可以用试试,api解释我好像是记得它是直接通过消息放到系统的消息队列里的。
我想.Net里也应该有对应的类,让你方便操作本地鼠标和键盘动作,应该和java的这个类似,这就是竞争的好处啊~~
[ 本帖最后由 park_p 于 2010-1-22 15:58 编辑 ] 通过实践证明,selenium RC 还是能够处理pop window的:victory: 前些时候 全面地总结了一下 Selenium处理弹出窗口
Web内的自动化 还是比 Web外的自动化 简单啊。
页:
[1]