【问题标题】:How to use 3rd party method that takes callback in webdriverio如何使用在 webdriverio 中进行回调的 3rd 方方法
【发布时间】:2018-08-10 17:43:28
【问题描述】:

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

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 中像这样调用这个方法:

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

在执行过程中,脚本不等待来自request.post 方法的响应...并且在没有响应的情况下执行完成...请帮助我如何使这项工作...

我尝试使用返回promiserequest-promise npm-module 进行相同的操作,而不是进行回调并遇到相同的问题:

这里是示例代码:

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

我正在使用带有sync:true 的 wdioRunner。我正在使用黄瓜框架。

谢谢!!

【问题讨论】:

    标签: webdriver-io


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      你错过了承诺的回报。

      您需要回复,请尝试返回它。

      您还可以返回一个包含状态码和响应等的数组。

      安装 request-promise npm install --save request-promise

      var rp = require('request-promise');
      var cheerio = require('cheerio'); 
      var options = {
              uri: 'http://www.google.com',
                        transform: function (body) {
                           return cheerio.load(body);
                         }
                     }; 
       rp(options)
           .then(function ($) {
                 console.log($);
            })
           .catch(function (err) {
          console.log('error');
            });
      

      响应体看起来像

      { [Function: initialize]
        fn:
         initialize {
           constructor: [Circular],
           _originalRoot:
            { type: 'root',
              name: 'root',
              namespace: 'http://www.w3.org/1999/xhtml',
              attribs: {},
              'x-attribsNamespace': {},
              'x-attribsPrefix': {},
              children: [Array],
              parent: null,
              prev: null,
              next: null } },
        load: [Function],
        html: [Function],
        xml: [Function],
        text: [Function],
        parseHTML: [Function],
        root: [Function],
        contains: [Function],
        merge: [Function],
        _root:
         { type: 'root',
           name: 'root',
           namespace: 'http://www.w3.org/1999/xhtml',
           attribs: {},
           'x-attribsNamespace': {},
           'x-attribsPrefix': {},
           children: [ [Object], [Object] ],
           parent: null,
           prev: null,
           next: null },
        _options:
         { withDomLvl1: true,
           normalizeWhitespace: false,
           xml: false,
           decodeEntities: true } }
      

      【讨论】:

      • 嗨,Bharath,感谢您帮助我...。您能帮我重写上面的代码吗...对不起,我是 JS 和 wdio 的初学者,所以我没有明白您的意思。 .. 一个示例代码可以帮助我...提前谢谢你!!!
      • 嗨 Bharath .. 感谢您编辑答案.. 我试过了,但没用.. WDIO 不等待回复...
      • 您可以尝试编辑您的问题 Naveen 吗?随着你所做的改变
      • 您好,您需要提及给定小黄瓜的超时时间
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 2013-04-22
      相关资源
      最近更新 更多