example_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. require 'tmpdir'
  4. RSpec.describe 'example' do
  5. def with_network_retry(max_retry: 2, timeout: 4, &block)
  6. if max_retry <= 0
  7. Timeout.timeout(timeout, &block)
  8. else
  9. begin
  10. Timeout.timeout(timeout, &block)
  11. rescue Timeout::Error
  12. puts "Retry with { remaining: #{max_retry - 1}, timeout: #{timeout * 1.5} }"
  13. with_network_retry(max_retry: max_retry - 1, timeout: timeout * 1.5, &block)
  14. end
  15. end
  16. end
  17. it 'should take a screenshot' do
  18. skip if Puppeteer.env.ci? && !Puppeteer.env.windows?
  19. with_network_retry { page.goto('https://github.com/YusukeIwaki') }
  20. tmpdir = Dir.mktmpdir
  21. begin
  22. path = File.join(tmpdir, 'YusukeIwaki.png')
  23. page.screenshot(path: path)
  24. expect(File.open(path, 'rb').read.size).to be > 1000
  25. ensure
  26. FileUtils.remove_entry(tmpdir, true)
  27. end
  28. end
  29. it 'should print PDF with options' do
  30. skip if Puppeteer.env.ci? && !Puppeteer.env.windows?
  31. page.viewport = Puppeteer::Viewport.new(width: 1200, height: 800, device_scale_factor: 2)
  32. with_network_retry { page.goto("https://github.com/YusukeIwaki") }
  33. page.wait_for_selector(".js-yearly-contributions")
  34. overlay = page.query_selector('.js-yearly-contributions')
  35. js = <<-JAVASCRIPT
  36. graph => {
  37. const width = getComputedStyle(graph).width;
  38. graph = graph.cloneNode(true);
  39. graph.style.width = width;
  40. document.body.innerHTML = `
  41. <div style="display:flex;justify-content:center;align-items:center;height:100vh;">;
  42. ${graph.outerHTML}
  43. </div>
  44. `;
  45. }
  46. JAVASCRIPT
  47. page.evaluate(js, overlay)
  48. page.pdf(
  49. path: '5.element-to-pdf.github.pdf',
  50. print_background: true,
  51. format: "letter",
  52. margin: { top: "1cm", left: "2cm", right: "3cm", bottom: "4cm" },
  53. omit_background: true,
  54. )
  55. end
  56. it 'should input text and grab DOM elements' do
  57. skip if Puppeteer.env.ci? && !Puppeteer.env.windows?
  58. page.viewport = Puppeteer::Viewport.new(width: 1280, height: 800)
  59. with_network_retry { page.goto("https://github.com/", wait_until: 'domcontentloaded') }
  60. form = page.query_selector("form.js-site-search-form")
  61. search_input = form.query_selector("input.header-search-input")
  62. search_input.click
  63. page.keyboard.type_text("puppeteer")
  64. page.wait_for_navigation do
  65. search_input.press("Enter")
  66. end
  67. list = page.query_selector("ul.repo-list")
  68. items = list.query_selector_all("div.f4")
  69. items.each do |item|
  70. title = item.eval_on_selector("a", "a => a.innerText")
  71. puts("==> #{title}")
  72. end
  73. end
  74. it 'should evaluate expression' do
  75. expect(page.evaluate('2 + 3')).to eq(5)
  76. end
  77. it 'should evaluate function returning object' do
  78. expect(page.evaluate('() => { return { a: 3, b: 4 } }')).to eq({ 'a' => 3, 'b' => 4 })
  79. end
  80. it 'should work with waitForFunction with timeout: 0' do
  81. page.content = '<body>Now Loading...</body>'
  82. page.wait_for_function('() => document.querySelector("body").innerText.includes("Something")', timeout: 0) do
  83. page.evaluate(<<~JAVASCRIPT)
  84. () => {
  85. setTimeout(() => {
  86. document.body.innerHTML = "<h1>It works!</h1><p>Something</p>"
  87. }, 500)
  88. }
  89. JAVASCRIPT
  90. end
  91. end
  92. end