如何使用在 webdriverio 中进行回调的第 3 方方法 [英] How to use 3rd party method that takes callback in webdriverio

查看:29
本文介绍了如何使用在 webdriverio 中进行回调的第 3 方方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里需要一些帮助...我正在使用第 3 方方法,它需要回调...所以现在如果我想在 WDIO 中使用它,我需要将该方法包装在 promise 中...所以我做到了以下内容:

I need some help here... I am using a 3rd party method which takes callback in it ... so now if i want to use that in WDIO i need to wrap that method inside promise ... So i did the following:

post(env, userAccount, canonical, isItQuery){
    let options = { ..... };
    return new Promise(function(resolve, reject){
        request.post(options,function(error, response){
            logger.info('In to the callback of request post');
            if(!error){
                resolve(response);
            }
            else{
                reject(error);
            }                
        });
    });
}

我尝试在 stepDefinition 中调用此方法,如下所示:

And i tried calling this method inside stepDefinition like this:

      rm.post(env,userAccountID,payloadName,true).then(function(resp) {
        console.log('Response: ' + resp);
    })
    .catch(function(error){
        console.log("ERROR: " + error);
    })

在执行期间,脚本不会等待来自 request.post 方法的响应......并且执行正在完成而没有它的响应......请帮助我如何完成这项工作...

During the execution the script is not waiting for the response from request.post method ... and execution is getting completed without its response ... Please help me how i can make this work ...

我使用 request-promise npm-module 尝试了相同的方法,它返回 promise 而不是采用回调并得到相同的问题:

I tried the same using request-promise npm-module which returns promise instead of taking callback and getting the same issue:

这是示例代码:

import {defineSupportCode} from 'cucumber';
import request from 'request-promise';
import config from 'config';
import fs from 'fs';
require('request-promise').debug = true;

defineSupportCode(function({Given, When, Then}){
    Given(/^Run the "([^"]*)" with user_session of "([^"]*)"$/, (canonical, user_session) => {

        .......
        .......
        const payload = fs.readFileSync(path,{encoding:'utf8'});

        let options = {
            ...........
            ...........
        };

        request(options)
        .then(function ($) {
              console.log($);
         })
        .catch(function (err) {
       console.log('error');
         });
    });
});

我正在使用 wdioRunner 和 sync:true.我正在使用黄瓜框架.

I am using wdioRunner with sync:true. I am using cucumber framework.

谢谢!!

推荐答案

好的.. 在一些帮助下我解决了这个问题.所以 wdioRunner(with sync:true) 同步运行每个命令.所以现在如果你想使用一个需要回调的异步方法,那么你需要使用 browser.call.

Ok.. With some help i was able to fix the issue. So wdioRunner(with sync:true) runs every command synchronous. So now if you want to use an async method which takes callback then you need to use browser.call.

http://webdriver.io/api/utility/call.html#Usage

所以 post 方法应该是这样的:

so this is how the post method should be:

post(env, userAccount, canonical, isItQuery){
let options = { ..... };
browser.call(() => {
    return new Promise(function(resolve, reject){
        request.post(options,function(error, response, resp){
            console.log('Inside the callback!!!!!');
            jsonResponse = resp;
            if(!error){
                console.log('NO Error: ' + resp);                    
                resolve(resp);

            }
            else{
                console.log('Error: ' + error);
                reject(error);
            }
    });
});
}

这篇关于如何使用在 webdriverio 中进行回调的第 3 方方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆