Search code examples
javascriptxpathwebdriver-io

WebdriverIO - How to convert a string back to Xpath locator for waitForDisplayed


I am using WebdriverIO V5 + nodejs with mocha.

I have an xpath mapped like below in Page Object model .js

eg: get valueLocator() { return ("//tag[contains(text(),'#')]"); }

then I use a function to replace # with required data using

import Data from './page/data'
    var xpath = this.valueLocator;
    var replacexpath = xpath.replace('#',Data.value);
    var newxpath = this.strToObj(replacexpath);

reference function:

strToObj(str){
    var obj = {};
    if(str&&typeof str ==='string'){
        var objStr = str.match(/\{(.)+\}/g);
        eval("obj ="+objStr);
    }
    return obj
 }

reference page

data.js

    module.exports = {
    value: "win 7"

    }

Function works and 'newxpath' is a type of object, now I need to execute WebdriverIO waitfordisplay command, so next command is

 newxpath.waitForDisplayed(9000);

But getting this error --> "Cannot read property 'waitForDisplayed' of null"

Looks like newxpath is not as expected by Webdriver. How to do these steps correctly?


Solution

  • Not sure why are you doing converse between string and object.

    Just use your new xpath (string, not obj) like this:

    $(newxpath).waitForDisplayed(9000);