javascript - 全局变量在量角器中获取值作为 [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
解决方案
您可能应该尝试使用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);
推荐阅读
- javascript - Javascript - axios 请求后脚本不起作用
- regex - 如何使用 regex crate 匹配整个 INI 部分?
- java - eclipse Java中无法解析导入java.io.ByteArrayInputStream
- pytorch - 训练部分网络
- c# - 如何在 ABP 存储库中使用 ToListAsync() 方法?
- php - 在PHP中解密coldfusion加密字符串
- python - WordNet 的同义词集中有什么顺序吗?
- java - 我应该如何在 Firebase 实时数据库中设置键值类型的 getter 和 setter?
- visual-studio - 无法恢复 Chevron.AzureAD.AspNet.Forms 的 1.72 版本
- javascript - JavaScript 上的不同随机字符串比赛匹配