Module: Selenium::WebDriver::DriverExtensions::FullScreenshot

Defined in:
lib/selenium-webdriver-full-screenshot/extentions/full_screenshot.rb

Instance Method Summary collapse

Instance Method Details

#full_screenshot(filename) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/selenium-webdriver-full-screenshot/extentions/full_screenshot.rb', line 5

def full_screenshot(filename)
  imgs = []
  (page_height.to_f / widnow_inner_height).ceil.times do |cnt|
    scroll_to(0, widnow_inner_height * cnt)
    sleep 1
    scroll_height = widnow_inner_height * cnt
    crop_height = scroll_height + widnow_inner_height - page_height
    crop_height = crop_height ? crop_height : 0
    # puts "#{scroll_height}, #{crop_height}"
    if crop_height < widnow_inner_height
      imgs << {
        img: ChunkyPNG::Image.from_blob(@bridge.screenshot.unpack('m')[0]),
        crop_height: crop_height,
        window_height: widnow_inner_height,
        window_width: widnow_inner_width
      }
    end
  end
  full_screenshot_png(imgs, filename)
end

#full_screenshot_png(imgs, filename) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/selenium-webdriver-full-screenshot/extentions/full_screenshot.rb', line 26

def full_screenshot_png(imgs, filename)
  png_height = imgs.map{|img| img[:img].height}.inject { |sum, i| sum + i }
  compose_height = 0
  png_width = imgs.first[:img].width
  window_width = imgs.first[:window_width]
  retina = png_width > window_width ? 2 : 1
  last_img = imgs.pop
  last_crop_pos_y = last_img[:crop_height] * retina
  png_height -= last_crop_pos_y
  # binding.pry
  imgs << {
    img: last_img[:img].crop(0, last_crop_pos_y, last_img[:img].width, last_img[:img].height - last_crop_pos_y)
  }
  png = ChunkyPNG::Image.new(png_width, png_height, ChunkyPNG::Color::TRANSPARENT)
  # binding.pry
  imgs.each_with_index do |img, idx|
    png.compose!(img[:img], 0, compose_height)
    compose_height += img[:img].height
  end
  puts "full_screenshot_png: #{filename}"
  png.save("#{filename}.png")
end

#page_heightObject



59
60
61
62
# File 'lib/selenium-webdriver-full-screenshot/extentions/full_screenshot.rb', line 59

def page_height
  js = 'return document.body.clientHeight'
  bridge.execute_script(js)
end

#page_widthObject



64
65
66
67
# File 'lib/selenium-webdriver-full-screenshot/extentions/full_screenshot.rb', line 64

def page_width
  js = 'return document.body.clientWidth'
  bridge.execute_script(js)
end

#scroll_to(x, y) ⇒ Object



69
70
71
# File 'lib/selenium-webdriver-full-screenshot/extentions/full_screenshot.rb', line 69

def scroll_to(x, y)
  bridge.execute_script("window.scrollTo(#{x}, #{y})")
end

#widnow_inner_heightObject



49
50
51
52
# File 'lib/selenium-webdriver-full-screenshot/extentions/full_screenshot.rb', line 49

def widnow_inner_height
  js = 'return window.innerHeight'
  bridge.execute_script(js)
end

#widnow_inner_widthObject



54
55
56
57
# File 'lib/selenium-webdriver-full-screenshot/extentions/full_screenshot.rb', line 54

def widnow_inner_width
  js = 'return window.innerWidth'
  bridge.execute_script(js)
end