Selenium selenuim 能不能监控 http 的 response

pulord · 2017年03月13日 · 最后由 ovpt 回复于 2017年03月13日 · 1099 次阅读

期望是自动化过程中,任何的操作之后,都去检查一下 http 的 response 的 code,如果实现这个功能

共收到 1 条回复 时间 点赞
  • 如果说原理的话就是
    • 创建一个 proxy,proxy 提供一个接口能够查询所有或最近的一步操作的请求
    • 创建 Webdriver 时指定该代理,webdriver 的每个操作对应的 request 都会经过 proxy
String PROXY = "localhost";
int PORT = 8080;

com.google.gson.JsonObject json = new com.google.gson.JsonObject();
json.addProperty("proxyType", "MANUAL");
json.addProperty("httpProxy", PROXY);
json.addProperty("httpProxyPort", PORT);
json.addProperty("sslProxy", PROXY);
json.addProperty("sslProxyPort", PORT);

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("proxy", json);

GeckoDriverService service =new GeckoDriverService.Builder(firefoxBinary)
  .usingDriverExecutable(new File("path to geckodriver"))
  .usingAnyFreePort()
  .usingAnyFreePort()
  .build();
service.start();

// GeckoDriver currently needs the Proxy set in RequiredCapabilities
driver = new FirefoxDriver(service, cap, cap);
  • 测试脚本中,webdriver 的每个操作后,从 proxy 查询这一步发送的请求和返回,校验 UI 和 response

可以借鉴的例子

browsermob-proxy allows you to manipulate HTTP requests and responses, capture HTTP content, and export performance data as a HAR file.

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册