我是量角器的新手,在创建测试脚本时,我在函数 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
global.store = element(by.xpath("(//table/tbody/tr/td[@class='ng-scope'][2])[1]")).getText();
// Because all protractor API are Async and return promise,
// instead of return the eventual value.
// To consume the eventual value you need to do it within `then()`
// Or use `await`
global.store.then(function(text){
return element(by.xpath("//div[contains(text(),'" +text+ "')]")).click();
});
您可能应该尝试使用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);