我正在尝试使用java中的selenium webdriver自动化我的测试用例。它涉及基本身份验证对话框。为了使IE,Chrome,FF兼容使用相同的方法,我必须使用AutoIT我已经完成了IE和FF,但对于Chrome它无法正常工作。我正在使用AutoIT Window Info
工具查找类和控件名称。但Chrome在这种情况下有什么不同可以帮助吗?
以下是工作IE和FF的代码
$classForBasicAuthenticationWindow = "[CLASS:#32770]"
$username = "XXXXXX"
$password = "XXXXXX"
WinWait($classForBasicAuthenticationWindow,"",120)
If WinExists($classForBasicAuthenticationWindow) Then
WinActivate($classForBasicAuthenticationWindow)
Send($username)
Send("{TAB}")
Send($password & "{Enter}")
EndIf
类似于FF,以上是针对IE
对于Chrome,我已经写了这个,如果您考虑使用window info
工具,您会理解弹出窗口不是Chrome的情况。所以它变得有点复杂。无论如何,这是我尝试过的:
$classForBasicAuthenticationWindow = "[CLASS:Chrome_WidgetWin_1]"
$username = "XXXXX"
$password = "XXXXX"
WinWait($classForBasicAuthenticationWindow,"",120)
If WinExists($classForBasicAuthenticationWindow) Then
WinActivate($classForBasicAuthenticationWindow)
While 1
$isAuthenticationRequiredVisible = ControlGetHandle($classForBasicAuthenticationWindow, "", "[CLASS:ViewsTextfieldEdit; INSTANCE:2]")
If $isAuthenticationRequiredVisible <> "" Then
MsgBox($isAuthenticationRequiredVisible)
ExitLoop
EndIf
WEnd
ControlSend($classForBasicAuthenticationWindow, "", "[CLASS:ViewsTextfieldEdit; INSTANCE:2]", $username)
EndIf
答案 0 :(得分:2)
为什么不这样打开您的网址(通过在网址中传递登录凭据)
@driver.get "#{username}:#{password}@#{URL}"
一个例子
@driver.get "user:pass@my.domain.com"
答案 1 :(得分:1)
ControlSend方法采用窗口标题而不是窗口的类。 因此,尝试给出自动窗口信息工具中显示的窗口标题。
下面给出的代码对我有用。
$titleForBasicAuthenticationWindow = "Window Title As Given in Window Info Tool"
$username = "XXXXX"
$password = "XXXXX"
WinWait($classForBasicAuthenticationWindow,"",120)
If WinExists($classForBasicAuthenticationWindow) Then
WinActivate($classForBasicAuthenticationWindow)
ControlSend($classForBasicAuthenticationWindow, "", "[CLASS:ViewsTextfieldEdit; INSTANCE:2]", $username)
EndIf
答案 2 :(得分:0)
对于chrome,我尝试了所有操作,在基本的HTTP身份验证弹出窗口中输入凭据的最佳方法是通过AutoIT。
您只需要添加
sleep(25000)
在脚本的开头。
确保chrome能够在25秒内打开该弹出窗口。如果没有,那么增加睡眠时间。