简体   繁体   中英

How to access site JavaScript functions using webdriverIO

I have a problem with accessing code that I am able to use through browser console. In my case it is a Tawk_Api function Tawk_API.hideWidget(); I tried to use browser execute and call but the output saying that Tawk.Api is not defined

Code example

 var expect = require('chai').expect;  
    function HideTawk (){
        Tawk_API.hideWidget();
    }
    describe('', function() {
        it('should be able to filter for commands', function () {
            browser.url('https://arutech.ee/en/windows-price-request');
            $('#uheosaline').click();
            browser.execute(HideTawk());
        var results = $$('.commands.property a').filter(function (link) {
            return link.isVisible();
        });
        expect(results.length).to.be.equal(3);
        results[1].click();
        expect($('#getText').getText()).to.be.equal('GETTEXT');
    });
});

Working fixed function:

  function HideTawk (){
        return new Promise(function(resolve, reject) {
            Tawk_API.hideWidget();
        })
    }

And browser.execute(HideTawk()) is a mistake it should be browser.call(HideTawk());

docs: http://webdriver.io/api/utility/call.html

I have a below code in my application base object it can help you to understand call api:

_callClientAPI(func, args) {
    let trial = 1;
    return new Promise(async(res, rej) => {
      while (true) {
        if (trial > this._pollTrials) {
          rej(`Could not retrieve the element in this method * this._pollTimeout} seconds.`);
          break;
        }

        let result;
        try {
          result = await func.call(this.client, args, false);
        } catch (e) { }

        if (result && result !== '') {
          res(result);
          break;
        }

        await this.wait();
        trial++;
      }
    });
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM