【问题标题】:Capture the API Request after clicking a button单击按钮后捕获 API 请求
【发布时间】:2021-02-27 22:36:47
【问题描述】:

场景: 我们有一个应用程序,它根据表单中给定的过滤器(复选框、单选按钮、下拉菜单、文本框)生成报告。一旦用户点击“生成报告”按钮,就会调用 API Request Header 请求并下载 pdf/csv 报告。

我可以在浏览器“网络”选项卡中看到此请求

Headers[tab] -> 请求标头 - :path api/path/to/download/pdf

Headers[tab] -> 请求 URL:full/api/path/url/to/download/pdf

我们能否使用 JavaScript/Java 捕获此请求 URL?

【问题讨论】:

  • 您能提供您的后端代码的摘录吗?如果您想记录请求,那就是您需要采取行动的地方。任何你在 js 方面做的事情,都可以轻松操作。
  • 是的,只需使用 javascript 进行 get/post 即可检索标题。

标签: javascript java selenium automated-tests


【解决方案1】:

您需要使用代理来捕获网络请求。 Browsermob 代理在 selenium 中很受欢迎。

请参阅https://www.seleniumeasy.com/selenium-tutorials/browsermob-proxy-selenium-example 中的此示例:

   // start the proxy
    proxy = new BrowserMobProxyServer();
    proxy.start(0);

    //get the Selenium proxy object - org.openqa.selenium.Proxy;
    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

    // configure it as a desired capability
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
    
    //set chromedriver system property
    System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
    driver = new ChromeDriver(capabilities);
    
    // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
    proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

    // create a new HAR with the label "seleniumeasy.com"
    proxy.newHar("seleniumeasy.com");

    // open seleniumeasy.com
    driver.get("https://seleniumeasy.com/");

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2020-10-19
    • 2018-10-05
    • 1970-01-01
    相关资源
    最近更新 更多