【问题标题】:Mocking XHR call when testing real website using selenium使用 selenium 测试真实网站时模拟 XHR 调用
【发布时间】:2019-07-16 08:13:57
【问题描述】:

我正在尝试将由 Selenium(java 绑定)驱动的 UI 测试与集成层隔离开来。我想拦截通过浏览器的 xhr 调用并发送回模拟响应。

我尝试配置代理(使用 browsermob)并且可以看到请求通过它。但是现在我不确定如何通过拦截 xhr 调用来返回模拟响应。

   Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

        proxy.addRequestFilter((request, content, messageInfo) -> {
                if (messageInfo.getOriginalUrl().contains("v1/listings/closing.json")) {
                    String messageContents = content.getTextContents();
                    System.out.println("messageContents:: " + messageContents);

                }
                return null;
        });


        try {
            String hostIp = Inet4Address.getLocalHost().getHostAddress();
            seleniumProxy.setHttpProxy(hostIp + ":" + proxy.getPort());
            seleniumProxy.setSslProxy(hostIp + ":" + proxy.getPort());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();
        seleniumCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
        ChromeOptions options = new ChromeOptions();
        options.merge(seleniumCapabilities);

        webDriver = new ChromeDriver(options);

在针对真实/实时网站而不是本地主机进行测试时,我希望将模拟响应提供给 UI。

【问题讨论】:

    标签: selenium proxy mocking


    【解决方案1】:

    使用代理。 addResponseFilter((response, contents, messageInfo) 方法

    例子:

    proxy.addResponseFilter((response, contents, messageInfo) -> {
            if (/*...some filtering criteria...*/) {
                contents.setTextContents("This message body will appear in all responses!");
            }
        });
    

    参考blogGithub 您将获得模拟响应的示例。

    您可以考虑 Cypress 将 UI 测试与集成测试隔离开来。使用 Cypress - Fixture 模拟响应会很容易

    【讨论】:

    • 我从博客和 github 代码中了解到的是,这仍然需要网络调用通过,然后更改从外部 url 返回的响应。而我们希望在没有网络调用从博客到外部源点 3 和 4 的情况下少运​​行测试环境。 3.The proxy forwards the request to the external URL. 4. The response from the external URL is passed through a response filter. 我同意 cypress 和 puppeteer 工具使得拦截 xhr 非常容易,如果通过则不会让其通过。
    【解决方案2】:

    Short-circuiting the request with mocked response and not let it pass through using Selenium.

     String stubResponse = { mocked json response body };
    
     proxy.addRequestFilter(new RequestFilter() {
            @Override
            public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpMessageInfo messageInfo) {
                if (request.getUri().contains("/v1/listings/closing.json")) {
    
                    //Use DefaultFullHttpResponse for posting the json body
                    HttpResponse httpResponse = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
                    //Close the connection so it doesn't pass through
                    httpResponse.headers().add("CONNECTION", "Close");
                    //Specify the content-type and charset
                    httpResponse.headers().add("Content-Type", "application/json; charset=UTF-8");
                    //other headers if necessary
                    httpResponse.headers().add("Access-Control-Allow-Headers", "");
                    httpResponse.headers().add("Access-Control-Allow-Origin", "*");
                    httpResponse.headers().add("Access-Control-Expose-Headers","");
    
                    //replace the body 
                    HttpObjectUtil.replaceTextHttpEntityBody((FullHttpMessage) httpResponse, stubResponse);
    
                    return httpResponse;
                }
    
                return null;
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 2010-11-12
      • 2015-02-11
      • 2018-06-18
      • 1970-01-01
      • 2020-10-03
      • 2012-12-08
      相关资源
      最近更新 更多