如果我必须进行REST调用并将响应值与UI进行比较,那么步骤定义将如何构建?

问题描述 投票:0回答:1

这是一个示例步骤定义,我希望最终从UI和GET API获取学生的名字和姓氏并进行比较。此时,我只是尝试打印GET响应,然后从那里构建测试用例

但它没有进行服务调用,而黄瓜显示了通过的步骤。我确定我的步骤定义格式不正确。我怎么能以差异的方式写这个。我在这里提到这篇文章: - https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/step_definitions.md

            Then(/^I check the student info against the service response$/, function(){
                var request = require('request');
                var options = {
                    method: 'GET',
                    url: 'https://examples.com/manager/01',
                    headers: {
                        'Authorization': 'username',
                        'Accept': 'application/json',
                        'Accept-Language': 'en-us',
                        'userID': 'Ap123'
                    },
                    };

function callback(error, response){
                        console.log("inside callback");
                        if (!error && response.statusCode == 200) {
                            var info = JSON.parse(body);
                            console.log(response);
                            console.log(info);
                    }
                }

                //text from page object and call to GET Service
                return this.pages.prd2Page.getstudentInfo().then(text => {
                    console.log("text from the UI: " + text);
                    request(options, callback);
                });
            });
javascript rest protractor cucumber
1个回答
0
投票

我能够以这种方式成功进行GET调用

Then(/^I check the manager info against the serice response$/, function () {
    //var request = require('request');
    var request = require('request-promise');
    var options = {
        method: 'GET',
        url: 'https://examples.com/manager/01',
        strictSSL: false,
        headers: {
            'Authorization': 'username',
            'Accept': 'application/json',                
            'userID': 'Ap123'
        },

    };

    return request(options).then(repos => {
        console.log(repos);
    }).catch(err => {
        console.log("Api call failed");
    });
});

我删除了UI步骤。我必须把它们放回去,但由于承诺的怪癖,服务没有被调用。我安装了另一个名为“request-promise”的npm模块来帮助我使用“.then”语法来构建我的步骤定义

© www.soinside.com 2019 - 2024. All rights reserved.