Scenario: Say I'm using Codecept to acceptance test a search form, like Google. On the results page, there's a set of <li> elements, each containing a search result. Within each <li>, say there is an h2 and a h3.
<li>
<h2>Result 1</h2>
<h3>Description 1</h3>
</li>
<li>
<h2>Result 2</h2>
<h3>Description 2</h3>
</li>
I want to grab these results, perhaps as an array of objects, so I can easily check each result against what I was expecting in that position. Is there a pattern I can use to do this? Currently, I see only 2 options, both bad:
1) Run grab on h2s and h3s, so I get array of n of each ([R1, R2] and [D1, D2]), which I can then zip together to get an element for each result ([R1, D1], [R2, D2]). However, this breaks if some results are missing h3s ([R1,D2], [R2, null]).
2) Grab the HTML for each li, and then parse that string to extract the relevant values. This is just painful, given that codeceptjs already has nice ways of extracting the text or other attributes from a given element.
There is another option, you can use see helper, with strict locator.
For example:
I.see('RESULT_TITLE', {css: '[data-href='RESULT_LINK']'});
Did anyone resolve the issue?
I have error for both cases:
var allResults = $$('span.ng-binding').map(function (el){...}
var allResults = element.all(by.xpath('//span[@class="ng-binding"]')).map(function (el) {...}
@neil-s
I can suggest this option:
Create custom method in pageobject:
grabResults: function* () {
let liAmount = yield I.grabNumberOfVisibleElements("//li")
let returnment = []
for (let i = 0; i < liAmount; i++) {
let result = yield I.grabTextFrom(`//li[${i+1}]//h2`)
let description = yield I.grabTextFrom(`//li[${i+1}]h3`)
returnment.push([result, description])
}
return returnment
},
and use this method from your tests
```
Scenario.only('test', function* (I, mainPage) {
let result = yield* mainPage.grabResults()
console.log(result)
})