How can we get all element values, when more than element hold same class name.

Eg: Consider I'm having n number of elements that having same class name as follows

<span class="country-name">Country 1</span>
<span class="country-name">Country 2</span>
<span class="country-name">Country 3</span>
<span class="country-name">Country 4</span>
<span class="country-name">Country 5</span>

How can I get all the element values that having the class name as country_name.

Also I have tried as follows:

span(:country, :class => 'country-name')
puts country

When I execute it, It only printing the first value (Country 1) other values are not printed. How can I get all values?

Any suggestions?

有帮助吗?

解决方案

You can create a collection accessor that returns all of the related spans (ie those with class "country-name").

In the page object, instead of calling span, call the pluralized version - spans:

class MyPage
    include PageObject

    spans(:country, :class => 'country-name')
end

This will create a country_elements method that return returns an array of all matching spans. You can iterate over this array to get the text of each country (element):

page = MyPage.new(browser)
page.country_elements.each{ |c| puts c.text }
#=> "Country 1"
#=> "Country 2"
#=> "Country 3"
#=> "Country 4"
#=> "Country 5"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top