WebdriverIO - 如何将字符串转换回 waitForDisplayed 的 Xpath 定位器

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

我正在使用 WebdriverIO V5 + nodejs 和 mocha。

我在页面对象模型 .js 中有一个如下所示的 xpath 映射

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

然后我使用一个函数用需要的数据替换#

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

参考函数:

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

参考页

data.js

    module.exports = {
    value: "win 7"

    }

功能有效,'newxpath'是一种对象,现在我需要执行WebdriverIO waitfordisplay命令,所以下一个命令是

 newxpath.waitForDisplayed(9000);

但是出现这个错误 --> "Cannot read property 'waitForDisplayed' of null"

看起来 newxpath 与 Webdriver 预期的不一样。如何正确执行这些步骤?

不确定你为什么要在字符串和对象之间进行转换。

只需像这样使用新的 xpath(字符串,而不是 obj):

$(newxpath).waitForDisplayed(9000);