我使用Selenium作为测试运行器运行集成测试,使用Selenium API的webdriver.io javascript库。 我的测试如下: 我加载一个html页面并点击一个按钮。我想检查是否调用了Get REST调用。使用Selenium捕获REST调用
我找到了一个名为webdriverajax的webdriver.io插件,它可以满足我的要求,但它不起作用。
任何想法如何捕获休息电话?
我使用Selenium作为测试运行器运行集成测试,使用Selenium API的webdriver.io javascript库。 我的测试如下: 我加载一个html页面并点击一个按钮。我想检查是否调用了Get REST调用。使用Selenium捕获REST调用
我找到了一个名为webdriverajax的webdriver.io插件,它可以满足我的要求,但它不起作用。
任何想法如何捕获休息电话?
你可以通过使用自定义的HttpClient
类来实现这一点,硒代码的外侧。据我所知,硒不支持此功能。
假设当你单击该按钮就会叫REST
的服务,URL可以是从HTML
DOM
抢element.Then您可以使用您的自定义代码来验证URL是否可访问或not.Then你可以,如果你决定测试通过或失败的基础上status code
或其他一些机制。
FileDownloader.java(样品的代码片段)
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使用这个你也可以实现你想要的。
问题是webdriver.io版本。显然,webdriverajax只适用于webdriver.io v3.x而不适用于v4.x。我使用v4.5.2。
我决定不使用插件和实施window.XMLHttpRequest 开一个模拟发送方法,如下所示:
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;
}