How to use 3rd party method that takes callback in webdriverio

独自空忆成欢 提交于 2019-12-02 08:29:52

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

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

You are missing what to return from the promise.

You need a response just try returning it .

You can also return a array with status code and response etc.

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

The response body looks like

{ [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 } }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!