一般通过Web UI进行基本认证,浏览器会弹出一个小窗口让用户输入用户名和密码。IE和其他浏览器不一样,它凭着和Windows操作系统的亲戚关系,用的是Windows的系统对话框而不是IE浏览器对话框。
这个时候Selenium就无能为力了。但是Robot Framework有在关键字组成的步骤之间随意切换关键字底层库的巨大优点,可以马上调用AutoIt库的关键字进行系统对话框的处理,例如下面的自定义关键字:
Input Credential If IE Popup Login Box
[Arguments] ${username} ${password}
${passed}= Run Keyword And Return Status Wait For Active Window Windows Security TimeOut=4
Return From Keyword If '${passed}' != 'True'
Control Set Text \ \ Edit1 ${username}
Control Set Text \ \ Edit2 ${password}
Control Click \ \ Button2
此关键字有超时机制,可以捕捉超时期内的windows登录弹出框,捕捉不到也会非失败返回(除非下面三行Control代码失败),不会中断测试。
注:中文环境下,‘Windows Security’应改为‘Windows安全’