factor-work/web-driver/web-driver.factor

408 lines
13 KiB
Factor
Raw Normal View History

2024-03-25 22:21:10 -04:00
USING: kernel accessors urls parser effects.parser words arrays sequences quotations json formatting assocs namespaces present hashtables words.symbol combinators lexer calendar prettyprint.backend base64 images.png destructors math.parser threads continuations ;
USING: http http.client http.server ;
2022-08-01 17:02:56 -04:00
USING: libc classes ;
2024-03-25 22:21:10 -04:00
USING: io io.sockets io.streams.byte-array io.pathnames io.encodings.binary io.encodings.string io.encodings.utf8 io.launcher ;
2022-02-27 17:49:07 -05:00
2021-08-21 23:37:51 -04:00
USING: prettyprint ;
IN: web-driver
2022-02-14 10:23:28 -05:00
! https://www.w3.org/TR/webdriver/#elements
2021-08-21 23:37:51 -04:00
CONSTANT: web-element-identifier "element-6066-11e4-a52e-4f735466cecf"
CONSTANT: css-location-strategy "css selector"
CONSTANT: link-text-location-strategy "link text"
CONSTANT: partial-link-text-location-strategy "partial link text"
CONSTANT: tag-name-location-strategy "tag name"
CONSTANT: xpath-location-strategy "xpath"
2022-08-01 17:02:56 -04:00
SYMBOLS: current-web-driver current-web-driver-session ;
2024-03-25 22:21:10 -04:00
TUPLE: web-driver process remote-url ;
2022-08-01 17:02:56 -04:00
TUPLE: web-driver-session session-id capabilities ;
2022-02-14 10:23:28 -05:00
TUPLE: session-status message ready? ;
2021-08-21 23:37:51 -04:00
2022-02-23 12:30:24 -05:00
: current-web-driver-session> ( -- session )
current-web-driver-session get ;
2022-08-01 17:02:56 -04:00
: current-session-id> ( -- session-id )
2022-02-23 12:30:24 -05:00
current-web-driver-session> session-id>> ;
2021-08-21 23:37:51 -04:00
: current-session-relative-url ( -- url )
2022-08-01 17:02:56 -04:00
"session" current-session-id> append-path >url ;
: current-remote-url ( -- url )
current-web-driver get remote-url>> ;
2021-08-21 23:37:51 -04:00
: current-session-url ( -- url )
2022-02-23 12:30:24 -05:00
current-remote-url current-session-relative-url derive-url ;
2021-08-21 23:37:51 -04:00
: <web-driver-url> ( path -- url )
2022-02-23 12:30:24 -05:00
[ current-remote-url ] dip >url derive-url ;
2021-08-21 23:37:51 -04:00
: <web-driver-session-url> ( path -- url )
[ current-session-url present ] dip append-path >url ;
: <web-driver-element-url> ( path id -- url )
swap append-path
[ "element" <web-driver-session-url> present ] dip append-path >url ;
2022-02-14 10:23:28 -05:00
: <web-driver-post-data> ( hashtable -- post-data )
"application/json" <post-data> swap
[ >json utf8 encode ] [ B{ 123 125 } ] if* >>data ;
2021-08-21 23:37:51 -04:00
: <web-driver-get-request> ( path -- request )
<web-driver-url> <get-request> ;
: <web-driver-post-request> ( data path -- request )
2022-02-14 10:23:28 -05:00
[ <web-driver-post-data> ] dip <web-driver-url> <post-request> ;
2021-08-21 23:37:51 -04:00
: <web-driver-delete-request> ( path -- request )
<web-driver-url> <delete-request> ;
2022-02-27 17:49:07 -05:00
: <session-post-request> ( data path -- request )
[ <web-driver-post-data> ] dip <web-driver-session-url> <post-request> ;
2021-08-21 23:37:51 -04:00
2022-02-27 17:49:07 -05:00
: <session-get-request> ( path -- request )
<web-driver-session-url> <get-request> ;
2021-08-21 23:37:51 -04:00
2022-02-27 17:49:07 -05:00
: <session-delete-request> ( path -- request )
<web-driver-session-url> <delete-request> ;
2021-08-21 23:37:51 -04:00
2022-02-27 17:49:07 -05:00
: <element-get-request> ( element path -- request )
swap id>> <web-driver-element-url> <get-request> ;
2021-08-21 23:37:51 -04:00
2022-02-27 17:49:07 -05:00
: <element-post-request> ( element data path -- request )
swap [ swap id>> <web-driver-element-url> ] dip <web-driver-post-data> swap <post-request> ;
2021-08-21 23:37:51 -04:00
2022-02-27 17:49:07 -05:00
: http-web-driver-request ( request -- data )
http-request nip json> "value" of ;
2021-08-21 23:37:51 -04:00
2022-02-14 10:23:28 -05:00
! Capabilities
! https://www.w3.org/webdriver/#capabilities
2024-03-25 22:21:10 -04:00
TUPLE: capabilities { always-match hashtable } { first-match sequence } ;
2022-02-14 10:23:28 -05:00
: <capabilities> ( always-match first-match -- capabilities )
2024-03-25 22:21:10 -04:00
[ [ H{ } ] unless* ]
[ [ { } ] unless* ] bi*
capabilities boa ;
: <no-capabilities> ( -- capabilities )
f f <capabilities> ;
2022-02-14 10:23:28 -05:00
: capabilities>json ( capabilities -- string )
[ always-match>> ] [ first-match>> ] bi
'H{ { "alwaysMatch" _ } { "firstMatch" _ } } 'H{ { "capabilities" _ } } ;
2022-02-23 12:30:24 -05:00
: <headless-firefox-capabilities> ( -- hash )
H{
2024-03-25 22:21:10 -04:00
{ "moz:firefoxOptions" H{ { "args" { "-headless" } } } }
} f <capabilities> capabilities>json ;
2022-02-23 12:30:24 -05:00
2022-02-14 10:23:28 -05:00
! Timeouts
! https://www.w3.org/TR/webdriver/#timeouts
TUPLE: timeouts
{ script initial: 30000 }
{ page-load initial: 300000 }
{ implicit initial: 0 } ;
: get-timeouts ( -- timeouts )
"timeouts" <session-get-request> http-web-driver-request
[ "script" of ] [ "pageload" of ] [ "implicit" of ] tri
timeouts boa ;
: set-timeouts ( timeouts -- )
[ script>> ] [ page-load>> ] [ implicit>> ] tri
'H{ { "script" _ } { "pageLoad" _ } { "implicit" _ } }
"timeouts" <session-post-request> http-web-driver-request drop ;
! Navigation
! https://www.w3.org/TR/webdriver/#navigation
2021-08-21 23:37:51 -04:00
: get-current-url ( -- url )
"url" <session-get-request> http-web-driver-request >url ;
: navigate-to ( url -- )
2022-02-27 17:49:07 -05:00
present 'H{ { "url" _ } } "url" <session-post-request> http-web-driver-request drop ;
2021-08-21 23:37:51 -04:00
: back ( -- )
f "back" <session-post-request> http-web-driver-request drop ;
: refresh ( -- )
f "refresh" <session-post-request> http-web-driver-request drop ;
: title ( -- title )
"title" <session-get-request> http-web-driver-request ;
! elements
TUPLE: web-element id ;
C: <web-element> web-element
: <locator> ( using value -- hashtable )
2022-02-14 10:23:28 -05:00
'H{ { "value" _ } { "using" _ } } ;
: <css-locator> ( value -- hashtable )
css-location-strategy <locator> ;
: <link-text-locator> ( value -- hashtable )
link-text-location-strategy <locator> ;
: <partial-link-text-locator> ( value -- hashtable )
partial-link-text-location-strategy <locator> ;
: <tag-name-locator> ( value -- hashtable )
tag-name-location-strategy <locator> ;
: <xpath-locator> ( value -- hashtable )
xpath-location-strategy <locator> ;
: find-element ( locator -- element )
2021-08-21 23:37:51 -04:00
"element" <session-post-request> http-web-driver-request
web-element-identifier of <web-element> ;
: find-elements ( locator -- element )
2021-08-21 23:37:51 -04:00
"elements" <session-post-request> http-web-driver-request
[ web-element-identifier of <web-element> ] map ;
! TODO: finish the element by definitions
! clean up a little
<<
SYNTAX: DEFINE-LOCATORS:
scan-token
{
[
[ "find-element" prepend-path create-word-in ]
[ "-locator" append "<" ">" surround parse-word 1quotation [ find-element ] compose ( value -- element ) ] bi
define-declared
]
[
[ "find-elements" prepend-path create-word-in ]
[ "-locator" append "<" ">" surround parse-word 1quotation [ find-elements ] compose ( value -- seq ) ] bi
define-declared
]
} cleave ;
>>
DEFINE-LOCATORS: css
DEFINE-LOCATORS: link-text
DEFINE-LOCATORS: partial-link-text
DEFINE-LOCATORS: tag-name
DEFINE-LOCATORS: xpath
! some helper css locators
: maybe-prepend ( string char -- string' )
2dup [ first ] bi@ = [ drop ] [ prepend ] if ;
2021-08-21 23:37:51 -04:00
: find-element-by-id ( id -- element )
"#" maybe-prepend find-element/css ;
: find-elements-by-id ( id -- element )
"#" maybe-prepend find-elements/css ;
2021-08-21 23:37:51 -04:00
: find-element-by-class-name ( name -- element )
"." maybe-prepend find-element/css ;
: find-elements-by-class-name ( name -- element )
"." maybe-prepend find-elements/css ;
2021-08-21 23:37:51 -04:00
: active-element ( -- element )
"element/active" <session-get-request> http-web-driver-request ;
! element state
: is-element-selected? ( element -- ? )
"selected" <element-get-request> http-web-driver-request ;
: get-element-attribute ( element name -- attribute )
"attribute" prepend-path <element-get-request> http-web-driver-request ;
: get-element-property ( element name -- property )
"property" prepend-path <element-get-request> http-web-driver-request ;
: get-element-css-value ( element property-name -- css-value )
"css" prepend-path <element-get-request> http-web-driver-request ;
: get-element-text ( element -- text )
"text" <element-get-request> http-web-driver-request ;
: is-element-enabled? ( element -- ? )
"enabled" <element-get-request> http-web-driver-request ;
TUPLE: rect x y width height ;
: get-element-rect ( element -- rect )
"rect" <element-get-request> http-web-driver-request
{ [ "x" of ] [ "y" of ] [ "width" of ] [ "height" of ] } cleave rect boa ;
! element interaction
: element-click ( element -- )
f "click" <element-post-request> http-web-driver-request drop ;
: element-clear ( element -- )
f "clear" <element-post-request> http-web-driver-request drop ;
: element-send-keys ( element value -- )
2022-02-14 10:23:28 -05:00
'H{ { "text" _ } } "value" <element-post-request> http-web-driver-request drop ;
! document handling
: get-page-source ( -- source )
"source" <session-get-request> http-web-driver-request ;
: execute-script ( script arguments -- return )
2022-02-14 10:23:28 -05:00
'H{ { "script" _ } { "args" _ } } "execute/sync" <session-post-request> http-web-driver-request ;
: execute-async-script ( script arguments -- return )
2022-02-14 10:23:28 -05:00
'H{ { "script" _ } { "args" _ } } "execute/async" <session-post-request> http-web-driver-request ;
2024-03-25 22:21:10 -04:00
! cookies
2024-03-25 22:21:10 -04:00
TUPLE: cookie name value path domain secure? http-only? expiry same-site ;
2024-03-25 22:21:10 -04:00
: parse-cookie ( hashtable -- cookie )
2021-08-23 22:44:45 -04:00
{
2024-03-25 22:21:10 -04:00
[ "name" of ]
[ "value" of ]
[ "path" of [ "/" ] unless* ]
[ "domain" of ]
[ "secure" of ]
[ "httpOnly" of ]
[ "expiry" of [ unix-time>timestamp ] [ f ] if* ]
[ "sameSite" of ]
2021-08-23 22:44:45 -04:00
} cleave cookie boa ;
: cookie>web-driver-cookie ( cookie -- hashtable )
tuple>assoc >hashtable [
2024-03-25 22:21:10 -04:00
{
[ "secure" "secure?" rot rename-at ]
[ "httpOnly" "http-only?" rot rename-at ]
[ "sameSite" "same-site" rot rename-at ]
} cleave
] keep 'H{ { "cookie" _ } } ;
2021-08-23 22:44:45 -04:00
: get-all-cookies ( -- cookies )
"cookie" <session-get-request> http-web-driver-request
2024-03-25 22:21:10 -04:00
[ parse-cookie ] map ;
2021-08-23 22:44:45 -04:00
: get-named-cookie ( name -- value )
2024-03-25 22:21:10 -04:00
"cookie" prepend-path <session-get-request> http-web-driver-request parse-cookie ;
2021-08-23 22:44:45 -04:00
: add-cookie ( cookie -- )
cookie>web-driver-cookie "cookie" <session-post-request> http-web-driver-request drop ;
2024-03-25 22:21:10 -04:00
: add-cookies ( seq -- )
[ add-cookie ] each ;
2021-08-23 22:44:45 -04:00
: delete-cookie ( name -- )
"cookie" prepend-path <session-delete-request> http-web-driver-request drop ;
: delete-all-cookies ( -- )
"cookie" <session-delete-request> http-web-driver-request drop ;
2022-02-14 10:23:28 -05:00
! User prompts
! https://www.w3.org/TR/webdriver/#user-prompts
: dismiss-alert ( -- )
f "alert/dismiss" <session-post-request> http-web-driver-request drop ;
: accept-alert ( -- )
f "alert/accept" <session-post-request> http-web-driver-request drop ;
2022-02-14 16:41:03 -05:00
: get-alert-text ( -- text )
"alert/text" <session-get-request> http-web-driver-request ;
2022-02-14 10:23:28 -05:00
: send-alert-text ( text -- )
2022-02-14 16:41:03 -05:00
'H{ { "text" _ } } "alert/text" <session-post-request> http-web-driver-request drop ;
2022-02-14 10:23:28 -05:00
! Screen capture
! https://www.w3.org/TR/webdriver/#screen-capture
2021-08-23 23:53:29 -04:00
: take-screenshot ( -- loading-png )
"screenshot" <session-get-request> http-web-driver-request
base64> binary <byte-reader> load-png ;
: take-element-screenshot ( element -- loading-png )
"screenshot" <element-get-request> http-web-driver-request base64> binary <byte-reader> load-png ;
2022-02-23 12:30:24 -05:00
! TODO: Actions
2022-08-01 17:02:56 -04:00
! Sessions
! https://www.w3.org/TR/webdriver/#sessions
: status ( -- session-status )
2024-03-25 22:21:10 -04:00
! [
2022-08-01 17:02:56 -04:00
"status" <web-driver-get-request> http-web-driver-request
2024-03-25 22:21:10 -04:00
[ "message" of dup "" = [ drop f ] when ] [ "ready" of ] bi session-status boa ;
! ]
! [ \ libc-error instance? ] ignore-error/f "SUP" session-status boa ;
2022-08-01 17:02:56 -04:00
: <web-driver-session> ( capabilities -- json )
2024-03-25 22:21:10 -04:00
dup .
2022-08-01 17:02:56 -04:00
"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 ;
2022-02-23 12:30:24 -05:00
: get-free-listen-port ( -- port )
<any-port-local-inet4> utf8 <server>
[ addr>> port>> ] [ dispose ] bi number>string ;
2024-03-25 22:21:10 -04:00
: build-url ( host port -- url )
":" prepend append "http://" prepend "/" append >url ;
2021-08-21 23:37:51 -04:00
2024-03-25 22:21:10 -04:00
! TODO: handle missing binary
2022-02-27 17:49:07 -05:00
2024-03-25 22:21:10 -04:00
: <gecko-driver-command-desc> ( -- desc )
{ "geckodriver" "--log" "fatal" } ;
2022-02-27 17:49:07 -05:00
2024-03-25 22:21:10 -04:00
: <chrome-driver-command-desc> ( -- desc )
{ "chromedriver" "--silent" } ;
: <web-driver-process> ( desc host port -- process )
"--port=" prepend
'{ "--host" _ _ } append utf8 <process-reader> ;
: <web-driver> ( command host port -- process )
[ <web-driver-process> ] 2keep build-url web-driver boa ;
2022-02-27 17:49:07 -05:00
2024-03-25 22:21:10 -04:00
: default-host/port ( -- host port )
"localhost" get-free-listen-port ;
2022-08-01 17:02:56 -04:00
2024-03-25 22:21:10 -04:00
: <gecko-driver> ( host port -- web-driver )
[ <gecko-driver-command-desc> ] 2dip <web-driver> ;
: <default-gecko-driver> ( -- web-driver )
default-host/port <gecko-driver> ;
: <chrome-driver> ( host port -- web-driver )
[ <chrome-driver-command-desc> ] 2dip <web-driver> ;
: <default-chrome-driver> ( -- web-driver )
default-host/port <chrome-driver> ;
: with-web-driver ( driver quote -- )
current-web-driver swap with-variable ; inline
: with-default-gecko-driver ( quote -- )
<default-gecko-driver> swap with-web-driver ; inline
2022-08-01 17:02:56 -04:00
: with-default-chrome-driver ( quote -- )
2024-03-25 22:21:10 -04:00
<default-chrome-driver> swap with-web-driver ; inline
: with-web-driver-session ( capabilities driver quote -- )
[
dup current-web-driver [
[ ready?>> ] [ 1 seconds sleep status ] do until
swap <web-driver-session>
'H{ { current-web-driver _ } { current-web-driver-session _ } }
] with-variable
] dip
with-variables ; inline
: with-default-gecko-driver-session ( capabilities quote -- )
<default-gecko-driver> swap with-web-driver-session ; inline
: with-default-chrome-driver-session ( capabilities quote -- )
<default-chrome-driver> swap with-web-driver-session ; inline