51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 7485|回复: 8
打印 上一主题 下一主题

[Selenium] 通过第三方脚本 处理windows Pop-Up Dialog的一种解决方案

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2009-12-15 12:42:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
昨天晚上闲着没事做,个人觉得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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2009-12-15 14:02:03 | 只看该作者
你用了 Mita!

这个应该不被允许吧 即使能放出来 外面的也无法用到。
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2010-1-21 17:18:17 | 只看该作者
请问你还有其他解决方案吗?不是基于mita的。
回复 支持 反对

使用道具 举报

该用户从未签到

4#
 楼主| 发表于 2010-1-22 13:43:08 | 只看该作者
你可以去调用Windows底层的API,直接对对鼠标定位,点击和键盘输入等操作~
回复 支持 反对

使用道具 举报

该用户从未签到

5#
发表于 2010-1-22 15:36:13 | 只看该作者
你好,请问你的msn或者qq多少?
回复 支持 反对

使用道具 举报

该用户从未签到

6#
发表于 2010-1-22 15:51:11 | 只看该作者
直接用AutoIt3,方便实用的windows本地自动工具,脚本很简单,不难。java里直接调用脚本就解决问题了,啥问题都没。
要是直接用win api调用,麻烦,你要自己写个程序,到时由java调用之,但是你要用VC还是VB还是其他调用呢,相当于你开发一个小工具程序了。有时间和兴趣的到可以试试,因为这样时间才充满乐趣,哈哈~~
VB调用要api要简单些,VC调用更快,而且可以写一个无窗口程序,很适合这里的要求。
回复 支持 反对

使用道具 举报

该用户从未签到

7#
发表于 2010-1-22 15:52:54 | 只看该作者
原帖由 欺负人 于 2010-1-22 13:43 发表
你可以去调用Windows底层的API,直接对对鼠标定位,点击和键盘输入等操作~

如果只是操作鼠标和键盘的话,java里有Robot类你可以用试试,api解释我好像是记得它是直接通过消息放到系统的消息队列里的。
我想.Net里也应该有对应的类,让你方便操作本地鼠标和键盘动作,应该和java的这个类似,这就是竞争的好处啊~~

[ 本帖最后由 park_p 于 2010-1-22 15:58 编辑 ]
回复 支持 反对

使用道具 举报

该用户从未签到

8#
发表于 2010-1-28 15:12:46 | 只看该作者
通过实践证明,selenium RC 还是能够处理pop window的
回复 支持 反对

使用道具 举报

该用户从未签到

9#
发表于 2010-2-1 11:47:55 | 只看该作者
前些时候 全面地总结了一下 Selenium处理弹出窗口

Web内的自动化 还是比 Web外的自动化 简单啊。
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /2 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-6-21 14:04 , Processed in 0.085919 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表