master
Steve Ayerhart 2022-08-01 17:02:56 -04:00
parent 84b402b246
commit 3ba96d3f44
No known key found for this signature in database
GPG Key ID: 4CB33EB9BB156C97
1 changed files with 77 additions and 45 deletions

View File

@ -1,11 +1,10 @@
USING: kernel http http.client http.server io.sockets io.streams.byte-array io.pathnames io.encodings.binary io.encodings.string io.encodings.utf8 io.launcher accessors urls parser effects.parser words arrays sequences quotations json.reader json.writer formatting assocs namespaces present hashtables words.symbol combinators lexer calendar prettyprint.backend base64 images.png destructors math.parser threads ;
USING: kernel http http.client http.server io.sockets io.streams.byte-array io.pathnames io.encodings.binary io.encodings.string io.encodings.utf8 io.launcher accessors urls parser effects.parser words arrays sequences quotations json.reader json.writer formatting assocs namespaces present hashtables words.symbol combinators lexer calendar prettyprint.backend base64 images.png destructors math.parser threads continuations ;
USING: libc classes ;
USING: prettyprint ;
IN: web-driver
SYMBOL: current-web-driver-session
! https://www.w3.org/TR/webdriver/#elements
CONSTANT: web-element-identifier "element-6066-11e4-a52e-4f735466cecf"
@ -15,25 +14,22 @@ CONSTANT: partial-link-text-location-strategy "partial link text"
CONSTANT: tag-name-location-strategy "tag name"
CONSTANT: xpath-location-strategy "xpath"
TUPLE: web-driver-session remote-url web-driver-process session-id capabilities ;
SYMBOLS: current-web-driver current-web-driver-session ;
TUPLE: web-driver remote-url process ;
TUPLE: web-driver-session session-id capabilities ;
TUPLE: session-status message ready? ;
: current-web-driver-session> ( -- session )
current-web-driver-session get ;
: >current-web-driver-session ( session -- )
current-web-driver-session set ;
: current-session-id ( -- session-id )
: current-session-id> ( -- session-id )
current-web-driver-session> session-id>> ;
: current-remote-url ( -- remote-url )
current-web-driver-session> remote-url>> ;
: current-session-relative-url ( -- url )
"session" current-session-id append-path >url ;
"session" current-session-id> append-path >url ;
: current-remote-url ( -- url )
current-web-driver get remote-url>> ;
: current-session-url ( -- url )
current-remote-url current-session-relative-url derive-url ;
: <web-driver-url> ( path -- url )
[ current-remote-url ] dip >url derive-url ;
@ -92,20 +88,6 @@ TUPLE: capabilities { always-match hashtable } { first-match hashtable } ;
{ "moz:firefoxOptions" { "args" { "-headless" } } }
} ;
! Sessions
! https://www.w3.org/TR/webdriver/#sessions
: status ( -- session-status )
"status" <web-driver-get-request> http-web-driver-request
[ "message" of ] [ "ready" of ] bi session-status boa ;
: new-session ( capabilities -- json )
"session" <web-driver-post-request> http-web-driver-request
dup "sessionId" of current-session-id set ;
: delete-session ( -- )
current-session-url <web-driver-delete-request> http-web-driver-request drop ;
! Timeouts
! https://www.w3.org/TR/webdriver/#timeouts
@ -328,6 +310,23 @@ TUPLE: rect x y width height ;
! TODO: handle driver processes better
! Sessions
! https://www.w3.org/TR/webdriver/#sessions
: status ( -- session-status )
[
"status" <web-driver-get-request> http-web-driver-request
[ "message" of dup "" = [ drop f ] when ] [ "ready" of ] bi session-status boa
]
[ \ libc-error instance? ] ignore-error/f f session-status boa ;
: <web-driver-session> ( capabilities -- json )
"session" <web-driver-post-request> http-web-driver-request
[ "sessionId" of ] [ "capabilities" of ] bi web-driver-session boa ;
: delete-session ( -- )
current-session-url <web-driver-delete-request> http-web-driver-request drop ;
: get-free-listen-port ( -- port )
<any-port-local-inet4> utf8 <server>
[ addr>> port>> ] [ dispose ] bi number>string ;
@ -335,25 +334,58 @@ TUPLE: rect x y width height ;
: <web-driver-process> ( host port command -- process )
-rot '{ _ "--host" _ "--port" _ } utf8 <process-reader> ;
: <gecko-driver-process> ( host port -- process )
"geckodriver" <web-driver-process> ;
: <chrome-driver-process> ( host port -- process )
"chromedriver" <web-driver-process> ;
: init-driver-session ( -- )
"127.0.0.1" get-free-listen-port
: <gecko-driver> ( host port -- gecko-driver )
[ ":" prepend append "http://" prepend "/" append >url ]
[ <gecko-driver-process> ] 2bi
f f web-driver-session boa
>current-web-driver-session ;
[ "geckodriver" <web-driver-process> ] 2bi
web-driver boa ;
: <web-driver-session> ( capabilities -- )
init-driver-session
: with-gecko-driver ( host port quote -- )
[ [ ready?>> ] [ 1 seconds sleep status ] do until ] prepose
[ <gecko-driver> current-web-driver ] dip with-variable ; inline
[ ready?>> ] [ 1 seconds sleep status ] do until
: with-default-gecko-driver ( quote -- )
[ "127.0.0.1" get-free-listen-port ] dip with-gecko-driver ; inline
new-session [ "sessionId" of ] [ "capabilities" of ] bi
: <chrome-driver> ( host port -- process )
[ ":" prepend append "http://" prepend "/" append >url ]
[ "chromedriver" <web-driver-process> ] 2bi
web-driver boa ;
current-web-driver-session> swap >>capabilities swap >>session-id
>current-web-driver-session ;
: with-chrome-driver ( host port quote -- )
[ <chrome-driver> current-web-driver ] dip with-variable ; inline
: with-default-chrome-driver ( quote -- )
[ "127.0.0.1" get-free-listen-port ] dip with-chrome-driver ; inline
! : with-default-gecko-session ( caps quote -- )
! [
! 2drop
! [ ready?>> ] [ 1 seconds sleep status ] do until
! [ <web-driver-session> current-web-driver-session ] dip with-variable
! delete-session
! ] with-default-gecko-driver ;
! : <web-driver-session> ( host port capabilities -- session )
! [
! [ <gecko-driver-process> ]
! [ ":" prepend append "http://" prepend "/" append ] 2bi
! ] dip
!
! [
! [ ready?>> ] [ 1 seconds sleep status ] do until
! ] dip
!
! 2dup new-session new-session [ "sessionId" of ] [ "capabilities" of ] bi
!
! : init-driver-session ( -- )
! f f web-driver-session boa
! : <web-driver-session> ( capabilities -- session )
! init-driver-session
!
! [ ready?>> ] [ 1 seconds sleep status ] do until
!
! new-session [ "sessionId" of ] [ "capabilities" of ] bi
!
! current-web-driver-session> swap >>capabilities swap >>session-id
! >current-web-driver-session ;