rspec-html

0.3.5
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

#children

Use #children to access immediate child elements of the current element. Return an empty array if no children present.

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

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

Pass text: true to also include dangling text (default: false):

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

it 'retrieves all matching elements' do
  expect(document.div.children(text: true).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>text<span>span #1</span><span>span #2</span>
    </div>
  </body></html>
textspan #1span #2

#[]

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