rspec-html

0.3.4
Light Mode Dark Mode

Enumerating Elements

Several interfaces exist to assist you if you need to test specifics about the number of elements that match a given element specification.

#all

Use #all to retrieve all matched elements:

subject(:document) { parse_html('<div>div #1</div><div>div #2</div><div>div #3</div>') }

it 'retrieves all matching elements' do
  expect(document.div.all).to all(match_text /div #[0-9]/)
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>div #1</div>
    <div>div #2</div>
    <div>div #3</div>
  </body></html>
div #1
div #2
div #3

#[]

Use #[] to index a specific element from a matching set. Note that indexing starts at 1, not 0, so the first element in a set is [1].

subject(:document) { parse_html('<div>div #1</div><div>div #2</div><div>div #3</div>') }

it 'retrieves all matching elements' do
  expect(document.div[2]).to match_text 'div #2'
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>div #1</div>
    <div>div #2</div>
    <div>div #3</div>
  </body></html>
div #1
div #2
div #3

You can also use #first or #last if you prefer:

subject(:document) { parse_html('<div>div #1</div><div>div #2</div><div>div #3</div>') }

it 'retrieves all matching elements' do
  expect(document.div.last).to match_text 'div #3'
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>div #1</div>
    <div>div #2</div>
    <div>div #3</div>
  </body></html>
div #1
div #2
div #3

#size

Use #size to verify the length of a set of matched elements:

subject(:document) { parse_html('<div>div #1</div><div>div #2</div><div>div #3</div>') }

it 'retrieves all matching elements' do
  expect(document.div.size).to eql 3
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>div #1</div>
    <div>div #2</div>
    <div>div #3</div>
  </body></html>
div #1
div #2
div #3

Documentation generated by rspec-documentation