【问题标题】:Failing at Logging HTTP Requests for testing file download记录测试文件下载的 HTTP 请求失败
【发布时间】:2019-07-10 09:57:13
【问题描述】:

我正在尝试复制这个:

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);

})

【问题讨论】:

  • 能否请您提供错误的文本?

标签: typescript automated-tests e2e-testing web-testing testcafe


【解决方案1】:

我注意到在提供的示例中,您没有指定夹具或测试的起始页。这可能是错误的原因。

您提到的 TestCafe 示例执行以下操作:

  1. 创建一个RequestLogger 实例,该实例监控对开始测试的同一页面的请求(标​​头和正文)。
  2. 点击按钮开始下载文件。
  3. 等待服务器的成功响应。
  4. 将响应正文保存到文件中。

过滤器对象中的url参数对应请求发送到的页面(documentation),不必与测试页面的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();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2021-01-12
    • 2016-09-07
    • 2014-11-19
    相关资源
    最近更新 更多