Skip to content

Instantly share code, notes, and snippets.

@NOTtheMessiah
Created August 31, 2016 07:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NOTtheMessiah/b2d5f1128d07237446591c817bb1651e to your computer and use it in GitHub Desktop.
Save NOTtheMessiah/b2d5f1128d07237446591c817bb1651e to your computer and use it in GitHub Desktop.
Proof-of-Concept of WebDriver client in Julia, run with interactive tag :> julia -i webdriver.jl
using Requests
using JSON
using PyCall
# Global configuration
myHost = "localhost"
myPort = 4444
myPathPrefix = "/wd/hub"
# Types
type WebDriver
sessionId :: AbstractString
browserName :: AbstractString
response
end
type WebDriverException
msg
screen
stacktrace
end
type WebElement
id
sessionId :: AbstractString
end
# Convenience functions
makeRequest(req, url) = req(URIParser.URI("$(myPathRoot)$url"))
makeRequest(req, url, arg) = req(URIParser.URI("$(myPathRoot)$url"),arg)
parseResponse(response) = JSON.parse(ASCIIString(response.data))
maybeget(a, k, e) = try a[k] catch; e end
desired(browser) = Dict{UTF8String,Any}(
"desiredCapabilities" => Dict{UTF8String,Any}(
"javascriptEnabled" => true,
"chromeOptions" => Dict{UTF8String,Any}(
"args" => Any[],
"extensions" => Any[]
),
"platform" => "ANY",
"browserName" => browser,
"version" => ""
))
# Session Control
function startSession(browserName::AbstractString)
response = makeRequest(post,"/session",desired("chrome") |> json)
sessionId = parseResponse(response)["sessionId"]
ASCIIString(response.data) |> println
WebDriver(sessionId,browserName,response)
end
function deleteSession(wd::WebDriver)
response = makeRequest(Requests.delete,"/session/$(wd.sessionId)")
ASCIIString(response.data) |> println
response
end
#Actions
function go(wd::WebDriver,url) # Renamed from get to go for now
response = makeRequest(post,"/session/$(wd.sessionId)/url","""{"url":"$url"}""")
if response.status == 500
parseResponse(response)["value"]["message"] |> println
error("You dun goofed, boy")
else
ASCIIString(response.data) |> println
response
end
end
function getURL(wd::WebDriver)
response = makeRequest(get,"/session/$(wd.sessionId)/url")
ASCIIString(response.data) |> println
response
end
function find_element_by_id(wd::WebDriver,s)
body = Dict("using"=>"css selector", "value"=>"""[id="$s"]""") |> JSON.json
response = makeRequest(post,"/session/$(wd.sessionId)/element", body)
if response.status == 200
parseResponse(response)["value"]["ELEMENT"]
else
response
end
end
function find_element_by_name(wd::WebDriver,name)
body = Dict("using"=>"name", "value"=>name) |> JSON.json
response = makeRequest(post,"/session/$(wd.sessionId)/element", body)
if response.status == 200
WebElement(parseResponse(response)["value"]["ELEMENT"],wd.sessionId)
else
response
end
end
function send_keys_to_element(we::WebElement,keys)
body = Dict("value"=>keys) |> JSON.json
makeRequest(post,"/session/$(we.sessionId)/element/$(we.id)/value",body)
end
# Hadn't yet figured how to start a session, so letting the python client do the work
@pyimport selenium.webdriver as pywebdriver
pywd = pywebdriver.Chrome()
myPathPrefix = ""
myPort = pywd[:command_executor][:_conn][:port]
myHost = pywd[:command_executor][:_conn][:host]
myPathRoot = "http://$myHost:$myPort$(myPathPrefix)"
# wd = startSession("chrome")
wd = WebDriver(pywd[:session_id],"chrome",Union{})
#Test script
function exampleSearch()
go(wd,"http://yahoo.com")
elem = find_element_by_name(wd,"p")
parseResponse(makeRequest(get,"/session/$(wd.sessionId)/title"))["value"] |> println
send_keys_to_element(elem,["seleniumhq","\ue006"])
sleep(5)
#deleteSession(wd)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment