how to query dom elements
I got a question about the dom element query, for example, i have following html
<div class="items">
<div class="itemA">
<span class="name">nameA1</span>
<div class="topics">
<span class="topic">topicA1</span>
<span class="topic">topicA2</span>
<span class="topic">topicA3</span>
</div>
</div>
<div class="itemB">
<span class="name">nameB1</span>
<div class="topics">
<span class="topic">topicB1</span>
<span class="topic">topicB2</span>
<span class="topic">topicB3</span>
</div>
</div>
<div class="itemC">
<span class="name">nameC1</span>
<div class="topics">
<span class="topic">topicC1</span>
<span class="topic">topicC2</span>
<span class="topic">topicC3</span>
</div>
</div>
</div>
I want to use protractor query to generate the object like:
[
{
name:'nameA1',
topics:['topicA1','topicA2','topicA3']
},
{
name:'nameB1',
topics:['topicB1','topicB2','topicB3']
},
{
name:'nameC1',
topics:['topicC1','topicC2','topicC3']
}
]
my solution looks really ugly containing a lot of nested "then" functions, could anybody provide a better way to do this. Thanks
Check out http://github.github.com/github-flavored-markdown/ to learn about using markdown in github comments. I've updated your question so it's readable.
You might want to look into element.all().map() - https://github.com/angular/protractor/blob/master/lib/protractor.js#L230
Because everything is a promise when querying the DOM with WebDriver, you can easily end up with massive amounts of nested then as everytime you would find an element, getting the element's text or html would add one more nesting level.
To stay at only 1 level, you can use things like cheerio or jsdom (npm modules) which will allow you to query a subset of the DOM jQuery-style. However you won't be able to "click" them for example, but for your current case, this would be a fitting solution.
Closing stale issues that seem resolved - please open a new issue if a problem is persisting.