1

I have a table with 3 columns 'firstname', 'lastname', 'birthday', like this:

<table id="table">
    <tr>
        <td id="firstname">John</td>
        <td id="lastname">Smith</td>
        <td id="birthday">Jan 1 2014</td>
    <tr>
    <tr>
        <td id="firstname">Bill</td>
        <td id="lastname">Matthews</td>
        <td id="birthday">Jan 2 2014</td>
    <tr>
</table>

I want to create a JSON from this table, like this:

{
    firstname: "John",
    lastname:  "Smith",
    birthday:  "Jan 1 2014"
},
{
    firstname: "Bill",
    lastname:  "Matthews",
    birthday:  "Jan 2 2014"
}

I've trying something like this:

var tableRows = [];
element.all(by.tagName('tr')).each( function(element) {
    tableRows.push(
                     {
                         firstname: element(by.id('firstname')).getText(),
                         lastname:  element(by.id('lastname')).getText(),
                         birthday:  element(by.id('lastname')).getText()
                     }
                  );
});
3
  • I didn't understand can you explain what should be returned? Commented Jan 9, 2015 at 4:45
  • then whats the question ? Commented Jan 9, 2015 at 4:55
  • This last part, the tests, doesn't work as expected. It's returning [1, 2], instead of the JSON objects. Commented Jan 9, 2015 at 5:20

1 Answer 1

3

Use map(), quote from the changelog:

Added a map function to element.all to apply a function to each element and return the result of the transformation.

Resolve promises if there is an object that contains multiple promises. Added index as a second argument to the map function callback.

Key point here is that it would resolve multiple promises:

var items = element.all(by.tagName('tr')).map(function (tr) {
    return {
               firstname: tr.element(by.id('firstname')).getText(),
               lastname: tr.element(by.id('lastname')).getText(),
               birthday: tr.element(by.id('birthday')).getText()
           };
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.