typescript - 记录测试文件下载的 HTTP 请求失败
问题描述
我试图复制这个:
https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/
我的目标页面有target="_blank"
没有办法改变删除这个的属性target
?
所以测试失败。
示例中的代码的作用不是很清楚。URL 是否必须是存在下载链接的页面?
import { ClientFunction, t, Selector, RequestLogger } from 'testcafe'
import * as fs from 'fs';
import * as path from 'path';
...
const forInvoice = new ForInvoice()
const client = 'STRV s.r.o.'
const url = urlFor('?/timers/unbilledOverview')
const logger = RequestLogger({ url, method: 'post' }, {
logResponseHeaders: true,
logResponseBody: true
});
fixture.only `For Invoicing`
.requestHooks(logger);
test('Verify download of .xls and .pdf', async t => {
await t.useRole(ADMIN_INVOICE)
await forInvoice.navigateToForInvoicing()
await forInvoice.filterClient(client)
await t
.click(Selector('a').filter('.sc-mini-icon-file.excel-file'))
.expect(logger.contains(r => r.response.statusCode === 200)).ok();
const filePath = path.join(__dirname, 'STRV-s-r-o-Attachment');
console.log(filePath);
console.log(logger.requests[0].response.headers);
fs.writeFileSync(filePath, logger.requests[0].response.body);
})
解决方案
我注意到在提供的示例中,您没有指定夹具或测试的起始页。这可能是错误的原因。
您提到的 TestCafe 示例执行以下操作:
- 创建一个
RequestLogger
实例,该实例监视对开始测试的同一页面的请求(标头和正文)。 - 单击按钮以启动文件下载。
- 等待服务器的成功响应。
- 将响应正文保存到文件中。
过滤器对象中的url
参数对应于请求发送到的页面(文档),不必与测试页面的 URL 匹配。
您可以使用带有标准 Web API 的ClientFunction修改页面上的元素。您可以使用以下代码开始:
import { Selector, ClientFunction } from 'testcafe'
const link = Selector('a');
const removeTarget = ClientFunction(() => {
link().removeAttribute('target');
}, { dependencies: { link } })
fixture`Fixture name`
.page`https://your_page.com/`;
test('Remove target', async t => {
await removeTarget();
});
推荐阅读
- asp.net-mvc - 带有 Angular 5 项目的 ASp.NET MVC - 添加没有 angular-CLI.json 文件的环境配置
- java - java.lang.NumberFormatException:空字符串无法启动活动组件信息
- spring-boot - 杰克逊如何在过渡之外序列化对象
- python - 如何查看动态库文件(.dylib)的函数定义?
- android - RecyclerView.OnItemTouchListener 不定期调用 OnItemClick 和 onLongItemClick
- git - 根据文件夹设置 git 凭据
- python - ValueError:信号仅在连接到 BIG IP 设备时在主线程错误中起作用
- ios - 如何在iOS中滑动离子滑动时禁用垂直滚动?
- spring-boot - 如何在 vert.x 中实现 Zipkin
- rust - 在 Rust 中指定 FFI 库的链接路径有哪些不同的方法?