【问题标题】:Capture REST calls with Selenium使用 Selenium 捕获 REST 调用
【发布时间】:2016-12-31 17:45:23
【问题描述】:

我使用 Selenium 作为测试运行程序和 webdriver.io Selenium API 的 javascript 库运行集成测试。 我的测试如下: 我加载一个 html 页面并单击一个按钮。我想检查是否调用了 Get REST 调用。

我找到了一个名为 webdriverajax 的 webdriver.io 插件,该插件旨在满足我的要求,但它不起作用。

任何想法如何捕获休息电话?

【问题讨论】:

    标签: javascript rest selenium webdriver-io


    【解决方案1】:

    您可以通过使用 selenium 代码之外的自定义 HttpClient 类来实现这一点。据我所知,selenium 不支持此功能。

    假设当您单击该按钮时,它将调用REST 服务,可以从HTMLDOM 元素中获取URL。然后您可以使用您的自定义代码来验证URL 是否可访问。然后您可以根据status code 或其他您的机制来决定您的测试是通过还是失败。

    FileDownloader.java(示例代码 sn-p)

    private String downloader(WebElement element, String attribute) throws IOException, NullPointerException, URISyntaxException {
            String fileToDownloadLocation = element.getAttribute(attribute);
            if (fileToDownloadLocation.trim().equals("")) throw new NullPointerException("The element you have specified does not link to anything!");
    
            URL fileToDownload = new URL(fileToDownloadLocation);
            File downloadedFile = new File(this.localDownloadPath + fileToDownload.getFile().replaceFirst("/|\\\\", ""));
            if (downloadedFile.canWrite() == false) downloadedFile.setWritable(true);
    
            HttpClient client = new DefaultHttpClient();
            BasicHttpContext localContext = new BasicHttpContext();
    
            LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState);
            if (this.mimicWebDriverCookieState) {
                localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies()));
            }
    
            HttpGet httpget = new HttpGet(fileToDownload.toURI());
            HttpParams httpRequestParameters = httpget.getParams();
            httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects);
            httpget.setParams(httpRequestParameters);
    
            LOG.info("Sending GET request for: " + httpget.getURI());
            HttpResponse response = client.execute(httpget, localContext);
            this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode();
            LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt);
            LOG.info("Downloading file: " + downloadedFile.getName());
            FileUtils.copyInputStreamToFile(response.getEntity().getContent(), downloadedFile);
            response.getEntity().getContent().close();
    
            String downloadedFileAbsolutePath = downloadedFile.getAbsolutePath();
            LOG.info("File downloaded to '" + downloadedFileAbsolutePath + "'");
    
            return downloadedFileAbsolutePath;
        }
    

    TestClass.java

    @Test
    public void downloadAFile() throws Exception {
    
    
           FileDownloader downloadTestFile = new FileDownloader(driver);
            driver.get("http://www.localhost.com/downloadTest.html");
            WebElement downloadLink = driver.findElement(By.id("fileToDownload"));
            String downloadedFileAbsoluteLocation = downloadTestFile.downloadFile(downloadLink);
    
            assertThat(new File(downloadedFileAbsoluteLocation).exists(), is(equalTo(true)));
            assertThat(downloadTestFile.getHTTPStatusOfLastDownloadAttempt(), is(equalTo(200)));
     // you can use status code to valid  the REST URL
        }
    

    Here 是参考。

    注意:这可能不完全符合您的要求,但您可以得到一些想法并相应地修改它以适应您的要求。

    也可以参考BrowserMob Proxy 使用这个你也可以实现你想要的。

    【讨论】:

      【解决方案2】:

      问题出在 webdriver.io 版本上。显然,webdriverajax 仅适用于 webdriver.io v3.x,但不适用于 v4.x。我使用 v4.5.2。

      我决定不使用插件并为 window.XMLHttpRequest 实现一个模拟 open 和 send 方法,如下:

      proxyXHR() {
        this.browser.execute(() => {
          const namespace = '__scriptTests';
          window[namespace] = { open: [], send: [] };
          const originalOpen = window.XMLHttpRequest.prototype.open;
          window.XMLHttpRequest.prototype.open = function (...args) {
            window[namespace].open.push({
              method: args[0],
              url: args[1],
              async: args[2],
              user: args[3],
              password: args[4]
            });
            originalOpen.apply(this, [].slice.call(args));
          };
          window.XMLHttpRequest.prototype.send = function (...args) {
            window[namespace].send.push(JSON.parse(args[0]));
          };
        });
      }
      
      getXHRsInfo() {
        const result = this.browser.execute(() => {
          const namespace = '__scriptTests';
          return window[namespace];
        });
        return result.value;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-13
        • 2013-01-05
        • 1970-01-01
        • 1970-01-01
        • 2013-07-13
        • 2014-12-16
        相关资源
        最近更新 更多