【问题标题】:TestCafe Triggering Test By POST Request In ExpressTestCafe 通过 Express 中的 POST 请求触发测试
【发布时间】:2020-05-16 22:06:53
【问题描述】:

我有一个问题似乎在任何地方都没有得到回答。

我正在我的 Express.js api 中运行测试。我设置了一个页面,该页面有一个按钮和一个字段来输入要在 testcafe 测试期间使用的关键字。我设置的端点是/testcafe。但是在向/testcafe 发送了一个帖子请求后,测试运行有很长的延迟,所以我的问题是除了挂起之外最好的下一步是什么?

另外,我的post request body,包含关键字,可以直接用在这样的测试中吗?请记住这是这种模式:

前端 -> POST 请求 -> Express 服务器 -> /testcafe 端点 - 测试

我的问题是在它达到测试后,我目前正在尝试从请求记录器中调用 fetch。是这样吗?

import { ClientFunction, Selector } from 'testcafe';
import { RequestLogger, RequestHook } from 'testcafe';
import zlib from 'zlib';
import fetch from 'isomorphic-unfetch';

const url = 'https://www.mysitetesturl.com/page';

class MyRequestHook extends RequestHook {
    constructor (requestFilterRules, responseEventConfigureOpts) {
        super(requestFilterRules, responseEventConfigureOpts);
    }

    onRequest (e) {
        console.log('in onRequest!')
        console.log('========================')
        console.log('Request Body')
        let buf = e._requestContext.reqBody
        console.log(buf.toLocaleString())
    }

    onResponse (e) {

    let buf = Buffer(e.body) 
    let unzippedBody = Buffer(zlib.gunzipSync(buf)) 
    let payload = unzippedBody.toLocaleString()
    fetch('http://myapiipaddress/api/testcafe',
        method: 'PUT',
        body: JSON.stringify(payload)
        )
        .then((err, doc) => {
            if(err) {
                console.log(err)
            } else {
                console.log(doc)
            }
        })   
    }
}

const myRequestHook = new MyRequestHook({
    url: url, 
    method:'get'},
    { 
        includeHeaders: true, 
        includeBody: true 
    }
);

fetch('http://myapiipaddress/api/testcafe',
    method: 'GET'
    )
    .then((err, doc) => {
        if(err) {
            console.log(err)
        } else {


            fixture`myfixture`
                .page(doc.url)
                .requestHooks(myRequestHook);

            test(`mytest`, async t => { 
            const inputField = Selector('input');

            await t
                await t
                .wait(5000)
                .typeText(inputField, doc.text)
                .wait(5000)

                }   
            );

        }
    })

【问题讨论】:

    标签: node.js express testing mongoose testcafe


    【解决方案1】:

    根据您的方案,您需要以不同的方式组织代码:

    const createTestCafe = require('testcafe');
    ....
    
    // Choose the necessary body parser for express application
    // https://github.com/expressjs/body-parser
    app.use(bodyParser.urlencoded({ extended: true })); 
    ...
    app.post('/', function (req, res) {
        createTestCafe('localhost', 1337, 1338, void 0, true)
           .then(testcafe => {
               const runner = testcafe.createRunner();
    
               return runner
                   .src('/tests')
                   .browsers('chrome')
                   .run();
            })
           .then(failedCount => {
              testcafe.close();
              res.end();
        });
    
    })
    
    

    【讨论】:

    • 如何在测试中使用请求体?我在 req.body.keyword 中发送 JSON。如何在测试中访问 req.body.keyword?我没有看到您传递请求的部分,因此它可以在测试中使用,例如在搜索栏中输入。
    • 您可以使用process.env 或TestCafe clientScripts 功能将req.body.keyword 传递给测试。请检查以下 StackOverflow 线程的答案:https://stackoverflow.com/questions/58075755/how-can-i-inject-parameters-into-a-testcaf%C3%A9-test
    • @aleks-pro 我得到了 process.env 工作但新问题。所以,我刚刚安装了 dotenv 库并调用了 .TypeText(inputField, process.env.KEYWORD)。但是,在使用一个关键字成功测试后,当我使用新关键字(例如,'frogs')运行下一个测试时,它使用来自 FIRST 测试的关键字,即使我在下一个测试。它仍然输入“青蛙”。为什么是这样?我有 fs.writeFile 将关键字写入 .env 文件,如下所示:KEYWORD=bacon.
    • @rom,你是不是在fs.writeFile重写了.env文件中的关键字之后调用了config方法?为什么不在代码中明确设置process.env.KEYWORDprocess.env.KEYWORD='bacon'?这个解决方案对我来说看起来更简单。
    • @aleks-pro 我找到了我的错误所在。我把 dotenv 库搞混了(我一直使用 dotenv 并创建 process.env,但由于保存到 .env 文件,这是不同的方法)。我最终意识到我可以任意为 process.env 分配任何值和任意数量的值并最终走得更远: process.env.KEYWORD = 'bacon', process.env.URL = 'urlhere', process.env .TACOS = '周二'。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多