query_selector_spec.rb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. require 'spec_helper'
  2. RSpec.describe 'querySelector' do
  3. describe 'Page#eval_on_selector' do
  4. it 'should work' do
  5. page.content = '<section id="testAttribute">43543</section>'
  6. id = page.eval_on_selector('section', '(e) => e.id')
  7. expect(id).to eq('testAttribute')
  8. end
  9. it 'should accept arguments' do
  10. page.content = '<section>hello</section>'
  11. text = page.eval_on_selector(
  12. 'section',
  13. '(e, suffix) => e.textContent + suffix',
  14. ' world!',
  15. )
  16. expect(text).to eq('hello world!')
  17. end
  18. it 'should accept ElementHandles as arguments' do
  19. page.content = '<section>hello</section><div> world</div>'
  20. div_handle = page.query_selector('div')
  21. text = page.eval_on_selector(
  22. 'section',
  23. '(e, div) => e.textContent + div.textContent',
  24. div_handle,
  25. )
  26. expect(text).to eq('hello world')
  27. end
  28. it 'should throw error if no element is found' do
  29. expect {
  30. page.eval_on_selector('section', '(e) => e.id')
  31. }.to raise_error(/failed to find element matching selector "section"/)
  32. end
  33. end
  34. describe 'Page#eval_on_selector_all' do
  35. it 'should work' do
  36. page.content = '<div>hello</div><div>beautiful</div><div>world!</div>'
  37. divs_count = page.eval_on_selector_all('div', '(divs) => divs.length')
  38. expect(divs_count).to eq(3)
  39. end
  40. end
  41. describe 'Page#query_selector' do
  42. it 'should query existing element' do
  43. page.content = '<section>test</section>'
  44. element = page.query_selector('section')
  45. expect(element).not_to be_nil
  46. end
  47. it 'should return null for non-existing element' do
  48. page.content = '<section>test</section>'
  49. element = page.query_selector('non-existing-element')
  50. expect(element).to be_nil
  51. end
  52. end
  53. describe 'Page#query_selector_all' do
  54. it 'should query existing elements' do
  55. page.content = '<div>A</div><br/><div>B</div>'
  56. elements = page.query_selector_all('div')
  57. expect(elements).to be_a(Enumerable)
  58. expect(elements.size).to eq(2)
  59. texts = elements.map { |el| page.evaluate('(e) => e.textContent', el) }
  60. expect(texts).to eq(%w[A B])
  61. end
  62. it 'should return empty array if nothing is found' do
  63. page.content = '<span>A</span><br/><span>B</span>'
  64. elements = page.query_selector_all('div')
  65. expect(elements).to be_a(Enumerable)
  66. expect(elements).to be_empty
  67. end
  68. end
  69. describe 'Page#$x' do
  70. it 'should query existing element' do
  71. page.content = '<section>test</section>'
  72. elements = page.Sx('//section')
  73. expect(elements).to be_a(Enumerable)
  74. expect(elements.size).to eq(1)
  75. end
  76. it 'should return empty array for non-existing element' do
  77. page.content = '<section>test</section>'
  78. elements = page.Sx('//div')
  79. expect(elements).to be_a(Enumerable)
  80. expect(elements).to be_empty
  81. end
  82. it 'should return multiple elements' do
  83. page.content = '<div></div><div></div>'
  84. elements = page.Sx('//div')
  85. expect(elements).to be_a(Enumerable)
  86. expect(elements.size).to eq(2)
  87. end
  88. end
  89. describe 'ElementHandle#query_selector' do
  90. it 'should query existing element' do
  91. page.content = '<html><body><div class="second"><div class="inner">A</div></div></body></html>'
  92. html = page.query_selector('html')
  93. second = html.query_selector('.second')
  94. inner = second.query_selector('.inner')
  95. content = page.evaluate('(e) => e.textContent', inner)
  96. expect(content).to eq('A')
  97. end
  98. it 'should return null for non-existing element' do
  99. page.content = '<html><body><div class="second"><div class="inner">B</div></div></body></html>'
  100. html = page.query_selector('html')
  101. second = html.query_selector('.third')
  102. expect(second).to be_nil
  103. end
  104. end
  105. describe 'ElementHandle#eval_on_selector' do
  106. it 'should work' do
  107. page.content = '<html><body><div class="tweet"><div class="like">100</div><div class="retweets">10</div></div></body></html>'
  108. tweet = page.query_selector('.tweet')
  109. content = tweet.eval_on_selector('.like', '(node) => node.innerText')
  110. expect(content).to eq('100')
  111. end
  112. it 'should retrieve content from subtree' do
  113. html_content = '<div class="a">not-a-child-div</div><div id="myId"><div class="a">a-child-div</div></div>'
  114. page.content = html_content
  115. element_handle = page.query_selector('#myId')
  116. content = element_handle.eval_on_selector('.a', '(node) => node.innerText')
  117. expect(content).to eq('a-child-div')
  118. end
  119. it 'should throw in case of missing selector' do
  120. html_content = '<div class="a">not-a-child-div</div><div id="myId"></div>'
  121. page.content = html_content
  122. element_handle = page.query_selector('#myId')
  123. expect {
  124. element_handle.eval_on_selector('.a', '(node) => node.innerText')
  125. }.to raise_error(/failed to find element matching selector ".a"/)
  126. end
  127. end
  128. describe 'ElementHandle#eval_on_selector_all' do
  129. it 'should work' do
  130. page.content = '<html><body><div class="tweet"><div class="like">100</div><div class="like">10</div></div></body></html>'
  131. tweet = page.query_selector('.tweet')
  132. content = tweet.eval_on_selector_all(
  133. '.like',
  134. '(nodes) => nodes.map((n) => n.innerText)',
  135. )
  136. expect(content).to eq(['100', '10'])
  137. end
  138. it 'should retrieve content from subtree' do
  139. html_content = '<div class="a">not-a-child-div</div><div id="myId"><div class="a">a1-child-div</div><div class="a">a2-child-div</div></div>'
  140. page.content = html_content
  141. element_handle = page.query_selector('#myId')
  142. content = element_handle.eval_on_selector_all(
  143. '.a',
  144. '(nodes) => nodes.map((n) => n.innerText)',
  145. )
  146. expect(content).to eq(['a1-child-div', 'a2-child-div'])
  147. end
  148. it 'should not throw in case of missing selector' do
  149. html_content = '<div class="a">not-a-child-div</div><div id="myId"></div>'
  150. page.content = html_content
  151. element_handle = page.query_selector('#myId')
  152. nodes_length = element_handle.eval_on_selector_all(
  153. '.a',
  154. '(nodes) => nodes.length',
  155. )
  156. expect(nodes_length).to eq(0)
  157. end
  158. end
  159. describe 'ElementHandle#query_selector_all' do
  160. it 'should query existing elements' do
  161. page.content = '<html><body><div>A</div><br/><div>B</div></body></html>'
  162. html = page.query_selector('html')
  163. elements = html.query_selector_all('div')
  164. expect(elements).to be_a(Enumerable)
  165. expect(elements.length).to eq(2)
  166. expect(elements.map { |el| page.evaluate('(e) => e.textContent', el) }).to eq(%w[A B])
  167. end
  168. it 'should return empty array for non-existing elements' do
  169. page.content = '<html><body><span>A</span><br/><span>B</span></body></html>'
  170. html = page.query_selector('html')
  171. elements = html.query_selector_all('div')
  172. expect(elements).to be_a(Enumerable)
  173. expect(elements).to be_empty
  174. end
  175. end
  176. describe 'ElementHandle#Sx' do
  177. it 'should query existing element' do
  178. page.content = '<html><body><div class="second"><div class="inner">A</div></div></body></html>'
  179. html = page.query_selector('html')
  180. second = html.Sx("./body/div[contains(@class, 'second')]")
  181. inner = second[0].Sx("./div[contains(@class, 'inner')]")
  182. content = page.evaluate(
  183. '(e) => e.textContent',
  184. inner.first,
  185. )
  186. expect(content).to eq('A')
  187. end
  188. it 'should return null for non-existing element' do
  189. page.content = '<html><body><div class="second"><div class="inner">B</div></div></body></html>'
  190. html = page.query_selector('html')
  191. second = html.Sx("/div[contains(@class, 'third')]")
  192. expect(second).to be_a(Enumerable)
  193. expect(second).to be_empty
  194. end
  195. end
  196. end