selenium学习系列--windows窗口处理扩展
selenium对windows的扩展处理不好,需要通过外部工具进行扩展
下面是通过使用autoit3编写的一个处理外部处理成功,实现如下:
1、先建一个dialog.ini文件,配置信息保存在该ini文件中,如下:
dialog.ini
[Choosefile]
title=选择文件|Choose file
[Alter]
title=Microsoft Internet Explorer|Windows Internet Explorer
[Prompt]
title=Explorer 用户提示|Explorer User Prompt
[Security]
title=安全警告|Security Alert
[DownloadFile]
title=文件下载 - 安全警告|文件下载|File Download
[SaveasFile]
title=另存为
title=选择文件|Choose file
[Alter]
title=Microsoft Internet Explorer|Windows Internet Explorer
[Prompt]
title=Explorer 用户提示|Explorer User Prompt
[Security]
title=安全警告|Security Alert
[DownloadFile]
title=文件下载 - 安全警告|文件下载|File Download
[SaveasFile]
title=另存为
2、从dialog.ini文件中读取配置信息,获取当前系统所有打开的窗口,逐个进行匹配处理,处理成功后自动退出
处理代码如下:
dealDialog.au3
1 #include <Array.au3>
2 #include <Date.au3>
3
4 Global $FilePath = @ScriptDir & "\dialog.ini"
5
6 DealDialog()
7
8 Func DealDialog()
9 Local $dialog_type,$win_list_array,$title_list,$list,$title_array
10
11 $dialog_type = GetDialogType()
12
13 $win_list_array = GetWinList()
14
15 ;_ArrayDisplay($win_list_array, "$win_list_array")
16
17 For $j = 1 to UBound($dialog_type)-1
18
19 For $i = 1 to UBound($win_list_array)-1
20 $list = IniRead($FilePath,$dialog_type[$j], "title","")
21
22 If ($list <> '') Then
23 $title_array = StringSplit($list , '|', 1)
24
25 If (_ArrayFindAll($title_array,$win_list_array[$i]) <> -1) Then
26 WinActivate($win_list_array[$i],'')
27
28 Switch $dialog_type[$j]
29 Case 'Alter'
30 ;处理Alter和Confirm对话框
31 $button_index = _ArrayFindAll(StringSplit(WinGetClassList($win_list_array[$i]),Chr(10),1),'Button')
32
33 $content = ControlGetText($win_list_array[$i],'','Static2')
34 SetClipBoard($content)
35
36 Switch UBound($button_index)
37 Case 1
38 Send('{ENTER}')
39 Case 2
40 ControlClick($win_list_array[$i],'','Button1')
41 EndSwitch
42
43 Exit
44 Case 'Choosefile'
45 ControlSetText($win_list_array[$i],'','Edit1',ClipGet())
46 ControlClick($win_list_array[$i],'','Button2')
47 Exit
48 Case 'Prompt'
49 ControlSetText($win_list_array[$i],'','Edit1',ClipGet())
50 ControlClick($win_list_array[$i],'','Button1')
51 Exit
52 Case 'Security'
53 $content = ControlGetText($win_list_array[$i],'','Static2')
54 SetClipBoard($content)
55 ControlClick($win_list_array[$i],'','Button1')
56 Exit
57 Case 'DownloadFile'
58 $save_win_title = StringSplit(IniRead($FilePath,'SaveasFile', "title","") , '|', 1)
59
60 For $k = 1 to UBound($save_win_title)-1
61 If(WinExists($save_win_title[$k]) <> 1) Then
62 ;Send('!S')
63 ControlClick($win_list_array[$i],'','Button2')
64 EndIf
65 Next
66
67 Exit
68 Case 'SaveasFile'
69 ControlSetText($win_list_array[$i],'','Edit1',ClipGet())
70 ControlClick($win_list_array[$i],'','Button2')
71 Exit
72 EndSwitch
73
74 EndIf
75 EndIf
76 Next
77 Next
78 EndFunc
79
80 Func SetClipBoard($content)
81 ClipPut($content)
82 EndFunc
83
84 Func GetDialogType()
85 $name = IniReadSectionNames($FilePath)
86
87 If @error Then
88 Exit;
89 Else
90 Return $name
91 EndIf
92 EndFunc
93
94 Func GetWinList()
95 Dim $array[1]
96
97 $win_list = WinList()
98
99 For $i = 1 to $win_list[0][0]
100 ; 只显示带有标题的可见窗口
101 If $win_list[$i][0] <> "" AND IsVisible($win_list[$i][1]) Then
102 _ArrayAdd($array,$win_list[$i][0])
103 EndIf
104 Next
105
106 Return $array
107 EndFunc
108
109 Func IsVisible($handle)
110 If BitAnd( WinGetState($handle),2 ) Then
111 Return 1
112 Else
113 Return 0
114 EndIf
115
116 EndFunc
117
2 #include <Date.au3>
3
4 Global $FilePath = @ScriptDir & "\dialog.ini"
5
6 DealDialog()
7
8 Func DealDialog()
9 Local $dialog_type,$win_list_array,$title_list,$list,$title_array
10
11 $dialog_type = GetDialogType()
12
13 $win_list_array = GetWinList()
14
15 ;_ArrayDisplay($win_list_array, "$win_list_array")
16
17 For $j = 1 to UBound($dialog_type)-1
18
19 For $i = 1 to UBound($win_list_array)-1
20 $list = IniRead($FilePath,$dialog_type[$j], "title","")
21
22 If ($list <> '') Then
23 $title_array = StringSplit($list , '|', 1)
24
25 If (_ArrayFindAll($title_array,$win_list_array[$i]) <> -1) Then
26 WinActivate($win_list_array[$i],'')
27
28 Switch $dialog_type[$j]
29 Case 'Alter'
30 ;处理Alter和Confirm对话框
31 $button_index = _ArrayFindAll(StringSplit(WinGetClassList($win_list_array[$i]),Chr(10),1),'Button')
32
33 $content = ControlGetText($win_list_array[$i],'','Static2')
34 SetClipBoard($content)
35
36 Switch UBound($button_index)
37 Case 1
38 Send('{ENTER}')
39 Case 2
40 ControlClick($win_list_array[$i],'','Button1')
41 EndSwitch
42
43 Exit
44 Case 'Choosefile'
45 ControlSetText($win_list_array[$i],'','Edit1',ClipGet())
46 ControlClick($win_list_array[$i],'','Button2')
47 Exit
48 Case 'Prompt'
49 ControlSetText($win_list_array[$i],'','Edit1',ClipGet())
50 ControlClick($win_list_array[$i],'','Button1')
51 Exit
52 Case 'Security'
53 $content = ControlGetText($win_list_array[$i],'','Static2')
54 SetClipBoard($content)
55 ControlClick($win_list_array[$i],'','Button1')
56 Exit
57 Case 'DownloadFile'
58 $save_win_title = StringSplit(IniRead($FilePath,'SaveasFile', "title","") , '|', 1)
59
60 For $k = 1 to UBound($save_win_title)-1
61 If(WinExists($save_win_title[$k]) <> 1) Then
62 ;Send('!S')
63 ControlClick($win_list_array[$i],'','Button2')
64 EndIf
65 Next
66
67 Exit
68 Case 'SaveasFile'
69 ControlSetText($win_list_array[$i],'','Edit1',ClipGet())
70 ControlClick($win_list_array[$i],'','Button2')
71 Exit
72 EndSwitch
73
74 EndIf
75 EndIf
76 Next
77 Next
78 EndFunc
79
80 Func SetClipBoard($content)
81 ClipPut($content)
82 EndFunc
83
84 Func GetDialogType()
85 $name = IniReadSectionNames($FilePath)
86
87 If @error Then
88 Exit;
89 Else
90 Return $name
91 EndIf
92 EndFunc
93
94 Func GetWinList()
95 Dim $array[1]
96
97 $win_list = WinList()
98
99 For $i = 1 to $win_list[0][0]
100 ; 只显示带有标题的可见窗口
101 If $win_list[$i][0] <> "" AND IsVisible($win_list[$i][1]) Then
102 _ArrayAdd($array,$win_list[$i][0])
103 EndIf
104 Next
105
106 Return $array
107 EndFunc
108
109 Func IsVisible($handle)
110 If BitAnd( WinGetState($handle),2 ) Then
111 Return 1
112 Else
113 Return 0
114 EndIf
115
116 EndFunc
117
3、实例:
运行下面的html文件,操作弹出对话框窗口,运行上面的autoit3代码
实例
<html>
<head>
<title>处理对话框</title>
<script language= "javascript" type="text/javascript">
function clickbutton(flag)
{
if (flag == 1) alert("测试alter对话框");
if (flag == 2) prompt("测试prompt对话框");
if (flag == 3) confirm('测试confirm对话框', '测试confirm对话框?',"测试结果:");
}
</script>
</head>
<body>
<center>
<H1>watir处理对话框<H1>
<hr>
<table border ="2">
<tr>
<th bgcolor = "#aaaaaa">测试内容</th>
<th bgcolor = "#aaaaaa">操作</th>
</tr>
<tr>
<td>文件上传</td>
<td><input type="file" name="file_field" /></td>
</tr>
<tr>
<td>alter对话框</td>
<td>
<input type="button" name="alterbutton" value="测试alter对话框" onclick = "clickbutton(1);" />
</td>
</tr>
<tr>
<td>prompt对话框</td>
<td>
<input type="button" name="promptbutton" value="测试prompt对话框" onclick = "clickbutton(2);" />
</td>
</tr>
<tr>
<td>confirm对话框 </td>
<td>
<input type="button" name="confirmbutton" value="测试confirm对话框" onclick = "clickbutton(3);" />
</td>
</tr>
<a class="jqTransformCheckbox jqTransformChecked" href=" http://staging.healthcompare.com/Health-Insurance-Quote/Individual-Quote.aspx#" jQuery1256697289107="13"/>
</table>
</center>
</body>
</html>
<head>
<title>处理对话框</title>
<script language= "javascript" type="text/javascript">
function clickbutton(flag)
{
if (flag == 1) alert("测试alter对话框");
if (flag == 2) prompt("测试prompt对话框");
if (flag == 3) confirm('测试confirm对话框', '测试confirm对话框?',"测试结果:");
}
</script>
</head>
<body>
<center>
<H1>watir处理对话框<H1>
<hr>
<table border ="2">
<tr>
<th bgcolor = "#aaaaaa">测试内容</th>
<th bgcolor = "#aaaaaa">操作</th>
</tr>
<tr>
<td>文件上传</td>
<td><input type="file" name="file_field" /></td>
</tr>
<tr>
<td>alter对话框</td>
<td>
<input type="button" name="alterbutton" value="测试alter对话框" onclick = "clickbutton(1);" />
</td>
</tr>
<tr>
<td>prompt对话框</td>
<td>
<input type="button" name="promptbutton" value="测试prompt对话框" onclick = "clickbutton(2);" />
</td>
</tr>
<tr>
<td>confirm对话框 </td>
<td>
<input type="button" name="confirmbutton" value="测试confirm对话框" onclick = "clickbutton(3);" />
</td>
</tr>
<a class="jqTransformCheckbox jqTransformChecked" href=" http://staging.healthcompare.com/Health-Insurance-Quote/Individual-Quote.aspx#" jQuery1256697289107="13"/>
</table>
</center>
</body>
</html>