question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsing getText() return in JavaScript

See original GitHub issue

Dear Protractor Community,

Thank you for the excellent software. I want to parse http://baconipsum.com/ then take the returned generated meaty text and slice it thinner in protractor before inserting it into my app (for savory filler content). The test below works well, but if I try to convert the return of getText() as a string it comes back empty or as a string that just says ‘object’. For example, if I add:

This works:

'use strict';
describe('Visiting the butcher', function() {
  it('should get meat', function() {

    var ptor = protractor.getInstance();
    ptor.ignoreSynchronization = true;
    var driver = ptor.driver;

    driver.get("http://baconipsum.com/api/?type=all-meat&sentences=1&start-with-lorem=1");

    var meat = ""
    meat = driver.getPageSource();

    expect(meat).toContain("Bacon");
  });
});

But not if I change:

    var meat = ""
    driver.getPageSource().then(function(ss){
        meat = ss;
    });

because meat becomes empty and this fails: expect(meat).toContain(“Bacon”); with error: Expected ‘’ to contain ‘Bacon’.

I am even running it in interactive console mode with selenium and can parse the return as text using a then function to process the response after the promise. Why does this work in interactive selenium driver land and not my protractor js test?:

var meat = "";
driver.findElement(By.tagName("body")).getText().then(function(stuff){meat = stuff;});
> meat.indexOf('Bacon')
89

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
juliemrcommented, Jun 4, 2014

Closing, since this bacon is crispy and done.

1reaction
juliemrcommented, May 23, 2014

You could do that all within the promise callback, like so:

var meat = driver.getPageSource().then(function(ss) {
  return ss.split(" ");
});
// Now meat is a promise which will resolve to an array of strings
meat.then(function(slices) {
  // You could do expectations in here
  expect(slices[0]).toEqual('salami');
  expect(slices[1]).toEqual('black forest');
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

gettext-parser - npm
Parse and compile gettext po and mo files to/from json, nothing more, nothing less. Latest version: 6.0.0, last published: 4 months ago.
Read more >
getText JavaScript and Node.js code examples - Tabnine
How to use. getText. function ... async categoryDisplayed() { return await this.driver . ... src/parser/lufile/newEntitySection.js/NewEntitySection/ ...
Read more >
selenium webdriver getText Returns [object object]
I have the script working but for some reason when I run getText() and check the value that is returned I get [Object...
Read more >
Response.text() - Web APIs - MDN Web Docs
The text() method of the Response interface takes a Response stream and reads it to completion. It returns a promise that resolves with...
Read more >
XML DOM - Get Node Values - W3Schools
The getElementsByTagName() method returns a node list of all elements, with the specified tag name, in the same order as they appear in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found