【问题标题】:How to handle server authentication using Geb/WebDriver如何使用 Geb/WebDriver 处理服务器身份验证
【发布时间】:2013-10-26 23:20:29
【问题描述】:

我有一个网页。当我首先打开该网页时,它要求进行服务器身份验证。提供服务器身份验证后,它允许我浏览网站。 我必须自动化该网页,但由于服务器身份验证,我无法继续前进。 如何在 Geb 或 Web Driver 中处理此服务器身份验证

【问题讨论】:

    标签: selenium automation webdriver selenium-webdriver geb


    【解决方案1】:

    尝试使用这个: http://username:password@site.com/page

    而不是: http://site.com/page

    【讨论】:

      【解决方案2】:

      这称为基本身份验证。所以你可以在地址栏中传递用户名和密码

      http://user:password@example.com
      

      WebDriver 不支持基本身份验证

      【讨论】:

        【解决方案3】:

        我无法使用它,因为我的用户名中有@(是的,Microsoft 帐户)。这就是为什么我决定使用代理解决方案来拦截我的请求并插入准备好的授权标头。而且我想在测试中直接使用代理,所以它是完全自动化的。现在生成的代码相当粗糙,基于Browser.drive 而不是任何最先进的 Geb 代码,但我相信如果您了解 Geb,您可以轻松适应它:

        import geb.Browser
        import geb.Configuration
        import io.netty.handler.codec.http.DefaultHttpRequest
        import io.netty.handler.codec.http.HttpObject
        import io.netty.handler.codec.http.HttpRequest
        import io.netty.handler.codec.http.HttpResponse
        import org.littleshoot.proxy.HttpFilters
        import org.littleshoot.proxy.HttpFiltersAdapter
        import org.littleshoot.proxy.HttpFiltersSourceAdapter
        import org.littleshoot.proxy.HttpProxyServer
        import org.littleshoot.proxy.impl.DefaultHttpProxyServer
        import org.openqa.selenium.firefox.FirefoxDriver
        import org.openqa.selenium.firefox.FirefoxProfile
        
        def baseUrl = System.getProperty("baseUrl", "http://localhost/app/")
        
        // LittleProxy setup
        def proxyPort = 8181
        HttpProxyServer server = DefaultHttpProxyServer.bootstrap()
            .withPort(proxyPort)
            .withFiltersSource(
            new HttpFiltersSourceAdapter() {
                @Override
                HttpFilters filterRequest(HttpRequest originalRequest) {
                    return new HttpFiltersAdapter(originalRequest) {
                        @Override
                        HttpResponse requestPre(HttpObject httpObject) {
                            if (httpObject instanceof DefaultHttpRequest) {
                                if (httpObject.getUri().startsWith(baseUrl)) {
                                    if (httpObject.headers().contains("Authorization")) {
                                        println "Already contains the Authorization header: " + httpObject.getUri()
                                    } else {
                                        println "Adding Authorization header to request: " + httpObject.getUri()
                                        httpObject.headers().add("Authorization", "Basic " + System.getProperty("basicAuth"))
                                    }
                                } else {
                                    println "Ignoring request: " + httpObject.getUri()
                                }
                            } else {
                                println "Ignoring event: " + httpObject
                            }
                            return null
                        }
                    }
                }
            })
            .start();
        
        println "Base URL: " + baseUrl
        
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("network.proxy.http", "localhost");
        profile.setPreference("network.proxy.http_port", proxyPort);
        // to force proxy for localhost or 127.0.0.1 too
        profile.setPreference("network.proxy.no_proxies_on", "");
        // http://kb.mozillazine.org/Network.proxy.type
        // 1 = manual proxy configuration; default 5 would use system settings
        profile.setPreference("network.proxy.type", 1);
        profile.setPreference("browser.fixup.alternate.enabled", false);
        
        def configuration = new Configuration()
        def driver = new FirefoxDriver(profile)
        configuration.setDriver(driver)
        
        Browser.drive(configuration, {
            go baseUrl
        
            def navigator = $("#app-title")
            assert navigator.getAttribute("title") == "App title"
        })
        
        driver.close()
        server.stop()
        

        首先它启动代理 - 基于https://github.com/adamfisk/LittleProxy

        然后它为代理设置 Firefox 配置文件。一些设置与 localhost 的使用有关,但如果您使用任何其他主机名,它不会受到伤害。

        最后我们开始测试,将配置提供给drive(...)调用。

        我还需要获得更多关于这个小代理的经验,它的稳定性等等。但对于这个简单的案例,它工作得很好。只拦截对baseUrl 的请求很重要,因为例如Firefox 可能会调用google 搜索-并且您不想将您的身份验证哈希发送到那里(因为它很容易反转)。这也是我在 JVM 参数中提供它的原因:-DbaseUrl=http://localhost/finrisk -DbasicAuth=xxx...allTheWayToEqualSing=

        显然,它现在适用于 Firefox,但我想它应该可以适用于其他浏览器,如果您可以设置它们的代理设置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-26
          • 1970-01-01
          • 1970-01-01
          • 2021-11-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-19
          相关资源
          最近更新 更多