Skip to content

Commit

Permalink
refactor(tests): reorganize specs and unify naming
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Oct 10, 2022
1 parent 4c4f1bc commit 927e5f4
Show file tree
Hide file tree
Showing 23 changed files with 1,459 additions and 1,351 deletions.
8 changes: 5 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AllCops:
TargetRubyVersion: 2.6
NewCops: enable
SuggestExtensions: false

Layout/FirstArrayElementIndentation:
EnforcedStyle: consistent
Expand Down Expand Up @@ -38,6 +39,8 @@ Metrics/AbcSize:

Metrics/ClassLength:
Max: 258
Exclude:
- spec/**/*

Metrics/CyclomaticComplexity:
Max: 14
Expand All @@ -53,6 +56,5 @@ Metrics/ModuleLength:
Metrics/PerceivedComplexity:
Max: 14

#require:
# - rubocop-rake
# - rubocop-rspec
require:
- rubocop-rake
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ source "https://rubygems.org"
gem "byebug", "~> 11.0", platforms: %i[mri mingw x64_mingw]
gem "rubocop", "~> 1.22"
gem "rubocop-rake", require: false
gem "rubocop-rspec", require: false

gem "kramdown", "~> 2.0", require: false
gem "redcarpet", require: false, platform: :mri
Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ task default: :test
begin
require "yard"
YARD::Rake::YardocTask.new
task docs: :yard
rescue LoadError
# nop
end
33 changes: 22 additions & 11 deletions lib/ferrum/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Cookies
class Cookie
# The parsed JSON attributes.
#
# @return [Hash{String => String}]
# @return [Hash{String => [String, Boolean, nil]}]
attr_reader :attributes

#
Expand All @@ -27,7 +27,7 @@ def initialize(attributes)
# @return [String]
#
def name
@attributes["name"]
attributes["name"]
end

#
Expand All @@ -36,7 +36,7 @@ def name
# @return [String]
#
def value
@attributes["value"]
attributes["value"]
end

#
Expand All @@ -45,7 +45,7 @@ def value
# @return [String]
#
def domain
@attributes["domain"]
attributes["domain"]
end

#
Expand All @@ -54,7 +54,7 @@ def domain
# @return [String]
#
def path
@attributes["path"]
attributes["path"]
end

#
Expand All @@ -63,16 +63,17 @@ def path
# @return ["Strict", "Lax", "None", nil]
#
def samesite
@attributes["sameSite"]
attributes["sameSite"]
end
alias same_site samesite

#
# The cookie's size.
#
# @return [Integer]
#
def size
@attributes["size"]
attributes["size"]
end

#
Expand All @@ -81,7 +82,7 @@ def size
# @return [Boolean]
#
def secure?
@attributes["secure"]
attributes["secure"]
end

#
Expand All @@ -90,16 +91,17 @@ def secure?
# @return [Boolean]
#
def httponly?
@attributes["httpOnly"]
attributes["httpOnly"]
end
alias http_only? httponly?

#
# Specifies whether the cookie is a session cookie or not.
#
# @return [Boolean]
#
def session?
@attributes["session"]
attributes["session"]
end

#
Expand All @@ -108,7 +110,16 @@ def session?
# @return [Time, nil]
#
def expires
Time.at(@attributes["expires"]) if @attributes["expires"].positive?
Time.at(attributes["expires"]) if attributes["expires"].positive?
end

#
# Compares different cookie objects.
#
# @return [Time, nil]
#
def ==(other)
other.class == self.class && other.attributes == attributes
end
end

Expand Down
12 changes: 7 additions & 5 deletions spec/browser/binary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Browser
ENV["PATHEXT"] = @original_env_pathext
end

context "#find" do
describe "#find" do
it "finds one binary" do
expect(Binary.find("bin1")).to eq(bin1)
end
Expand Down Expand Up @@ -64,7 +64,7 @@ class Browser
end
end

context "#all" do
describe "#all" do
it "finds one binary" do
expect(Binary.all("bin1")).to eq([bin1])
end
Expand Down Expand Up @@ -92,10 +92,12 @@ class Browser
end
end

it "works lazily" do
enum = Binary.lazy_find(%w[ls which none])
describe "#lazy_find" do
it "works lazily" do
enum = Binary.lazy_find(%w[ls which none])

expect(enum.instance_of?(Enumerator::Lazy)).to be_truthy
expect(enum.instance_of?(Enumerator::Lazy)).to be_truthy
end
end
end
end
Expand Down
68 changes: 68 additions & 0 deletions spec/browser/xvfb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

module Ferrum
class Browser
describe Xvfb, skip: !Binary.find("Xvfb") do
let(:process) { xvfb_browser.process }
let(:xvfb_browser) { Browser.new(default_options.merge(options)) }
let(:default_options) { Hash(headless: true, xvfb: true) }

context "headless" do
context "with window_size" do
let(:options) { Hash(window_size: [1400, 1400]) }

it "allows to run tests configured to xvfb" do
xvfb_browser.go_to(base_url)

expect(xvfb_browser.body).to include("Hello world!")
expect(process_alive?(process.xvfb.pid)).to be(true)
expect(process.xvfb.screen_size).to eq("1400x1400x24")
expect(process.xvfb.to_env).to eq("DISPLAY" => ":#{process.xvfb.display_id}")
ensure
xvfb_browser&.quit
expect(process_alive?(process.xvfb.pid)).to be(false)
end
end

context "without window_size" do
let(:options) { {} }

it "allows to run tests configured to xvfb" do
xvfb_browser.go_to(base_url)

expect(xvfb_browser.body).to include("Hello world!")
expect(process_alive?(process.xvfb.pid)).to be(true)
expect(process.xvfb.screen_size).to eq("1024x768x24")
expect(process.xvfb.to_env).to eq("DISPLAY" => ":#{process.xvfb.display_id}")
ensure
xvfb_browser&.quit
expect(process_alive?(process.xvfb.pid)).to be(false)
end
end
end

context "headful" do
let(:options) { Hash(headless: false) }

it "allows to run tests configured to xvfb" do
xvfb_browser.go_to(base_url)

expect(xvfb_browser.body).to include("Hello world!")
expect(process_alive?(process.xvfb.pid)).to be(true)
expect(process.xvfb.screen_size).to eq("1024x768x24")
ensure
xvfb_browser&.quit
expect(process_alive?(process.xvfb.pid)).to be(false)
end
end

def process_alive?(pid)
return false unless pid

::Process.kill(0, pid) == 1
rescue Errno::ESRCH
false
end
end
end
end
32 changes: 32 additions & 0 deletions spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,38 @@ module Ferrum
ensure
browser&.quit
end

context ":save_path argument" do
let(:filename) { "attachment.pdf" }
let(:browser) do
Ferrum::Browser.new(
base_url: Ferrum::Server.server.base_url,
save_path: save_path
)
end

context "with absolute path" do
let(:save_path) { "/tmp/ferrum" }

it "saves an attachment" do
browser.go_to("/#{filename}")

expect(File.exist?("#{save_path}/#{filename}")).to be true
ensure
FileUtils.rm_rf(save_path)
end
end

context "with local path" do
let(:save_path) { "spec/tmp" }

it "raises an error" do
expect do
browser.go_to("/#{filename}")
end.to raise_error(Ferrum::Error, "supply absolute path for `:save_path` option")
end
end
end
end

describe "#crash" do
Expand Down
Loading

0 comments on commit 927e5f4

Please sign in to comment.