Skip to content

Commit 0a6700b

Browse files
committed
Fix screenshots.
1 parent 867531c commit 0a6700b

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

lib/async/webdriver/scope/screen_capture.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module ScreenCapture
1313
# Take a screenshot of the current page or element.
1414
# @returns [String] The screenshot as a Base64 encoded string.
1515
def screenshot
16-
reply = current_scope.post("screenshot")
16+
reply = current_scope.get("screenshot")
1717

1818
return Base64.decode64(reply)
1919
end

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Fix `Scope#screenshot` to use the correct HTTP method (`GET` instead of `POST`).
6+
37
## v0.8.0
48

59
- Fix `fill_in` `<select>` on Safari.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2023-2025, by Samuel Williams.
5+
6+
require "sus/fixtures/async/reactor_context"
7+
require "sus/fixtures/async/http/server_context"
8+
9+
require "async/webdriver"
10+
require "pool_context"
11+
12+
AScreenCapture = Sus::Shared("screen capture") do
13+
include Sus::Fixtures::Async::ReactorContext
14+
include Sus::Fixtures::Async::HTTP::ServerContext
15+
16+
let(:app) do
17+
proc do |request|
18+
Protocol::HTTP::Response[200, [], [<<~HTML]]
19+
<html>
20+
<head>
21+
<title>Test Page</title>
22+
<style>
23+
body {
24+
font-family: Arial, sans-serif;
25+
background-color: #f0f0f0;
26+
margin: 20px;
27+
}
28+
#main {
29+
background-color: white;
30+
padding: 20px;
31+
border-radius: 5px;
32+
width: 300px;
33+
height: 200px;
34+
}
35+
</style>
36+
</head>
37+
<body>
38+
<div id="main">
39+
<h1>Screenshot Test</h1>
40+
<p>This is a test page for taking screenshots.</p>
41+
<button id="test-button">Click Me</button>
42+
</div>
43+
</body>
44+
</html>
45+
HTML
46+
end
47+
end
48+
49+
with "#screenshot (session)" do
50+
it "should take a screenshot of the page" do
51+
session.visit(bound_url)
52+
53+
screenshot_data = session.screenshot
54+
55+
# Should return binary data (not base64 encoded)
56+
expect(screenshot_data).to be_a(String)
57+
expect(screenshot_data.length).to be > 0
58+
59+
# Check if it looks like PNG data (PNG files start with specific bytes)
60+
png_header = "\x89PNG\r\n\x1a\n".b
61+
expect(screenshot_data[0, 8]).to be == png_header
62+
end
63+
64+
it "should return different screenshots for different pages" do
65+
session.visit(bound_url)
66+
screenshot1 = session.screenshot
67+
68+
# Navigate to a different URL (about:blank)
69+
session.navigate_to("about:blank")
70+
screenshot2 = session.screenshot
71+
72+
# Screenshots should be different
73+
expect(screenshot1).not.to be == screenshot2
74+
end
75+
end
76+
77+
with "#screenshot (element)" do
78+
it "should take a screenshot of a specific element" do
79+
session.visit(bound_url)
80+
81+
element = session.find_element_by_css("#main")
82+
screenshot_data = element.screenshot
83+
84+
# Should return binary data (not base64 encoded)
85+
expect(screenshot_data).to be_a(String)
86+
expect(screenshot_data.length).to be > 0
87+
88+
# Check if it looks like PNG data
89+
png_header = "\x89PNG\r\n\x1a\n".b
90+
expect(screenshot_data[0, 8]).to be == png_header
91+
end
92+
93+
it "should return different sized screenshots for different elements" do
94+
session.visit(bound_url)
95+
96+
# Take screenshot of a large element
97+
main_element = session.find_element_by_css("#main")
98+
main_screenshot = main_element.screenshot
99+
100+
# Take screenshot of a smaller element
101+
button_element = session.find_element_by_css("#test-button")
102+
button_screenshot = button_element.screenshot
103+
104+
# Screenshots should be different
105+
expect(main_screenshot).not.to be == button_screenshot
106+
107+
# Both should be valid PNG data
108+
png_header = "\x89PNG\r\n\x1a\n".b
109+
expect(main_screenshot[0, 8]).to be == png_header
110+
expect(button_screenshot[0, 8]).to be == png_header
111+
end
112+
end
113+
end
114+
115+
Async::WebDriver::Bridge.each do |klass|
116+
name = klass.name.split("::").last
117+
pool = Async::WebDriver::Bridge::Pool.new(klass.new)
118+
119+
describe(klass, unique: name) do
120+
include PoolContext
121+
122+
it_behaves_like AScreenCapture
123+
end
124+
end

0 commit comments

Comments
 (0)