如何使用Geb / WebDriver处理服务器身份验证

时间:2013-10-18 07:38:32

标签: selenium automation webdriver selenium-webdriver geb

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

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

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

http://user:password@example.com

WebDriver

不支持基本身份验证

答案 2 :(得分:0)

我无法使用此功能,因为我的用户名是@(是的,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搜索 - 而且您不想在那里发送auth哈希(因为它很容易反转)。这也是我在JVM参数中提供它的原因:-DbaseUrl=http://localhost/finrisk -DbasicAuth=xxx...allTheWayToEqualSing=

显然,它现在适用于Firefox,但我想如果你可以设置代理设置,应该可以将它改编为其他浏览器。