Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
Fixes some Crystal compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Aug 31, 2017
1 parent ed5e93d commit e5710b5
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.crystal
.shards
libs
/.crystal
/.shards
/lib
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

Selenium Webdriver bindings for the Crystal programming language.

WARNING: this implements the now obsolete
[JsonWireProtocol](https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol)
the [W3C Webdriver spec](https://w3c.github.io/webdriver/webdriver-spec.html).
Your selenium and browser mileages may not be supported until the new protocol
is implemented.

Latest known working combination is Selenium 2.48.2 and Firefox 34. A
combination known to NOT WORK is Selenium 3.5.3, Firefox 55 and the now required
Geckodriver 0.18.0.


## Usage

Expand Down
2 changes: 1 addition & 1 deletion shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ version: 1.0
shards:
minitest:
github: ysbaddaden/minitest.cr
version: 0.3.4
version: 0.3.5

2 changes: 1 addition & 1 deletion src/webdriver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Selenium

case response.status_code
when 200
JSON.parse(response.body).raw as Hash
JSON.parse(response.body).raw.as(Hash)
else
failure(response)
end
Expand Down
2 changes: 1 addition & 1 deletion src/webdriver/alert.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Selenium
class Alert
private getter :session
private getter session : Session

def initialize(@session)
end
Expand Down
14 changes: 7 additions & 7 deletions src/webdriver/session.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ module Selenium
delete
end

def timeouts=(script : Int = nil, implicit : Int = nil, page_load : Int = nil)
body = {} of Symbol => String | Nil
body[:script] = script if script
body[:implicit] = implicit if implicit
body[:page_load] = page_load if page_load
def timeouts(script : Int? = nil, implicit : Int? = nil, page_load : Int? = nil)
body = {} of Symbol => Int32?
body[:script] = script.to_i if script
body[:implicit] = implicit.to_i if implicit
body[:page_load] = page_load.to_i if page_load
post("/timeouts", body)
end

Expand Down Expand Up @@ -97,7 +97,7 @@ module Selenium
# post("/screenshot")
#end

def find_element(by, selector, parent : WebElement = nil)
def find_element(by, selector, parent : WebElement? = nil)
url = parent ? "/element/#{ parent.id }/element" : "/element"
value = post(url, {
using: WebElement.locator_for(by),
Expand All @@ -106,7 +106,7 @@ module Selenium
WebElement.new(self, value.as(Hash))
end

def find_elements(by, selector, parent : WebElement = nil)
def find_elements(by, selector, parent : WebElement? = nil)
url = parent ? "/element/#{ parent.id }/elements" : "/elements"
value = post(url, {
using: WebElement.locator_for(by),
Expand Down
2 changes: 1 addition & 1 deletion src/webdriver/web_element.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module Selenium
# Numpad 9 U+E023
#}

getter id
getter id : String
private getter session : Session

def initialize(@session, item)
Expand Down
19 changes: 13 additions & 6 deletions test/session_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@ class SessionTest < Minitest::Test
session.url = "https://crystal-lang.org"
assert_equal "https://crystal-lang.org/", session.url

nav = session.find_element(:css, "nav .menu")
nav = session.find_element(:css, "nav[role=navigation]")

links = nav.find_elements(:tag_name, "a")
assert_equal ["GITHUB", "DOCS", "API"], links.map(&.text)
links = nav.find_elements(:tag_name, "a").map(&.text)
assert_includes links, "Blog"
assert_includes links, "GitHub"
assert_includes links, "Docs"
assert_includes links, "API"

session.execute <<-JAVASCRIPT
Array.from(document.querySelectorAll("nav .menu a"))
.forEach(function (a) { a.removeAttribute("target"); })
JAVASCRIPT

nav.find_element(:css, "a[title='API']").click
assert_match "https://crystal-lang.org/api/", session.url
assert_raises(Selenium::NoSuchElement) do
nav.find_element(:css, "a[href='/some/unknown/link']")
end

nav.find_element(:css, "a[href='/blog/']").click
assert_match "https://crystal-lang.org/blog/", session.url

session.back
assert_equal "https://crystal-lang.org/", session.url

session.forward
assert_match "https://crystal-lang.org/api/", session.url
assert_match "https://crystal-lang.org/blog/", session.url
end

def test_input
Expand Down

0 comments on commit e5710b5

Please sign in to comment.