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

发布于 2024-11-02 00:17:06 字数 600 浏览 1 评论 0 原文

我正在使用 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");  

但它似乎对我不起作用。有人有适用于这些浏览器的可行解决方案吗?

I am using the Selenium-Firefox-driver and Selenium-Chrome-Driver version 2.0a5 (Web Driver API), and I am trying to test a web app that has BASIC authentication (there is a popup that come up to authenticate the user when I hit whatever page, the popup is not part of the HTML).

Now, I need to a strategy to authenticate the user in Firefox, Chrome and IE (I'm going to import the IE Driver soon).

I was reading in few articles that I can set a Firefox profile for instance..something like:

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

but it doesn't seem to work for me. Does anyone have a working solution for those browsers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

路还长,别太狂 2024-11-09 00:17:06

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

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

driver.Navigate().GoToUrl("http://user:[email protected]");

I got it to work with Firefox webdriver by the following:

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

driver.Navigate().GoToUrl("http://user:[email protected]");
汐鸠 2024-11-09 00:17:06

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

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

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

True, BASIC HTTP authentication is not currently supported but I got it working now for FF and for Chrome.

The code I wrote in the questions works for those drivers. I just tried using FF3.6 as Firefox default browser (installed in Firefox folder) instead of FF4 (not supported yet). For IE, i may try to disable the authentication through Windows Registry.

This page http://code.google.com/p/selenium/issues/detail?id=34 may help.

苦笑流年记忆 2024-11-09 00:17:06

为了提高可移植性,这可以通过存根 API 并使用 警报

Java 代码示例 (示例):

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 GitHub 上的可移植基本身份验证

相关:

For more portability, this can be handled by stub API and using Alert.

Example Java code (sample):

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

See also: auth_tests.py

Or by sending keys manually like:

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

See also the following feature request: #453 Portable BASIC Auth at GitHub

Related:

独自唱情﹋歌 2024-11-09 00:17:06

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

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

Firefox 配置设置

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

  • 在您的 FF url 上输入“about:config
  • 现在输入“Proxy” ”在搜索字段中
  • 确保“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 使用二进制文件。

Add this New Firefox Profile on your code

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

Firefox configuration settings

This works fine without prompting any authentication when you do the following settings..

  • Type "about:config" on your FF url
  • Now type "Proxy" in the search field
  • Make sure "signon.autologin.proxy" is set "true" (By default
    it is "false")

Load Default/Custom Chrome Profile to run tests using Selenium
WebDriver


  1. Download chromedriver.exe
  2. Extract the chromedriver_win_26.0.1383.0.zip folder and locate .exe file to C:/ folder

Add this Script on your JAVA code

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);

Note: IE doesn't need profile setup to run tests because they run on Server user while Firefox and Chrome works with binary.

べ繥欢鉨o。 2024-11-09 00:17:06

如果要在 Internet Explorer 中启用 http 身份验证,则必须编辑注册表并添加以下内容(如果不存在则创建注册表项):

  1. in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl 创建一个值为 0

    的 DWORD iexplore.exe

  2. \FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE,在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ 中 创建一个值为 0

    的 DWORD iexplore.exe

  3. 关闭并重新打开 Internet Explorer

如果您使用的是 x64 IE,则路径有点不同:

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

If you want to enable the http auth in Internet explorer, you have to edit the registry and add this (create keys if they are not present):

  1. in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create a DWORD iexplore.exe with a value of 0

  2. in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create a DWORD iexplore.exe with a value of 0

  3. Close and reopen Internet explorer

If you have a x64 IE, the path is a bit different :

  • HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
厌味 2024-11-09 00:17:06

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

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

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

There is a solution for performing authentication with Selenium 1.x by manually setting the HTTP headers at http://mogotest.com/blog/2010/06/23/how-to-perform-basic-auth-in-selenium but I don't think this is transferable to Selenium 2, as you don't have access to the headers.

According to the information here 'Basic Authentication support for Selenium 2' was added in Selenium 2 Beta 2 but looking through the source code I can only see it implemented as a way of securing Remote Selenium Servers against anonymous access.

So I think the answer is that BASIC HTTP authentication is not currently supported.

甜嗑 2024-11-09 00:17:06

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

I was not able to use the basic authentication with Selenium 2 and Chrome (Due a bug with Chrome), so I created an extension for Chrome that sends the basic authentication credentials automatically (See https://chrome.google.com/webstore/detail/basic-authentication-auto/dgpgkkfheijbcgjklcbnokoleebmeokn).

月下伊人醉 2024-11-09 00:17:06

Firefox 的多通道扩展让自动化工程师的工作变得轻松。通过这个,我们可以使用任何编程语言处理任何浏览器中的基本身份验证弹出窗口。 PFB步骤:

  1. 打开Firefox浏览器并下载插件
    -> https://addons.mozilla.org/en-US/firefox/addon /多通道/

  2. 现在转到以下位置获取 XPI 文件,该文件是 Firefox 的“multipass”可执行文件
    -> C:\Users\您的用户名\AppData\Roaming\Mozilla\Firefox\Profiles\oleovwxr.extensionUser\extensions

  3. 复制文件 '[email protected]' 从上述目录并将其粘贴到资源包任何文件夹内的项目目录。

  4. 现在使用下面的代码片段来配置 Firefox 驱动程序。

    公共 WebDriver 配置(){

     System.setProperty("webdriver.gecko.driver", "geco 驱动程序路径");
         DesiredCapability 功能 = new DesiredCapability();
         功能.setCapability(CapabilityType.BROWSER_NAME, "firefox");
         功能.setCapability(CapabilityType.PLATFORM_NAME, "WINDOWS");
         功能.setCapability(“acceptSslCerts”, true);
         能力.setCapability(“木偶”, true);
         FirefoxProfile 配置文件 = new FirefoxProfile();
         //给出多通道路径
         profile.addExtension(new File("c:/你的项目名称/src/main/resources/ [电子邮件受保护]"));
         FirefoxOptions firefoxOptions = new FirefoxOptions();
         firefoxOptions.setProfile(配置文件);
         firefoxOptions.merge(功能);
         返回新的 FirefoxDriver(firefoxOptions);
     }
    
  5. 现在的主要挑战是获取下载的多通道扩展的 UUID,因为每次运行它都会发生变化。所以我们每次运行时都会进行记录。
  6. public void setup() 抛出 InterruptedException {

     WebDriver 驱动程序 = config();
         driver.get("关于:调试#/runtime/this-firefox");
         线程.sleep(4000);
         String uuid = driver.findElement(By.xpath("//span[@title='用于 HTTP 基本身份验证的 MultiPass']/parent::li/section/dl/div/dt[contains(text(),'UUID') )]/parent::div/dd")).getText();
         System.out.println("我的网址:::" + "moz-extension://" + uuid + "/popin.html");
         driver.get("moz-extension://" + uuid + "/popin.html");
         //使用您的 URL 以及用户名和密码更改以下 URL
         driver.findElement(By.id("url")).sendKeys("http://mywebsite.com");
         driver.findElement(By.id("用户名")).sendKeys("站点用户名");
         driver.findElement(By.id("密码")).sendKeys("站点密码");
         driver.findElement(By.xpath("//button[.='添加']")).click();
         //现在将下面的 URL 更改为您的 url,注意:: 域应该与上面的多通道 url 进行数学运算
         driver.get("http://mywebsite.com/homeLogin.html");
     }
    
  7. 我已经使用以下 selenium 版本 selenium-java 4.1.1 和 selenium-server 3.141.59 进行了测试。

Multipass extension of Firefox made automation engineers life easy. Through this, we can handle the basic authentication pop-up in any browser using any programing language. PFB the steps:

  1. Open the Firefox browser and download the plug-in
    -> https://addons.mozilla.org/en-US/firefox/addon/multipass/

  2. Now go to the below location to get the XPI file that is the 'multipass' executable file for firefox
    -> C:\Users\Your user name\AppData\Roaming\Mozilla\Firefox\Profiles\oleovwxr.extensionUser\extensions

  3. Copy the file '[email protected]' from the above directory and past it to your project directory inside any folder of the resource package.

  4. Now use the below code snippet to configure the Firefox driver.

    public WebDriver config() {

         System.setProperty("webdriver.gecko.driver", "Path to geco driver");
         DesiredCapabilities capabilities = new DesiredCapabilities();
         capabilities.setCapability(CapabilityType.BROWSER_NAME, "firefox");
         capabilities.setCapability(CapabilityType.PLATFORM_NAME, "WINDOWS");
         capabilities.setCapability("acceptSslCerts", true);
         capabilities.setCapability("marionette", true);
         FirefoxProfile profile = new FirefoxProfile();
         //Give the multipass path
         profile.addExtension(new File("c:/your project name/src/main/resources/[email protected]"));
         FirefoxOptions firefoxOptions = new FirefoxOptions();
         firefoxOptions.setProfile(profile);
         firefoxOptions.merge(capabilities);
         return new FirefoxDriver(firefoxOptions);
     }
    
    1. Now the main challenge is to get the UUID of the downloaded multipass extension as whenever we run it changes. So we are taking every time when run.

    public void setup() throws InterruptedException {

         WebDriver driver = config();
         driver.get("about:debugging#/runtime/this-firefox");
         Thread.sleep(4000);
         String uuid = driver.findElement(By.xpath("//span[@title='MultiPass for HTTP basic authentication']/parent::li/section/dl/div/dt[contains(text(),'UUID')]/parent::div/dd")).getText();
         System.out.println("My Url:::" + "moz-extension://" + uuid + "/popin.html");
         driver.get("moz-extension://" + uuid + "/popin.html");
         //change below URL with your URL and username and password
         driver.findElement(By.id("url")).sendKeys("http://mywebsite.com");
         driver.findElement(By.id("username")).sendKeys("site user name");
         driver.findElement(By.id("password")).sendKeys("site password");
         driver.findElement(By.xpath("//button[.='Add']")).click();
         //Now change below URL with your url, note:: the domain should math with above multipass url
         driver.get("http://mywebsite.com/homeLogin.html");
     }
    
  5. I have tested with the below selenium version selenium-java 4.1.1 and selenium-server 3.141.59.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文