Module type Webdriver.S
type json= Yojson.Safe.ttype 'a ioThe client I/O monad, within which communication happens with the WebDriver server.
Commands
type sessionA connection to the WebDriver server. A session statefully holds the active windows, tabs, cookies, and other state required by the browser.
module Infix : sig ... endSince the
~sessionparameter is generally constant, this module provides a reader monad to sequence multiple commands within the same session. You can either pass explicitly the~sessionargument or open this module.
module Error : sig ... endAll potentital errors raised by the WebDriver protocol.
exceptionWebdriver of Error.tEvery command that fails raises this exception, which contains some hint as to what went wrong.
Sessions
In order to create a session, a connection to a WebDriver-compatible browser must be established:
- The
hostis a url where the WebDriver server can be accessed, for example"http://localhost:4444". - The
capabilitiesdescribe the requested browser settings.
module Capabilities : sig ... endThe requested capabilities when creating a new session.
module Session : sig ... endFor creating and deleting sessions manually.
val run : host:string -> Capabilities.t -> 'a cmd -> 'a iorun ~host capabilities cmdis a helper function to create a new session onhostwith the requiredcapabilities, execute thecmdwithin that session, and finally ensure that the session is deleted on termination ofcmd(from its natural death or an exception.)
module Timeouts : sig ... endConfigure the timeouts for page loads, script execution and the implicit wait when searching for elements on a page.
module Wait : sig ... endEven though the browser attempts to complete most operations before giving back control, some commands might trigger too soon and raise an error. The recommended strategy is to sleep and retry the operation repeatedly until it succeeds.
Navigation
val goto : string -> unit cmdgoto urlask the browser to visit the page aturl.
val current_url : string cmdThe current url.
val back : unit cmdClick the browser
backbutton, to reload the previous url in history.
val forward : unit cmdClick the
forwardbutton, to move forward to the next url in history.
val refresh : unit cmdRefresh the current url.
module Cookie : sig ... endCookies management.
HTML Elements
type using=[|`cssCSS selectors, like
"h1 span"|`link_textThe exact text in an
<a>...</a>|`partial_link_textThe partial text present in a link
|`tag_namethe HTML tag name, like
"h1"or"div"|`xpathXPath query
]A strategy to find an element on a page.
val find_first : ?from:elt -> using -> string -> elt cmdfind_first `using "query"returns the first element that matches thequery(interpreted withusing). The element is searched inside the current frame of the current window, and if a?fromparent element is provided, the search takes place inside it.raise (Webdriver { error = `no_such_element ; _ })otherwise.let* elt = find_first `css "h1 a" in ... let* elt = find_first `xpath "//h1//a" in ...
val find_all : ?from:elt -> using -> string -> elt list cmdfind_all `using "query"behaves likefind_first, but returns a list of all elements matching thequery.
Inspecting HTML elements
val attribute : elt -> string -> string cmdattribute e attrreturns the value of the HTML attributeattrof the elemente.
val property : elt -> string -> string option cmdproperty e propreturnsSomevalue of the DOM propertypropof the elemente, orNoneif undefined.
val is_selected : elt -> bool cmdThe boolean status of a checkbox, a radio or an option in a select.
val css : elt -> string -> string cmdcss e propreturns the computed value of the css propertypropfor the elemente.
val aria_role : elt -> string cmdaria_role ereturns the accessibility role of the elemente. See ARIA Roles on MDN
val aria_label : elt -> json cmdaria_role ereturns the accessibility label of the elemente. See ARIA Labels on MDN
Interactions
val send_keys : elt -> string -> unit cmdsend_keys e strsends the stringstrto an input element, as if typed from a keyboard. For special keys likeenterorbackspace, use the predefined values in the moduleKey:send_keys my_input ("hello" ^ Key.enter)
module Key : sig ... endSpecial keys on a keyboard.
val perform : action list -> unit cmdperform actionsexecutes the sequence of actions for each input source. The actions are synchronized vertically, such that:perform [ mouse [ `down button0 ; `pause ; `up button0 ] ; keyboard [ `down "a" ; `up "a" ; `pause ] ]- The mouse starts a left click and the keyboard presses "a" simultaneously;
- Then the keyboard releases the "a" key;
- And finally the mouse releases the left click.
The
`pauseaction does nothing and is used for synchronization.The pressed keys and buttons stay pressed at the end of the interaction, unless explicitly released.
val release : unit cmdreleaseany pending keys or button from previous interactions.
Timing actions
val none : ?name:string -> pause list -> actionAn inoperative device, that can be used to time the duration of each vertical frame.
val sleep : int -> unit cmdsleep durationwaits fordurationin milliseconds before continuing.
Keyboard actions
type key=[|pause|`down of Key.tpress down the key
|`up of Key.trelease a pressed key
]A typing interaction from a keyboard.
val typing : string -> key listtyping keysis a helper function to produce an alternating sequence of`down keyand`up keyto simulate the typing ofkeys:typing "ab" = [`down "a" ; `up "a" ; `down "b" ; `up "b"]The modifier
Key.alt,Key.control,Key.metaandKey.shiftwill be pressed differently to trigger the desired shortcut:typing (Key.control ^ "a") (* CTRL-A *) = [`down Key.control ; `down "a" ; `up "a" ; `up Key.control]
Pointer actions
type moveA pointer movement to a new location, taking some
durationof time (in milliseconds). The defaultdurationis a teleportation in0ms.
val absolute : ?duration:int -> (int * int) -> moveabsolute (x, y)moves the pointer to the position(x, y)measured from the top left of the document.
val relative : ?duration:int -> (int * int) -> moverelative (dx, dy)moves the pointer by(dx, dy)from its current location.
val center : ?duration:int -> ?offset:(int * int) -> elt -> movecenter ~offset:(dx, dy) eltmoves the pointer to the center of the elementeltoffsetted byoffset. The defaultoffsetis(0, 0).
val button_left : buttonThe left mouse button (at position
0).
val button_middle : buttonThe middle mouse button (at position
1).
val button_right : buttonThe right mouse button (at position
2).
type pointer=[|pause|`cancelcancel the pointer current action
|`down of buttonpress down the button
|`up of buttonrelease a pressed button
|`move of movemove the pointer
]An action from a mouse/touch/pen device
val mouse : ?name:string -> pointer list -> actionmouse actionsdescribes the movement, click, etc of a mouse pointer.
Scroll wheel actions
val scroll_absolute : ?duration:int -> ?x:int -> ?y:int -> unit -> scrollscroll_absolute ~x ~y ()resets the scrollbar such that the positionx, yfalls into view, as measured from the top of the page. The default value ofxandyis 0.
Document
val title : string cmdThe page
titleof the current document.
val source : string cmdThe HTML
sourcecode of the current document.
val print : string cmdThe current page, printed as a PDF.
val screenshot : ?elt:elt -> unit -> string cmdReturns a PNG screenshot of the current page, or of the provided
?elt.
val switch_to_frame : [ `top | `id of int | `elt of elt ] -> unit cmdFocus the selected frame inside the current document.
- The
`topframe is the current document root. - The
`id nframe is thenth frame in the page. - The
`elt eframe is the frame associated with the HTML elemente.
- The
val switch_to_parent_frame : unit cmdFocus the parent of the currently selected frame.
module Window : sig ... endWindows and tabs management.
module Alert : sig ... endPopup management: alert, confirm and prompt
Javascript execution
val execute : string -> json cmdexcute "js"runs thejson the current page, returning its result injson.
val execute_async : string -> json cmdexcute_async "js"runs thejsasynchronously on the current page.This function terminates when the javascript callback
arguments[0]is called, and returns its parameter as json. This can be used to block until some component has initialized:let* _ = execute_async {| var k = arguments[0]; something.onload(k); |} in (* blocks until onload triggers k *)