question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using webDriverIO helper properly

See original GitHub issue

What are you trying to achieve? To use webdriverio helper

What do you get instead? An error “Cannot read property ‘promise’ of undefined”.

webdriverIO test --
 try to locate element
 • I am on page "http://codecept.io/" (13.999 sec)
 • I try locate  (0.001 sec)
 ✖ FAILED in 16342ms


-- FAILURES:

  1) webdriverIO test: try to locate element:
     Cannot read property 'promise' of undefined
  
  Scenario Steps:
  
  - I.tryLocate() at Test.<anonymous> (livetv_test.js:5:7)
  - I.amOnPage("http://codecept.io/") at Test.<anonymous> (livetv_test.js:4:7)
  
  
  
  Run with --verbose flag to see NodeJS stacktrace


  FAIL  | 0 passed, 1 failed   // 16s

Provide test source code if related some_test.js:

Feature('webdriverIO test');

Scenario("try to locate element", function * (I) {
    I.amOnPage("http://codecept.io/");
    I.tryLocate();
});

WebDriverIOHelper.js:

'use strict';

class WebDriverIOHelper extends Helper {
    tryLocate() {
        const element = this.helpers['WebDriverIO'].browser.element;
        return element('h1');
    }
}

module.exports = WebDriverIOHelper;

Details

  • CodeceptJS version: v1.0.0
  • NodeJS Version: v8.4.0
  • Operating System: macOs Sierra v 10.12.6
  • SeleniumWebDriver version: ‘3.4.0’
  • Configuration file:
{
  "output": "./output",
  "helpers": {
    "WebDriverIO": {
      "url": "http://localhost:3000",
      "windowSize": "maximize",
      "browser": "chrome",
      "restart": true,
      "smartWait": 15000
    },
    "WebDriverIOHelper": {
      "require": "./WebDriverIOHelper.js"
    }
  },
  "include": {
    "I": "./custom_steps.js"
  },
  "mocha": {
    "reporterOptions": {
      "mochaFile": "./output/result.xml"
    }
  },
  "bootstrap": "./bootstrap.js",
  "teardown": null,
  "hooks": [],
  "tests": "./*_test.js",
  "timeout": 5000,
  "multiple": {
    "default": {
      "grep": "signin",
      "browsers": [
        "chrome",
        "safari"
      ]
    }
  },
  "name": "tests"
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
reubenmillercommented, Oct 25, 2017

You’re welcome. I just noticed that you have a small typo in the test scenario aync.

Also the advantage of having the helper function return a promise instead of being an async function is that you can call it from the interactive shell and it behaves as expected.

Don’t forget to close the issue if everything works.

0reactions
alias8commented, Oct 25, 2017

Awesome, thank you so much for your help! I adjusted the function to return the text in that element. I’ve updated the github repo with the changes.

    tryLocate(selector = 'h1') {
        const browser = this.helpers['WebDriverIO'].browser;

        return browser.element(selector).getText()
            .then((res) => {
                return res;
            })
            .catch((err) => {
                // Catch the error because webdriver.io throws if the element could not be found
                // Source: https://github.com/webdriverio/webdriverio/blob/master/lib/protocol/element.js
                return null;
            });
    }

and call it by using:

Feature('HomePage Tests');

Scenario('Check home page', async function (I) {
    I.amOnPage('http://codecept.io/');
    if(await I.tryLocate('h1') === "CodeceptJS") {
        console.log("text found!")
    } else {
        console.log("text not found!")
    }
    I.wait(10);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using webDriverIO helper properly · Issue #738 - GitHub
To use webdriverio helper What do you get instead? An error "Cannot read property 'promise' of undefined". webdriverIO test -- try to locate ......
Read more >
Getting Started - WebdriverIO
Note: all WebdriverIO commands are asynchronous and need to be properly handled using async/await . Record tests​. WebdriverIO provides tools to help you...
Read more >
Optimize Test Framework - WebdriverIO Tutorial | #12 - YouTube
webdriverio #javascript #frameworkIn this video, we will look at some effective ways to optimize our tests:- we will create utility helper ...
Read more >
Mobile Automation Testing using WebdriverIO - YouTube
testautomation #appium # webdriverio Get started with Mobile Automation Testing using WebdriverIO & Appium. In this video, we will cover ...
Read more >
Find web elements using the best WEBDRIVERIO ... - YouTube
Webdriverio selectors: CSS, XPath, Link, ByText, By Index, and MORE.# webdriverio #typescript #javascript⏰Timestamps: 00:00​​​​ - Selector ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found