首页 文章

如何在Selenium WebDriver中对FirefoxDriver,ChromeDriver和IEdriver执行基本身份验证?

提问于
浏览
38

我正在使用Selenium-Firefox驱动程序和Selenium-Chrome驱动程序版本2.0a5( Web Driver API ),我正在尝试测试具有BASIC身份验证的Web应用程序(当我点击时会出现一个弹出窗口来验证用户身份无论什么页面,弹出窗口都不是HTML的一部分 .

现在,我需要一个策略来验证Firefox,Chrome和IE中的用户(我即将导入IE驱动程序) .

我正在阅读一些文章,我可以设置一个Firefox配置文件 . 例如:

FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.setPreference("network.http.phishy-userpass-length", 255);
WebDriver driver = new FirefoxDriver(ffProfile);
driver.get("http://username:password@hostname");

但它似乎对我不起作用 . 有没有人为这些浏览器提供有效的解决方案?

7 回答

  • 2

    我通过以下方式使用Firefox webdriver:

    profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "google.com");
    driver = new FirefoxDriver(profile);
    
    driver.Navigate().GoToUrl("http://user:pwd@google.com");
    
  • 3

    是的,目前不支持BASIC HTTP身份验证,但我现在正在为FF和Chrome工作 .

    我在问题中写的代码适用于那些驱动程序 . 我只是尝试使用FF3.6作为Firefox默认浏览器(安装在Firefox文件夹中)而不是FF4(尚不支持) . 对于IE,我可能会尝试通过Windows注册表禁用身份验证 .

    这个页面http://code.google.com/p/selenium/issues/detail?id=34可能会有所帮助 .

  • 2

    在您的代码上添加此新的Firefox配置文件

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile myprofile = profile.getProfile("myProjectProfile"); //replace "myProjectProfile" with your profile"
    WebDriver driver = new FirefoxDriver(myprofile);
    

    Firefox配置设置

    当您执行以下设置时,这可以正常工作而不会提示任何身份验证 .

    • 在您的FF网址上输入“ about:config

    • 现在在搜索字段中输入“ Proxy

    • 确保“ signon.autologin.proxy " is set " true " (By default it is " false”)


    加载默认/自定义Chrome配置文件以使用Selenium WebDriver运行测试


    • 下载 chromedriver.exe

    • 解压缩 chromedriver_win_26.0.1383.0.zip 文件夹,找到.exe文件到C:/文件夹

    在您的JAVA代码上添加此脚本

    DesiredCapabilities capability = DesiredCapabilities.chrome();
    System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
    capability.setCapability("chrome.switches", Arrays.asList("–disable-extensions"));
    capability.setCapability("chrome.binary", "C:/Users/user_name/AppData/Local/Google/Chrome/Application/chrome.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/Default");
    driver = new ChromeDriver(capability);
    

    注意:IE不需要配置文件设置来运行测试,因为它们在Server用户上运行,而Firefox和Chrome使用二进制文件 .

  • 10

    如果要在Internet Explorer中启用http auth,则必须编辑注册表并添加它(如果不存在则创建密钥):

    • in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE ,创建一个值为 0 的DWORD iexplore.exe

    • in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE ,创建一个值为 0 的DWORD iexplore.exe

    • 关闭并重新打开Internet Explorer

    如果你有一个x64 IE,路径有点不同:

    • HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
  • 1

    为了获得更多可移植性,可以使用存根API并使用Alert来处理 .

    示例Java代码(sample):

    import org.openqa.selenium.Alert;
    import org.openqa.selenium.security.Credentials;
    public void authenticateUsing(Credentials credentials) {
        private final Alert alert;
        alert.authenticateUsing(credentials);
    }
    

    另见:auth_tests.py

    或者手动发送密钥,如:

    SendKeys("user");
    SendKeys("{TAB}");
    SendKeys("password");
    SendKeys("~"); // Enter
    

    另请参阅以下功能请求:#453 Portable BASIC Auth at GitHub

    有关:

    在QA SE

  • 6

    有一个解决方案,通过在http://mogotest.com/blog/2010/06/23/how-to-perform-basic-auth-in-selenium手动设置HTTP标头,使用Selenium 1.x执行身份验证,但我无法访问标头 .

    根据信息here 'Basic Authentication support for Selenium 2'在Selenium 2 Beta 2中添加,但查看源代码我只能看到它实现为一种保护远程Selenium服务器免受匿名访问的方式 .

    所以我认为答案是目前不支持BASIC HTTP身份验证 .

  • 1

    我无法使用Selenium 2和Chrome进行基本身份验证(由于Chrome存在错误),因此我为Chrome创建了一个自动发送基本身份验证凭据的扩展程序(请参阅https://chrome.google.com/webstore/detail/basic-authentication-auto/dgpgkkfheijbcgjklcbnokoleebmeokn) .

相关问题