【问题标题】:How to perform Basic Authentication for FirefoxDriver, ChromeDriver and IEdriver in Selenium WebDriver?如何在 Selenium WebDriver 中对 FirefoxDriver、ChromeDriver 和 IEdriver 执行基本身份验证?
【发布时间】:2011-08-06 01:21:24
【问题描述】:

我正在使用 Selenium-Firefox-driverSelenium-Chrome-Driver 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");  

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

【问题讨论】:

标签: java selenium selenium-webdriver cross-browser basic-authentication


【解决方案1】:

我通过以下方式让它与 Firefox webdriver 一起工作:

profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "google.com");
driver = new FirefoxDriver(profile);

driver.Navigate().GoToUrl("http://user:pwd@google.com");

【讨论】:

  • Chrome 和 IE 怎么办?
【解决方案2】:

没错,目前不支持 BASIC HTTP 身份验证,但我现在可以在 FF 和 Chrome 上使用它。

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

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

【解决方案3】:

为了提高可移植性,这可以通过存根 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

相关:

【讨论】:

    【解决方案4】:

    在您的代码中添加这个新的 Firefox 配置文件

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

    Firefox 配置设置

    当您进行以下设置时,无需提示任何身份验证即可正常工作..

    • 在您的 FF 网址上输入“about:config
    • 现在在搜索字段中输入“代理
    • 确保“signon.autologin.proxy”设置为“true”(默认情况下 这是“假”)

    加载默认/自定义 Chrome 配置文件以使用 Selenium 运行测试 网络驱动程序


    1. 下载 chromedriver.exe
    2. 解压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 不需要配置文件设置来运行测试,因为它们在服务器用户上运行,而 Firefox 和 Chrome 使用二进制文件。

    【讨论】:

    • 我看不到您的代码在哪里指定了用户名和密码。
    • @Elmue 这个脚本绕过你的用户名和密码
    • 如果您在域中并且 Windows 发送您当前会话的凭据,它可能会起作用。但对我来说这是无用的,因为我的计算机不在域中,我必须手动将它们输入到浏览器中。在这种情况下,您的代码将无法工作。
    • 在你的情况下可能不需要
    【解决方案5】:

    如果你想在 Internet Explorer 中启用 http auth,你必须编辑注册表并添加这个(如果它们不存在则创建键):

    1. HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE中,创建一个DWORD iexplore.exe,其值为0

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

    3. 关闭并重新打开 Internet Explorer

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

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

    【讨论】:

    • 这改善了这种情况,但如果您运行测试的服务器使用 http 而不是 https,这似乎还不够。在 http 上,您会收到登录提示,但您仍然卡住了。
    • 你在哪里指定用户名和密码?
    【解决方案6】:

    有一种解决方案可以通过手动将 HTTP 标头设置为 http://mogotest.com/blog/2010/06/23/how-to-perform-basic-auth-in-selenium 来使用 Selenium 1.x 执行身份验证,但我认为这不能转移到 Selenium 2,因为您无权访问标头。

    根据here“Selenium 2 Beta 2 中添加了对 Selenium 2 的基本身份验证支持”的信息,但通过查看源代码,我只能看到它是作为一种保护远程 Selenium 服务器免受匿名访问的方式实现的。

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

    【讨论】:

      【解决方案7】:

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

      【讨论】:

      • 干得好!您介意分享扩展程序的源代码吗?
      • 它在扩展本身中可用。
      • 我发现了另一个 Chrome 插件,它做同样的事情:“MultiPass for HTTP basic authentication”效果很好。
      猜你喜欢
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2022-08-19
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      相关资源
      最近更新 更多