栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

防止在Selenium Webdriver测试中加载外部内容

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

防止在Selenium Webdriver测试中加载外部内容

解决方法是使用代理。Webdriver与browsermob代理集成得很好: http
://bmp.lightbody.net/

private WebDriver initializeDriver() throws Exception {    // Start the server and get the selenium proxy object    ProxyServer server = new ProxyServer(proxy_port);  // package net.lightbody.bmp.proxy    server.start();    server.setCaptureHeaders(true);    // Blacklist google analytics    server.blacklistRequests("https?://.*\.google-analytics\.com/.*", 410);    // Or whitelist what you need    server.whitelistRequests("https?://*.*.yoursite.com/.*. https://*.*.someOtherYourSite.*".split(","), 200);    Proxy proxy = server.seleniumProxy(); // Proxy is package org.openqa.selenium.Proxy    // configure it as a desired capability    DesiredCapabilities capabilities = new DesiredCapabilities();    capabilities.setCapability(CapabilityType.PROXY, proxy);    // start the driver   ;    Webdriver driver = new FirefoxDriver(capabilities);    return driver;}

编辑:人们经常要求提供http状态代码,您可以使用代理轻松检索它们。代码可以是这样的:

// create a new har with given labelpublic void setHar(String label) {    server.newHar(label);}public void getHar() throws IOException {    // FIXME : What should be done with the this data?    Har har = server.getHar();    if (har == null) return;    File harFile = new File("C:\localdev\bla.har");    har.writeTo(harFile);    for (HarEntry entry : har.getLog().getEntries()) {        // Check for any 4XX and 5XX HTTP status pres        if ((String.valueOf(entry.getResponse().getStatus()).startsWith("4"))     || (String.valueOf(entry.getResponse().getStatus()).startsWith("5"))) { log.warn(String.format("%s %d %s", entry.getRequest().getUrl(), entry.getResponse().getStatus(),         entry.getResponse().getStatusText())); //throw new UnsupportedOperationException("Not implemented");        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649305.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号