Skip to content

Commit

Permalink
Switch selenium libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew McGarvey authored and paulcsmith committed May 12, 2020
1 parent 26ebd2e commit 65e2c45
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 41 deletions.
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ license: MIT

dependencies:
selenium:
github: ysbaddaden/selenium-webdriver-crystal
commit: bda2fd406c1a118251c5a2883f1e1f3af242116e
github: matthewmcgarvey/selenium.cr
version: ~> 0.4.0
habitat:
github: luckyframework/habitat
version: ~> 0.4.0
8 changes: 4 additions & 4 deletions spec/lucky_flow_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ describe LuckyFlow do

it "can reset the session" do
flow = LuckyFlow.new
flow.session.cookies.set("hello", "world")
flow.session.cookies.get("hello").value.should eq "world"
flow.session.cookie_manager.add_cookie("hello", "world")
flow.session.cookie_manager.get_cookie("hello").value.should eq "world"

LuckyFlow.reset

expect_raises KeyError do
flow.session.cookies.get("hello").value
expect_raises Selenium::Error do
flow.session.cookie_manager.get_cookie("hello").value
end
end
end
Expand Down
17 changes: 8 additions & 9 deletions src/lucky_flow.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LuckyFlow
end

def visit(path : String)
session.url = "#{settings.base_uri}#{path}"
session.navigate_to("#{settings.base_uri}#{path}")
end

def visit(action : Lucky::Action.class, as user : User? = nil)
Expand All @@ -46,9 +46,9 @@ class LuckyFlow

def take_screenshot(filename : String = generate_screenshot_filename, fullsize : Bool = true)
if fullsize
with_fullsized_page { session.save_screenshot(filename) }
with_fullsized_page { session.screenshot(filename) }
else
session.save_screenshot(filename)
session.screenshot(filename)
end
end

Expand All @@ -57,18 +57,17 @@ class LuckyFlow
end

def expand_page_to_fullsize
width = session.execute("return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);").as_i
height = session.execute("return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);").as_i
window = session.window
window.resize_to(width + 100, height + 100)
width = session.document_manager.execute_script("return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);").to_i64
height = session.document_manager.execute_script("return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);").to_i64
session.window_manager.resize_window(width: width + 100, height: height + 100)
end

def with_fullsized_page(&block)
original_size = session.window.rect
original_size = session.window_manager.window_rect
expand_page_to_fullsize
yield
ensure
session.window.rect = original_size
session.window_manager.set_window_rect(original_size)
end

private def open_command(process) : String
Expand Down
4 changes: 2 additions & 2 deletions src/lucky_flow/element.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class LuckyFlow::Element
def initialize(@raw_selector : String, text @inner_text : String? = nil)
end

@_element : Selenium::WebElement?
@_element : Selenium::Element?

private def element : Selenium::WebElement
private def element : Selenium::Element
@_element ||= FindElement.run(session, selector, inner_text)
end

Expand Down
4 changes: 3 additions & 1 deletion src/lucky_flow/find_element.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class LuckyFlow::FindElement
LuckyFlow.settings
end

private def matching_elements : Array(Selenium::WebElement)
private def matching_elements : Array(Selenium::Element)
session.find_elements(:css, selector).select do |element|
text_to_check_for = inner_text
if text_to_check_for
Expand All @@ -55,6 +55,8 @@ class LuckyFlow::FindElement
true
end
end
rescue Selenium::Error
[] of Selenium::Element
end

private def raise_element_not_found_error
Expand Down
31 changes: 8 additions & 23 deletions src/lucky_flow/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,8 @@ class LuckyFlow::Server
# This is used so that only one server instance is started
INSTANCE = new

CAPABILITIES = {
browserName: "chrome",
platform: "ANY",
javascriptEnabled: true,
takesScreenshot: true,
handlesAlerts: true,
databaseEnabled: true,
locationContextEnabled: true,
applicationCacheEnabled: true,
browserConnectionEnabled: true,
cssSelectorsEnabled: true,
webStorageEnabled: true,
rotatable: true,
acceptSslCerts: true,
nativeEvents: true,
chromeOptions: {args: ["no-sandbox", "headless", "disable-gpu"]},
}
CAPABILITIES = Selenium::Chrome::Capabilities.new
CAPABILITIES.args(["no-sandbox", "headless", "disable-gpu"])

@retry_limit : Time?
@session : Selenium::Session?
Expand All @@ -44,15 +29,15 @@ class LuckyFlow::Server
# If less than 0.34.0
{% if compare_versions(Crystal::VERSION, "0.34.0") == -1 %}
private def start_session
driver = Selenium::Webdriver.new
Selenium::Session.new(driver, capabilities)
driver = Selenium::Driver.for(:chrome)
driver.create_session(CAPABILITIES)
rescue e : Errno
retry_start_session(e)
end
{% else %}
private def start_session
driver = Selenium::Webdriver.new
Selenium::Session.new(driver, capabilities)
driver = Selenium::Driver.for(:chrome)
driver.create_session(CAPABILITIES)
rescue e : IO::Error
retry_start_session(e)
end
Expand Down Expand Up @@ -98,11 +83,11 @@ class LuckyFlow::Server
end

def reset
@session.try &.cookies.clear
@session.try &.cookie_manager.delete_all_cookies
end

def shutdown
@session.try(&.stop)
@session.try &.delete
@chromedriver.try(&.stop)
end
end

0 comments on commit 65e2c45

Please # to comment.