Skip to content

Commit f5aad68

Browse files
authored
Added Ferrum::Browser#version which calls the Browser.getVersion command. (#293)
* Also added `Ferrum::Browser::VersionInfo` to wrap around the returned JSON hash. * https://chromedevtools.github.io/devtools-protocol/1-3/Browser/#method-getVersion
1 parent d6e8c22 commit f5aad68

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

lib/ferrum/browser.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require "ferrum/browser/process"
1010
require "ferrum/browser/client"
1111
require "ferrum/browser/binary"
12+
require "ferrum/browser/version_info"
1213

1314
module Ferrum
1415
class Browser
@@ -150,6 +151,10 @@ def crash
150151
command("Browser.crash")
151152
end
152153

154+
def version
155+
VersionInfo.new(command("Browser.getVersion"))
156+
end
157+
153158
private
154159

155160
def start

lib/ferrum/browser/version_info.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Ferrum
2+
class Browser
3+
class VersionInfo
4+
5+
def initialize(properties)
6+
@properties = properties
7+
end
8+
9+
def protocol_version
10+
@properties['protocolVersion']
11+
end
12+
13+
def product
14+
@properties['product']
15+
end
16+
17+
def revision
18+
@properties['revision']
19+
end
20+
21+
def user_agent
22+
@properties['userAgent']
23+
end
24+
25+
def js_version
26+
@properties['jsVersion']
27+
end
28+
29+
end
30+
end
31+
end

spec/browser/version_info_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'spec_helper'
2+
require 'ferrum/browser/version_info'
3+
4+
describe Ferrum::Browser::VersionInfo do
5+
let(:protocol_version) { "1.3" }
6+
let(:product) { "HeadlessChrome/106.0.5249.91" }
7+
let(:revision) { "@fa96d5f07b1177d1bf5009f647a5b8c629762157" }
8+
let(:user_agent) { "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/106.0.5249.91 Safari/537.36" }
9+
let(:js_version) { "10.6.194.17" }
10+
let(:properties) do
11+
{
12+
"protocolVersion" => protocol_version,
13+
"product" => product,
14+
"revision" => revision,
15+
"userAgent" => user_agent,
16+
"jsVersion" => js_version
17+
}
18+
end
19+
20+
subject { described_class.new(properties) }
21+
22+
describe "#protocol_version" do
23+
it "must return the 'protocolVersion' property" do
24+
expect(subject.protocol_version).to eq(properties['protocolVersion'])
25+
end
26+
end
27+
28+
describe "#product" do
29+
it "must return the 'product' property" do
30+
expect(subject.product).to eq(properties['product'])
31+
end
32+
end
33+
34+
describe "#revision" do
35+
it "must return the 'revision' property" do
36+
expect(subject.revision).to eq(properties['revision'])
37+
end
38+
end
39+
40+
describe "#user_agent" do
41+
it "must return the 'userAgent' property" do
42+
expect(subject.user_agent).to eq(properties['userAgent'])
43+
end
44+
end
45+
46+
describe "#js_version" do
47+
it "must return the 'jsVersion' property" do
48+
expect(subject.js_version).to eq(properties['jsVersion'])
49+
end
50+
end
51+
end

spec/browser_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ module Ferrum
7474
expect(browser.body).to include("Hello world")
7575
end
7676

77+
it "must return allow requesting the browser version information" do
78+
version_info = browser.version
79+
80+
expect(version_info).to be_kind_of(Ferrum::Browser::VersionInfo)
81+
expect(version_info.protocol_version).to_not be(nil)
82+
expect(version_info.protocol_version).to_not be_empty
83+
expect(version_info.product).to_not be(nil)
84+
expect(version_info.product).to_not be_empty
85+
expect(version_info.revision).to_not be(nil)
86+
expect(version_info.revision).to_not be_empty
87+
expect(version_info.user_agent).to_not be(nil)
88+
expect(version_info.user_agent).to_not be_empty
89+
expect(version_info.js_version).to_not be(nil)
90+
expect(version_info.js_version).to_not be_empty
91+
end
92+
7793
it "stops silently before goto call" do
7894
browser = Browser.new
7995
expect { browser.quit }.not_to raise_error

0 commit comments

Comments
 (0)