首页 > 解决方案 > 全局变量在量角器中获取值作为 [Object object]

问题描述

我是量角器的新手,在创建测试脚本时,我在函数 1 中得到一个值并将其保存到全局变量并尝试在另一个函数中使用它。

将一个函数中的值设为

global.store = element(by.xpath("(//table/tbody/tr/td[@class='ng-scope'][2])[1]")).getText();   

现在尝试在另一个函数中使用相同的值

element(by.xpath("//div[contains(text(),'" +store+ "')]")).click();

显示错误为

Failed: No element found using locator: By(xpath, //div[contains(text(),'[object Object]')])[0m

标签: javascriptprotractorglobal-variables

解决方案


您可能应该尝试使用JSON.stringify()

element(by.xpath("//div[contains(text(),'" +JSON.stringify(store)+ "')]")).click();

制作一个表示存储在 中的对象的字符串store

let obj = {"foo":"bar"};

console.log(obj.toString()); // toString get's called when adding an object to a String in JS

console.log(JSON.stringify(obj));


根据 OP 评论:

使用JSON.stringify()消除循环定义的自定义函数:

let obj = {"foo":"bar"};
obj.o = obj; //circular definition


let cache = [];

let text = JSON.stringify(obj, function(key, value) {
    if (typeof value === 'object' && value !== null) {
        if (cache.indexOf(value) !== -1) {
            // Circular reference found, discard key
            return;
        }
        // Store value in our collection
        cache.push(value);
    }
    return value;
});
cache = null; // Enable garbage collection

console.log(text);
资源

然后,您可以在代码中使用该text变量:

element(by.xpath("//div[contains(text(),'" +text+ "')]")).click();

根据 OP 的评论进行编辑:

你想要的字符串在obj.parentElementArrayFinder.actionResults_.value_[0]. 这是您访问它的方式:

let obj = {"browser_":{"driver":{"flow_":{"propagateUnhandledRejections_":true,"activeQueue_":{"name_":"TaskQueue::651","tasks_":[],"interrupts_":null,"pending_":null,"subQ_":null,"state_":"new","unhandledRejections_":{}},"taskQueues_":{},"shutdownTask_":null,"hold_":{"_called":false,"_idleTimeout":2147483647,"_idlePrev":{"_timer":{},"_unrefed":false,"msecs":2147483647,"nextTick":false},"_idleStart":76744,"_repeat":2147483647,"_destroyed":false}},"session_":{"stack_":null,"parent_":null,"callbacks_":null,"state_":"fulfilled","handled_":true,"value_":"971758c120a30c6a741c31c905833dea","queue_":{"name_":"TaskQueue::26","tasks_":[],"interrupts_":null,"pending_":null,"subQ_":null,"state_":"finished","unhandledRejections_":{}}},"executor_":{"w3c":false,"customCommands_":{},"log_":{"name_":"webdriver.http.Executor","level_":null,"parent_":{"name_":"webdriver.http","level_":null,"parent_":{"name_":"webdriver","level_":null,"parent_":{"name_":"","level_":{"name_":"OFF","value_":null},"parent_":null,"handlers_":null},"handlers_":null},"handlers_":null},"handlers_":null}},"fileDetector_":null},"baseUrl":"","getPageTimeout":10000,"params":{},"resetUrl":"data:text/html,<html></html>","debugHelper":{},"ready":{"stack_":null,"parent_":null,"callbacks_":null,"state_":"fulfilled","handled_":true,"queue_":null},"trackOutstandingTimeouts_":true,"mockModules_":[{"name":"protractorBaseModule_","args":[true]}],"ExpectedConditions":{},"plugins_":{"pluginObjs":[],"assertions":{},"resultsReported":false},"allScriptsTimeout":11000,"internalRootEl":"","internalIgnoreSynchronization":true},"parentElementArrayFinder":{"locator_":{"using":"xpath","value":"(//table/tbody/tr/td[@class='ng-scope'][2])[1]"},"actionResults_":{"stack_":null,"parent_":null,"callbacks_":null,"state_":"fulfilled","handled_":false,"value_":["DLL000010"],"queue_":null}},"elementArrayFinder_":{}};

let wantedText = obj.parentElementArrayFinder.actionResults_.value_[0];

console.log(wantedText);


推荐阅读