使用Selenium WebDriver处理Windows NTLM身份验证

时间:2019-02-12 10:12:03

标签: java firefox selenium-webdriver ntlm-authentication

我正在尝试对使用NTLM身份验证协议的Web应用程序运行硒Web驱动程序(Firefox)测试用例。

我使用DesiredCapabilities用“ http://localhost:8080”来更新“ network.automatic-ntlm-auth.trusted-uris” 值,以避免显示认证窗口。

“ network.automatic-ntlm-auth.trusted-uris” 值已更新,但在浏览器中仍然为空。

问题:

  1. 如何设置“ network.automatic-ntlm-auth.trusted-uris”值?
  2. 解决此问题的最佳方法是什么?

请查看下面的截屏代码,以了解更多详细信息。

谢谢。

SSO_Screenshot

public RemoteWebDriver getWebDriverObject(DesiredCapabilities capabilities) {
        String os = SystemUtils.IS_OS_WINDOWS ? "windows" : "linux";
        System.setProperty("webdriver.gecko.driver", "target/test-classes/selenium_standalone_binaries/" + os + "/marionette/64bit/geckodriver.exe");

        FirefoxOptions options = new FirefoxOptions();

        // check the "Network.automatic-ntlm-auth.trusted-uris value before update"
        System.out.println("Capability before update >>>>>" + capabilities.getCapability("Network.automatic-ntlm-auth.trusted-uris"));

        // update the "Network.automatic-ntlm-auth.trusted-uris value" after update
        capabilities.setCapability("Network.automatic-ntlm-auth.trusted-uris", "http://localhost:8080");

        // check the "Network.automatic-ntlm-auth.trusted-uris value after update"
        System.out.println("Capability after update >>>>>" + capabilities.getCapability("Network.automatic-ntlm-auth.trusted-uris"));

        options.merge(capabilities);
        options.setHeadless(HEADLESS);

        return new FirefoxDriver(options);
    }

1 个答案:

答案 0 :(得分:0)

问题已解决。我必须使用FirefoxProfile覆盖所有浏览器配置值。

请检查this以获得更多详细信息。

public RemoteWebDriver getWebDriverObject(DesiredCapabilities capabilities) {
        String os = SystemUtils.IS_OS_WINDOWS ? "windows" : "linux";
        System.setProperty("webdriver.gecko.driver", "target/test-classes/selenium_standalone_binaries/" + os + "/marionette/64bit/geckodriver.exe");

        FirefoxOptions options = new FirefoxOptions();
        options.merge(capabilities);
        options.setHeadless(HEADLESS);

        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "http://localhost:8080");
        profile.setPreference("dom.disable_beforeunload", false);

        options.setProfile(profile);
        options.setLogLevel(FirefoxDriverLogLevel.DEBUG);
        return new FirefoxDriver(options);
    }