-1

I am new in protractor, While creating a test script I get one value in function 1 & saved it to global variable & tried using it in another function.

Got the value in one function as

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

Now tried using same value in another function as

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

Showing error as

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

AmitS
  • 5
  • 4

2 Answers2

0

You should probably try using JSON.stringify()

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

To make a String representing the object stored in 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));

As per the OPs comment:

Use a custom function with JSON.stringify() that eliminates circular definitions:

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);
Source

You can then use the text variable in your code:

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

Edit as per the OP's comment:

The String you want is in obj.parentElementArrayFinder.actionResults_.value_[0]. This is how you access it:

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);
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • Thanks for quick reply Luca , but when I have tried above solution , it gives error as Failures: Message: [31m Failed: Converting circular structure to JSON[0m Stack: TypeError: Converting circular structure to JSON – AmitS May 18 '18 at 10:26
  • On text variable it stores value as"{"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":64201,"_repeat":214748......] – AmitS May 18 '18 at 10:53
  • its should only get "DLL000010" – AmitS May 18 '18 at 10:54
  • is `DLL000010` somewhere in the JSON object? – Luca Kiebel May 18 '18 at 10:55
  • yes it is... "value_":["DLL000010"],"queue_":null}},"elementArrayFinder_":{}} [31mF[0m – AmitS May 18 '18 at 10:57
  • can you please post the entire value to an edit to your question? – Luca Kiebel May 18 '18 at 10:58
  • Again. In this case you don't need to stringify the JSON data. Note, that with the data you gave me, there were no circular definitions – Luca Kiebel May 18 '18 at 11:14
  • ohhk.. means I am back on the situation where we have started.. Thanks for your efforts. – AmitS May 18 '18 at 11:18
  • Well, in that case there simply is no element found with that locator – Luca Kiebel May 18 '18 at 11:20
  • @Luca, you get an entire wrong direction. Because all protractor API are Async, each of them return a promise, not the eventual value. – yong May 18 '18 at 13:00
  • Hm, that might be true, in that case, I seem to have too little knowledge about the library – Luca Kiebel May 18 '18 at 15:31
0
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();
});
yong
  • 13,357
  • 1
  • 16
  • 27