使用Selenium和AutoIt加载网页时处理Windows安全性弹出窗口

时间:2011-11-03 14:13:54

标签: selenium authentication popup selenium-rc

我有一个基于Web的应用程序,只要第一个网页开始加载就会显示Windows安全弹出窗口,并且只有在我们提供登录凭据后页面才会完成加载。

我编写了以下脚本,但它不会在selenium.open("/faces/Main");之后转到下一个代码行,最后因默认超时错误消息而失败。

结果控制台:

Timeout 30000ms

Selenium1代码:

package first;

import java.io.IOException;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;



public class Auth_assign {

private Selenium selenium;

   @BeforeClass
   public void setup() 
   {             
          selenium = new DefaultSelenium("localhost",4444, "*firefox C:/Program Files/FF 7.1/firefox.exe", "http://qa02.mydomain.com");
          selenium.start();
          selenium.windowMaximize();

   }

   @AfterClass(alwaysRun=true) 
   public void stopSelenium() {  
   //this.selenium.stop(); 
   } 




   @Test (description="Authentication pop-up")     
          public void Auth_popup() throws Exception
          {


          System.out.print("Unity page\n ");
          try
          {
          selenium.open("/faces/Main");
          Thread.sleep(10000);

          System.out.print("Window \n ");


          String[] dialog;

          dialog = new String[] { "Login_Dialog.exe","Authentication Required","test46#", "test123"};
          System.out.print( dialog);
          Runtime.getRuntime().exec(dialog);


          selenium.select("//select[@id='qryId1::criteriaItems::content']", "Customer Name");
          selenium.type("//input[@id='qryId1:criterionValue::content']", "Search Company");
          selenium.click("//a[@id='qryId1::search_icon']");
          selenium.waitForPageToLoad("3000");

          selenium.click("//a[@id='t1:0:gl1']");


          } catch (SeleniumException ex) {
        System.err.println(ex.getMessage());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }


   }      

Login.exe code

;~ ------------------------------------------------------------
;~ Login_dialog.au3
;~ To handle the Authentication Dialogbox
;~ Create By: Gaurang Shah
;~ Usage: Login_dialog.au3 "Dialog Title" "User Name" "Password"
;~ ------------------------------------------------------------
AutoItSetOption("WinTitleMatchMode","2")

if $CmdLine[0] < 3 then
msgbox(0,"Error","Supply all the Arguments, Dialog title User Name, Password")
Exit
EndIf

WinWait($CmdLine[1]) ; match the window with substring
$title = WinGetTitle($CmdLine[1]) ; retrives whole window title
ControlSend($title,"","Edit2",$CmdLine[2]);Sets User Name
ControlSend($title,"","Edit3",$CmdLine[3]);Sets Password
ControlClick($title,"","OK");

3 个答案:

答案 0 :(得分:0)

您可以在授权标头中传递用户名和密码,这可能有所帮助:How can I send HTTP Basic Authentication headers in Android?

答案 1 :(得分:0)

您可以在基本网址中提供凭据。例如,如果您的基本网址是 http://www.mysite.com/然后你可以提供它  http://user:pwd@www.mysite.com/

答案 2 :(得分:0)

尝试在安全弹出窗口出现之前调用自动脚本。否则你永远无法控制自己。我的意思是在你的主页selenium.open(“/ faces / Main”);

之前