WebDriver/src/WebDriver/WebDriver.class.st

190 lines
4.4 KiB
Smalltalk

Class {
#name : #WebDriver,
#superclass : #Object,
#instVars : [
'browser',
'server',
'port',
'sessionId',
'parameters',
'optionsName'
],
#category : #WebDriver
}
{ #category : #'instance creation' }
WebDriver class >> geckodriver [
| subproc |
subproc := OSSUnixSubprocess new
command: 'geckodriver'.
[ subproc run ] fork.
^ self new
browser: subproc
server: '127.0.0.1'
port: 4444
optionsName: #moz:firefoxOptions.
]
{ #category : #accessing }
WebDriver >> attribute: attr from: element [
^ (self send: { }
to: 'session/',sessionId,'/element/',element,'/attribute/',attr
using: #GET)
at: #value.
]
{ #category : #navigation }
WebDriver >> back [
self send: { } to: 'session/',sessionId,'/back' using: #POST.
]
{ #category : #initialization }
WebDriver >> browser: brs server: srv port: portnum [
browser := brs.
server := srv.
port := portnum.
]
{ #category : #initialization }
WebDriver >> browser: brs server: srv port: portnum optionsName: optName [
browser := brs.
server := srv.
port := portnum.
optionsName := optName.
]
{ #category : #finalization }
WebDriver >> deleteSession [
"Deletes the session."
self send: {} to: 'session/',sessionId using: #DELETE.
]
{ #category : #finalization }
WebDriver >> finalize [
browser terminate.
super finalize.
]
{ #category : #accessing }
WebDriver >> findElement: elemSelector using: locStrategy [
| reply |
reply := (self send: { #using -> locStrategy. #value -> elemSelector. }
to: 'session/',sessionId,'/element'
using: #POST)
at: #value.
(reply at: #error ifPresent: [ true ] ifAbsent: [ false ])
ifTrue: [ ^ WDException raise: reply ]
ifFalse: [ ^ (WDElement fromString: reply values first) ]
]
{ #category : #accessing }
WebDriver >> findElements: elemSelector using: locStrategy [
| reply |
reply := (self send: { #using -> locStrategy. #value -> elemSelector. }
to: 'session/',sessionId,'/elements'
using: #POST)
at: #value.
(reply isArray)
ifTrue: [ ^ reply collect: [ :elem | WDElement fromString: elem values first ] ]
ifFalse: [ ^ WDException raise: reply ]
]
{ #category : #navigation }
WebDriver >> forward [
self send: { } to: 'session/',sessionId,'/forward' using: #POST.
]
{ #category : #accessing }
WebDriver >> property: attr from: element [
^ (self
send: { }
to:
'session/' , sessionId , '/element/' , element , '/property/'
, attr
using: #GET) at: #value
]
{ #category : #navigation }
WebDriver >> refresh [
self send: { } to: 'session/',sessionId,'/refresh' using: #POST.
]
{ #category : #navigation }
WebDriver >> send: dict to: url using: method [
|result|
result := nil.
ZnClient new
host: server;
port: port;
path: url;
contents: (NeoJSONWriter toString: dict asDictionary);
contentType: ZnMimeType applicationJson;
method: method;
contentReader: [ :entity |
result := (NeoJSONReader on: (entity contents) readStream) propertyNamesAsSymbols: true; next.
];
execute.
^result.
]
{ #category : #accessing }
WebDriver >> session [
"Initializes a new WebDriver session."
sessionId
ifNil: [ sessionId := (self send: {} to: 'session' using: #POST) at: #value at: #sessionId ].
^sessionId.
]
{ #category : #accessing }
WebDriver >> session: capabilities [
sessionId ifNotNil: [ ^ self session ] ifNil: [
sessionId := (self
send: { #desiredCapabilities -> ({ optionsName -> ({#prefs -> capabilities}) asDictionary } asDictionary) }
to: 'session'
using: #POST) at: #value at: #sessionId.
^ sessionId ]
]
{ #category : #accessing }
WebDriver >> source [
^ (self
send: { }
to:
'session/' , sessionId , '/source'
using: #GET) at: #value
]
{ #category : #accessing }
WebDriver >> status [
^ (self send: { } to: 'status' using: #GET) at: #value.
]
{ #category : #accessing }
WebDriver >> title [
^ (self send: { } to: 'session/',sessionId,'/title' using: #GET) at: #value.
]
{ #category : #'text input' }
WebDriver >> type: text into: element [
self send: { #text -> text } to: 'session/',sessionId,'/element/',element,'/value' using: #POST.
]
{ #category : #accessing }
WebDriver >> url [
^ (self
send: { }
to:
'session/' , sessionId , '/url'
using: #GET) at: #value
]
{ #category : #navigation }
WebDriver >> url: url [
"Navigates the browser to the given URL."
self send: { #url -> url } to: 'session/',sessionId,'/url' using: #POST.
]