Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sgur
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgur/079083dc5369fa4aa2f3 to your computer and use it in GitHub Desktop.
Save sgur/079083dc5369fa4aa2f3 to your computer and use it in GitHub Desktop.
webapi を使って WebDriver を叩くサンプル
" Naviaget webdriver via vimscript
" https://code.google.com/p/selenium/wiki/JsonWireProtocol
scriptencoding utf-8
let s:webdriver_path = expand('chromedriver.exe', 1)
function! s:webdriver_start(port)
" execute printf('!start %s --port=%d', s:webdriver_path, a:port)
return vimproc#popen3(printf('%s --port=%d', tr(s:webdriver_path, '\', '/'), a:port))
endfunction
function! s:webdriver_status(port)
let response = webapi#http#get(printf('http://localhost:%d/status', a:port))
echo 'STATUS:' eval(response.content)
endfunction
function! s:webdriver_create_session(port)
let response = webapi#http#post(printf('http://localhost:%d/session', a:port)
\ , webapi#json#encode({'desiredCapabilities': {}}))
let result = webapi#json#decode(response.content)
if result.status == 0
return result.sessionId
else
return ''
endif
endfunction
function! s:webdriver_sessions(port)
let response = webapi#http#get(printf('http://localhost:%d/sessions', a:port))
if empty(response.content)
return []
endif
return map(webapi#json#decode(response.content).value, 'v:val.sessionId')
endfunction
function! s:webdriver_show_url(port, sessionId, url)
let response = webapi#http#post(
\ printf('http://localhost:%d/session/%s/url', a:port, a:sessionId)
\ , webapi#json#encode({'url': a:url}))
echo 'URL:' webapi#json#decode(response.content)
endfunction
function! s:webdriver_delete(port, sessionId)
let response = webapi#http#post(
\ printf('http://localhost:%d/session/%s', a:port, a:sessionId)
\ , {}, {}, 'DELETE')
echo 'DELETE:' webapi#json#decode(response.content)
endfunction
let s:proc = s:webdriver_start(9513)
try
let s:sessionId = s:webdriver_create_session(9513)
call s:webdriver_show_url(9513, s:sessionId, 'https://news.google.com/')
" call s:webdriver_status(9515)
finally
for s:id in s:webdriver_sessions(9513)
call s:webdriver_delete(9513, s:id)
endfor
unlet s:id
echo 'finalize'
call s:proc.kill(9)
endtry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment