|
昨天晚上闲着没事做,个人觉得Selenium对pop-up dialog的支持不太好,所以自己就打算通过第三方的脚本去实现这个东西,下面是其中的一个方案,It does work, and Hope it can help some 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 |
|