diff --git a/source/api.d b/source/api.d index 4ec58bb..89232ac 100755 --- a/source/api.d +++ b/source/api.d @@ -235,567 +235,577 @@ struct LogEntry { string message; } -class SeleniumApi { - string serverUrl; +class SeleniumApiConnector { + const { + string serverUrl; + + Capabilities desiredCapabilities; + Capabilities requiredCapabilities; + Capabilities session; + Capabilities responseSession; + } - Capabilities desiredCapabilities; - Capabilities requiredCapabilities; - Capabilities session; + immutable { + SeleniumApiConnection connection; + SeleniumApi api; + } - this(string serverUrl, - Capabilities desiredCapabilities, - Capabilities requiredCapabilities = Capabilities(), - Capabilities session = Capabilities()) { + this(const string serverUrl, + const Capabilities desiredCapabilities, + const Capabilities requiredCapabilities = Capabilities(), + const Capabilities session = Capabilities()) { this.serverUrl = serverUrl; this.desiredCapabilities = desiredCapabilities; this.requiredCapabilities = requiredCapabilities; this.session = session; - } - - auto timeouts(TimeoutType type, long ms) { - POST("/timeouts", ["type": Json(type), "ms": Json(ms)]); - return this; - } - - auto timeoutsAsyncScript(long ms) { - POST("/timeouts/async_script", ["ms": Json(ms)]); - return this; - } - - auto timeoutsImplicitWait(long ms) { - POST("/timeouts/implicit_wait", ["ms": Json(ms)]); - return this; - } - auto windowHandle() { - return GET!string("/window_handle"); - } - - auto windowHandles() { - return GET!(string[])("/window_handles"); - } - - auto url(string url) { - POST("/url", ["url": Json(url)]); - return this; - } - auto url() { - return GET!string("/url"); - } + responseSession = makeRequest(HTTPMethod.POST, serverUrl ~ "/session", ["desiredCapabilities": desiredCapabilities]) + .deserializeJson!(SessionResponse!Capabilities).value; - auto forward() { - POST("/forward"); - return this; + connection = new immutable SeleniumApiConnection(serverUrl, responseSession.webdriver_remote_sessionid.idup); + api = new immutable SeleniumApi(connection); } +} - auto back() { - POST("/back"); - return this; +class SeleniumApiConnection { + immutable { + string sessionId; + string serverUrl; } - auto refresh() { - POST("/refresh"); - return this; + this(immutable string serverUrl, string sessionId) immutable { + this.sessionId = sessionId; + this.serverUrl = serverUrl; } - auto execute(T = string)(string script, Json args = Json.emptyArray) { - return POST!T("/execute", [ "script": Json(script), "args": args ]); - } + inout { + void disconnect() { + makeRequest(HTTPMethod.DELETE, + serverUrl ~ "/session/" ~ sessionId); + } - auto executeAsync(T = string)(string script, Json args = Json.emptyArray) { - return POST!T("/execute_async", [ "script": Json(script), "args": args ]); - } + void DELETE(T)(string path, T values = null) { + makeRequest(HTTPMethod.DELETE, + serverUrl ~ "/session/" ~ sessionId ~ path, + values); + } - auto screenshot() { - return GET!string("/screenshot"); - } + void DELETE(string path) { + makeRequest(HTTPMethod.DELETE, + serverUrl ~ "/session/" ~ sessionId ~ path); + } - auto imeAvailableEngines() { - return GET!string("/ime/available_engines"); - } + void POST(T)(string path, T values) { + makeRequest(HTTPMethod.POST, + serverUrl ~ "/session/" ~ sessionId ~ path, + values); + } - auto imeActiveEngine() { - return GET!string("/ime/active_engine"); - } + void POST(string path) { + makeRequest(HTTPMethod.POST, + serverUrl ~ "/session/" ~ sessionId ~ path); + } - auto imeActivated() { - return GET!bool("/ime/activated"); - } + auto POST(U, T)(string path, T values) { + return makeRequest(HTTPMethod.POST, + serverUrl ~ "/session/" ~ sessionId ~ path, + values).deserializeJson!(SessionResponse!U).value; + } - auto imeDeactivate() { - POST("/ime/deactivate"); - return this; - } + auto POST(U)(string path) { + return makeRequest(HTTPMethod.POST, + serverUrl ~ "/session/" ~ sessionId ~ path) + .deserializeJson!(SessionResponse!U).value; + } - auto imeActivate(string engine) { - POST("/ime/activate", ["engine": engine]); - return this; + T GET(T)(string path) { + return makeRequest(HTTPMethod.GET, serverUrl ~ "/session/" ~ sessionId ~ path) + .deserializeJson!(SessionResponse!T).value; + } } +} - auto frame(string id) { - POST("/frame", ["id": id]); - return this; - } +class SeleniumApi { + const SeleniumApiConnection connection; - auto frame(long id) { - POST("/frame", ["id": id]); - return this; + this(inout SeleniumApiConnection connection) inout { + this.connection = connection; } - auto frame(WebElement element) { - POST("/frame", element); - return this; - } + inout { + auto timeouts(TimeoutType type, long ms) { + connection.POST("/timeouts", ["type": Json(type), "ms": Json(ms)]); + return this; + } - auto frameParent() { - POST("/frame/parent"); - return this; - } + auto timeoutsAsyncScript(long ms) { + connection.POST("/timeouts/async_script", ["ms": Json(ms)]); + return this; + } - auto selectWindow(string name) { - POST("/window", ["name": name]); - return this; - } + auto timeoutsImplicitWait(long ms) { + connection.POST("/timeouts/implicit_wait", ["ms": Json(ms)]); + return this; + } + auto windowHandle() { + return connection.GET!string("/window_handle"); + } - auto windowClose() { - DELETE("/window"); - return this; - } + auto windowHandles() { + return connection.GET!(string[])("/window_handles"); + } - auto windowSize(Size size) { - POST("/window/size", size); - return this; - } + auto url(string url) { + connection.POST("/url", ["url": Json(url)]); + return this; + } - auto windowSize() { - return GET!Size("/window/size"); - } + auto url() { + return connection.GET!string("/url"); + } - auto windowMaximize() { - POST("/window/maximize"); - return this; - } + auto forward() { + connection.POST("/forward"); + return this; + } - auto cookie() { - return GET!(Cookie[])("/cookie"); - } + auto back() { + connection.POST("/back"); + return this; + } - auto setCookie(Cookie cookie) { - struct Body { - Cookie cookie; + auto refresh() { + connection.POST("/refresh"); + return this; } - POST("/cookie", Body(cookie)); - return this; - } + auto execute(T = string)(string script, Json args = Json.emptyArray) { + return connection.POST!T("/execute", [ "script": Json(script), "args": args ]); + } - auto deleteAllCookies() { - DELETE("/cookie"); - return this; - } + auto executeAsync(T = string)(string script, Json args = Json.emptyArray) { + return connection.POST!T("/execute_async", [ "script": Json(script), "args": args ]); + } - auto deleteCookie(string name) { - DELETE("/cookie/" ~ name); - return this; - } + auto screenshot() { + return connection.GET!string("/screenshot"); + } - auto source() { - return GET!string("/source"); - } + auto imeAvailableEngines() { + return connection.GET!string("/ime/available_engines"); + } - auto title() { - return GET!string("/title"); - } + auto imeActiveEngine() { + return connection.GET!string("/ime/active_engine"); + } - auto element(ElementLocator locator) { - return POST!WebElement("/element", locator); - } + auto imeActivated() { + return connection.GET!bool("/ime/activated"); + } - auto elements(ElementLocator locator) { - return POST!(WebElement[])("/elements", locator); - } + auto imeDeactivate() { + connection.POST("/ime/deactivate"); + return this; + } - auto activeElement() { - return POST!WebElement("/element/active"); - } + auto imeActivate(string engine) { + connection.POST("/ime/activate", ["engine": engine]); + return this; + } - auto elementFromElement(string initialElemId, ElementLocator locator) { - return POST!WebElement("/element/" ~ initialElemId ~ "/element", locator); - } + auto frame(string id) { + connection.POST("/frame", ["id": id]); + return this; + } - auto elementsFromElement(string initialElemId, ElementLocator locator) { - return POST!(WebElement[])("/element/" ~ initialElemId ~ "/elements", locator); - } + auto frame(long id) { + connection.POST("/frame", ["id": id]); + return this; + } - auto clickElement(string elementId) { - POST("/element/" ~ elementId ~ "/click"); - return this; - } + auto frame(WebElement element) { + connection.POST("/frame", element); + return this; + } - auto submitElement(string elementId) { - POST("/element/" ~ elementId ~ "/submit"); - return this; - } + auto frameParent() { + connection.POST("/frame/parent"); + return this; + } - auto elementText(string elementId) { - return GET!string("/element/" ~ elementId ~ "/text"); - } + auto selectWindow(string name) { + connection.POST("/window", ["name": name]); + return this; + } - auto sendKeys(string elementId, string[] value) { - struct Body { - string[] value; + auto windowClose() { + connection.DELETE("/window"); + return this; } - POST("/element/" ~ elementId ~ "/value", Body(value)); - return this; - } + auto windowSize(Size size) { + connection.POST("/window/size", size); + return this; + } - auto sendKeysToActiveElement(string[] value) { - struct Body { - string[] value; + auto windowSize() { + return connection.GET!Size("/window/size"); } - POST("/keys", Body(value)); - return this; - } + auto windowMaximize() { + connection.POST("/window/maximize"); + return this; + } - auto elementName(string elementId) { - return GET!string("/element/" ~ elementId ~ "/name"); - } + auto cookie() { + return connection.GET!(Cookie[])("/cookie"); + } - auto clearElementValue(string elementId) { - POST("/element/" ~ elementId ~ "/clear"); - return this; - } + auto setCookie(Cookie cookie) { + struct Body { + Cookie cookie; + } - auto elementSelected(string elementId) { - return GET!bool("/element/" ~ elementId ~ "/selected"); - } + connection.POST("/cookie", Body(cookie)); + return this; + } - auto elementEnabled(string elementId) { - return GET!bool("/element/" ~ elementId ~ "/enabled"); - } + auto deleteAllCookies() { + connection.DELETE("/cookie"); + return this; + } - auto elementValue(string elementId, string attribute) { - return GET!string("/element/" ~ elementId ~ "/attribute/" ~ attribute); - } + auto deleteCookie(string name) { + connection.DELETE("/cookie/" ~ name); + return this; + } - auto elementEqualsOther(string firstElementId, string secondElementId) { - return GET!bool("/element/" ~ firstElementId ~ "/equals/" ~ secondElementId); - } + auto source() { + return connection.GET!string("/source"); + } - auto elementDisplayed(string elementId) { - return GET!bool("/element/" ~ elementId ~ "/displayed"); - } + auto title() { + return connection.GET!string("/title"); + } - auto elementLocation(string elementId) { - return GET!Position("/element/" ~ elementId ~ "/location"); - } + auto element(ElementLocator locator) { + return connection.POST!WebElement("/element", locator); + } - auto elementLocationInView(string elementId) { - return GET!Position("/element/" ~ elementId ~ "/location_in_view"); - } + auto elements(ElementLocator locator) { + return connection.POST!(WebElement[])("/elements", locator); + } - auto elementSize(string elementId) { - return GET!Size("/element/" ~ elementId ~ "/size"); - } + auto activeElement() { + return connection.POST!WebElement("/element/active"); + } - auto elementCssPropertyName(string elementId, string propertyName) { - return GET!string("/element/" ~ elementId ~ "/css/" ~ propertyName); - } + auto elementFromElement(string initialElemId, ElementLocator locator) { + return connection.POST!WebElement("/element/" ~ initialElemId ~ "/element", locator); + } - auto orientation() { - return GET!Orientation("/orientation"); - } + auto elementsFromElement(string initialElemId, ElementLocator locator) { + return connection.POST!(WebElement[])("/element/" ~ initialElemId ~ "/elements", locator); + } - auto setOrientation(Orientation orientation) { - struct Body { - Orientation orientation; + auto clickElement(string elementId) { + connection.POST("/element/" ~ elementId ~ "/click"); + return this; } - return POST("/orientation", Body(orientation)); - } + auto submitElement(string elementId) { + connection.POST("/element/" ~ elementId ~ "/submit"); + return this; + } - auto alertText() { - return GET!string("/alert_text"); - } + auto elementText(string elementId) { + return connection.GET!string("/element/" ~ elementId ~ "/text"); + } - auto setPromptText(string text) { - POST("/alert_text", ["text": text]); - return this; - } + auto sendKeys(string elementId, string[] value) { + struct Body { + string[] value; + } - auto acceptAlert() { - POST("/accept_alert"); - return this; - } + connection.POST("/element/" ~ elementId ~ "/value", Body(value)); + return this; + } - auto dismissAlert() { - POST("/dismiss_alert"); - return this; - } + auto sendKeysToActiveElement(const(string[]) value) { + struct Body { + const(string[]) value; + } - auto moveTo(Position position) { - POST("/moveto", ["xoffset": position.x, "yoffset": position.y]); - return this; - } + connection.POST("/keys", Body(value)); + return this; + } - auto moveTo(string elementId) { - POST("/moveto", ["element": elementId]); - return this; - } + auto elementName(string elementId) { + return connection.GET!string("/element/" ~ elementId ~ "/name"); + } - auto moveTo(string elementId, Position position) { - struct Body { - string element; - long xoffset; - long yoffset; + auto clearElementValue(string elementId) { + connection.POST("/element/" ~ elementId ~ "/clear"); + return this; } - POST("/moveto", Body(elementId, position.x, position.y)); - return this; - } + auto elementSelected(string elementId) { + return connection.GET!bool("/element/" ~ elementId ~ "/selected"); + } - auto click(MouseButton button = MouseButton.left) { - POST("/click", ["button": button]); - return this; - } + auto elementEnabled(string elementId) { + return connection.GET!bool("/element/" ~ elementId ~ "/enabled"); + } - auto buttonDown(MouseButton button = MouseButton.left) { - POST("/buttondown", ["button": button]); - return this; - } + auto elementValue(string elementId, string attribute) { + return connection.GET!string("/element/" ~ elementId ~ "/attribute/" ~ attribute); + } - auto buttonUp(MouseButton button = MouseButton.left) { - POST("/buttonup", ["button": button]); - return this; - } + auto elementEqualsOther(string firstElementId, string secondElementId) { + return connection.GET!bool("/element/" ~ firstElementId ~ "/equals/" ~ secondElementId); + } - auto doubleClick() { - POST("/doubleclick"); - return this; - } + auto elementDisplayed(string elementId) { + return connection.GET!bool("/element/" ~ elementId ~ "/displayed"); + } - auto touchClick(string elementId) { - POST("/touch/click", ["element": elementId]); - return this; - } + auto elementLocation(string elementId) { + return connection.GET!Position("/element/" ~ elementId ~ "/location"); + } - auto touchDown(Position position) { - POST("/touch/down", ["x": position.x, "y": position.y]); - return this; - } + auto elementLocationInView(string elementId) { + return connection.GET!Position("/element/" ~ elementId ~ "/location_in_view"); + } - auto touchUp(Position position) { - POST("/touch/up", ["x": position.x, "y": position.y]); - return this; - } + auto elementSize(string elementId) { + return connection.GET!Size("/element/" ~ elementId ~ "/size"); + } - auto touchMove(Position position) { - POST("/touch/move", ["x": position.x, "y": position.y]); - return this; - } + auto elementCssPropertyName(string elementId, string propertyName) { + return connection.GET!string("/element/" ~ elementId ~ "/css/" ~ propertyName); + } - auto touchScroll(string elementId, Position position) { - struct Body { - string element; - long xoffset; - long yoffset; + auto orientation() { + return connection.GET!Orientation("/orientation"); } - POST("/touch/scroll", Body(elementId, position.x, position.y)); - return this; - } + auto setOrientation(Orientation orientation) { + struct Body { + Orientation orientation; + } - auto touchScroll(Position position) { - POST("/touch/scroll", ["xoffset": position.x, "yoffset": position.y]); - return this; - } + return connection.POST("/orientation", Body(orientation)); + } - auto touchDoubleClick(string elementId) { - POST("/touch/doubleclick", ["element": elementId]); - return this; - } + auto alertText() { + return connection.GET!string("/alert_text"); + } - auto touchLongClick(string elementId) { - POST("/touch/longclick", ["element": elementId]); - return this; - } + auto setPromptText(string text) { + connection.POST("/alert_text", ["text": text]); + return this; + } - auto touchFlick(string elementId, Position position, long speed) { - struct Body { - string element; - long xoffset; - long yoffset; - long speed; + auto acceptAlert() { + connection.POST("/accept_alert"); + return this; } - POST("/touch/flick", Body(elementId, position.x, position.y, speed)); - return this; - } + auto dismissAlert() { + connection.POST("/dismiss_alert"); + return this; + } - auto touchFlick(long xSpeed, long ySpeed) { - POST("/touch/flick", [ "xspeed": xSpeed, "yspeed": ySpeed ]); - return this; - } + auto moveTo(Position position) { + connection.POST("/moveto", ["xoffset": position.x, "yoffset": position.y]); + return this; + } - auto geoLocation() { - return GET!(GeoLocation!double)("/location"); - } + auto moveTo(string elementId) { + connection.POST("/moveto", ["element": elementId]); + return this; + } - auto setGeoLocation(T)(T location) { - POST("/location", ["location": location]); - return this; - } + auto moveTo(string elementId, Position position) { + struct Body { + string element; + long xoffset; + long yoffset; + } - auto localStorage() { - return GET!(string[])("/local_storage"); - } + connection.POST("/moveto", Body(elementId, position.x, position.y)); + return this; + } - auto setLocalStorage(string key, string value) { - POST("/local_storage", ["key": key, "value": value]); - return this; - } + auto click(MouseButton button = MouseButton.left) { + connection.POST("/click", ["button": button]); + return this; + } - auto deleteLocalStorage() { - DELETE("/local_storage"); - return this; - } + auto buttonDown(MouseButton button = MouseButton.left) { + connection.POST("/buttondown", ["button": button]); + return this; + } - auto localStorage(string key) { - return GET!(string)("/local_storage/key/" ~ key); - } + auto buttonUp(MouseButton button = MouseButton.left) { + connection.POST("/buttonup", ["button": button]); + return this; + } - auto deleteLocalStorage(string key) { - DELETE("/local_storage/key/" ~ key); - return this; - } + auto doubleClick() { + connection.POST("/doubleclick"); + return this; + } - auto localStorageSize() { - return GET!(long)("/local_storage/size"); - } + auto touchClick(string elementId) { + connection.POST("/touch/click", ["element": elementId]); + return this; + } - auto sessionStorage() { - return GET!(string[])("/session_storage"); - } + auto touchDown(Position position) { + connection.POST("/touch/down", ["x": position.x, "y": position.y]); + return this; + } - auto setSessionStorage(string key, string value) { - POST("/session_storage", ["key": key, "value": value]); - return this; - } + auto touchUp(Position position) { + connection.POST("/touch/up", ["x": position.x, "y": position.y]); + return this; + } - auto deleteSessionStorage() { - DELETE("/session_storage"); - return this; - } + auto touchMove(Position position) { + connection.POST("/touch/move", ["x": position.x, "y": position.y]); + return this; + } - auto sessionStorage(string key) { - return GET!(string)("/session_storage/key/" ~ key); - } + auto touchScroll(string elementId, Position position) { + struct Body { + string element; + long xoffset; + long yoffset; + } - auto deleteSessionStorage(string key) { - DELETE("/session_storage/key/" ~ key); - return this; - } + connection.POST("/touch/scroll", Body(elementId, position.x, position.y)); + return this; + } - auto sessionStorageSize() { - return GET!(long)("/session_storage/size"); - } + auto touchScroll(Position position) { + connection.POST("/touch/scroll", ["xoffset": position.x, "yoffset": position.y]); + return this; + } - auto log(LogType logType) { - return POST!(LogEntry[])("/log", ["type": logType]); - } + auto touchDoubleClick(string elementId) { + connection.POST("/touch/doubleclick", ["element": elementId]); + return this; + } - auto logTypes() { - return GET!(string[])("/log/types"); - } + auto touchLongClick(string elementId) { + connection.POST("/touch/longclick", ["element": elementId]); + return this; + } - auto applicationCacheStatus() { - return GET!CacheStatus("/application_cache/status"); - } + auto touchFlick(string elementId, Position position, long speed) { + struct Body { + string element; + long xoffset; + long yoffset; + long speed; + } - /* -/session/:sessionId/element/:id - not yet implemented in Selenium + connection.POST("/touch/flick", Body(elementId, position.x, position.y, speed)); + return this; + } -/session/:sessionId/log/types -/session/:sessionId/application_cache/status*/ + auto touchFlick(long xSpeed, long ySpeed) { + connection.POST("/touch/flick", [ "xspeed": xSpeed, "yspeed": ySpeed ]); + return this; + } + auto geoLocation() { + return connection.GET!(GeoLocation!double)("/location"); + } - auto wait(long ms) { - sleep(ms.msecs); - return this; - } + auto setGeoLocation(T)(T location) { + connection.POST("/location", ["location": location]); + return this; + } - void disconnect() { - if(isConnected) { + auto localStorage() { + return connection.GET!(string[])("/local_storage"); + } - makeRequest(HTTPMethod.DELETE, - serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid); + auto setLocalStorage(string key, string value) { + connection.POST("/local_storage", ["key": key, "value": value]); + return this; } - } - private { - bool isConnected; + auto deleteLocalStorage() { + connection.DELETE("/local_storage"); + return this; + } - void DELETE(T)(string path, T values = null) { - if(!isConnected) connect; + auto localStorage(string key) { + return connection.GET!(string)("/local_storage/key/" ~ key); + } - makeRequest(HTTPMethod.DELETE, - serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid ~ path, - values); + auto deleteLocalStorage(string key) { + connection.DELETE("/local_storage/key/" ~ key); + return this; } - void DELETE(string path) { - if(!isConnected) connect; + auto localStorageSize() { + return connection.GET!(long)("/local_storage/size"); + } - makeRequest(HTTPMethod.DELETE, - serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid ~ path); + auto sessionStorage() { + return connection.GET!(string[])("/session_storage"); } - void POST(T)(string path, T values) { - if(!isConnected) connect; + auto setSessionStorage(string key, string value) { + connection.POST("/session_storage", ["key": key, "value": value]); + return this; + } - makeRequest(HTTPMethod.POST, - serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid ~ path, - values); + auto deleteSessionStorage() { + connection.DELETE("/session_storage"); + return this; } - void POST(string path) { - if(!isConnected) connect; + auto sessionStorage(string key) { + return connection.GET!(string)("/session_storage/key/" ~ key); + } - makeRequest(HTTPMethod.POST, - serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid ~ path); + auto deleteSessionStorage(string key) { + connection.DELETE("/session_storage/key/" ~ key); + return this; } - auto POST(U, T)(string path, T values) { - if(!isConnected) connect; + auto sessionStorageSize() { + return connection.GET!(long)("/session_storage/size"); + } - return makeRequest(HTTPMethod.POST, - serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid ~ path, - values).deserializeJson!(SessionResponse!U).value; + auto log(LogType logType) { + return connection.POST!(LogEntry[])("/log", ["type": logType]); } - auto POST(U)(string path) { - if(!isConnected) connect; + auto logTypes() { + return connection.GET!(string[])("/log/types"); + } - return makeRequest(HTTPMethod.POST, - serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid ~ path) - .deserializeJson!(SessionResponse!U).value; + auto applicationCacheStatus() { + return connection.GET!CacheStatus("/application_cache/status"); } - T GET(T)(string path) { - if(!isConnected) connect; + /* + /session/:sessionId/element/:id - not yet implemented in Selenium - return makeRequest(HTTPMethod.GET, serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid ~ path) - .deserializeJson!(SessionResponse!T).value; - } + /session/:sessionId/log/types + /session/:sessionId/application_cache/status*/ - void connect() { - session = makeRequest(HTTPMethod.POST, serverUrl ~ "/session", ["desiredCapabilities": desiredCapabilities]) - .deserializeJson!(SessionResponse!Capabilities).value; - isConnected = true; + auto wait(long ms) { + sleep(ms.msecs); + return this; } } } diff --git a/source/session.d b/source/session.d index 5c0089d..d502ee6 100644 --- a/source/session.d +++ b/source/session.d @@ -5,13 +5,14 @@ import std.stdio; class SeleniumSession { - SeleniumApi api; + immutable SeleniumApi api; this(string serverUrl, Capabilities desiredCapabilities, Capabilities requiredCapabilities = Capabilities(), Capabilities session = Capabilities()) { - api = new SeleniumApi(serverUrl, desiredCapabilities, requiredCapabilities, session); + auto connector = new SeleniumApiConnector(serverUrl, desiredCapabilities, requiredCapabilities, session); + api = connector.api; } SeleniumWindow currentWindow() @@ -22,12 +23,12 @@ class SeleniumSession class SeleniumWindow { - protected + protected const { SeleniumApi api; } - this(SeleniumApi api) + this(const SeleniumApi api) { this.api = api; } @@ -88,12 +89,12 @@ class Element { alias opEquals = Object.opEquals; - private const { + private immutable { SeleniumApi api; WebElement element; } - this(SeleniumApi api, WebElement element) + this(immutable SeleniumApi api, immutable WebElement element) immutable { this.api = api; this.element = element; @@ -101,141 +102,143 @@ class Element static { - Element findOne(SeleniumApi api, ElementLocator locator) + immutable(Element) findOne(immutable SeleniumApi api, immutable ElementLocator locator) { - return new Element(api, api.element(locator)); + return new immutable Element(api, api.element(locator)); } - Element[] findMany(SeleniumApi api, ElementLocator locator) + immutable(Element)[] findMany(immutable SeleniumApi api, immutable ElementLocator locator) { - Element[] elements; + immutable(Element)[] elements; foreach (webElement; api.elements(locator)) { - elements ~= new Element(api, webElement); + elements ~= new immutable Element(api, webElement); } return elements; } - Element getActive(SeleniumApi api) + immutable(Element) getActive(immutable SeleniumApi api) { - return new Element(api, api.activeElement); + return new immutable Element(api, api.activeElement); } - void sendKeysToActive(SeleniumApi api, string[] value) + void sendKeysToActive(immutable SeleniumApi api, const string[] value) { api.sendKeysToActiveElement(value); } } - Element findOne(ElementLocator locator) - { - return new Element(api, api.elementFromElement(element.ELEMENT, locator)); - } - - Element[] findMany(ElementLocator locator) - { - Element[] elements; - - foreach (webElement; api.elementsFromElement(element.ELEMENT, locator)) + inout { + immutable(Element) findOne(ElementLocator locator) { - elements ~= new Element(api, webElement); + return new immutable Element(api, api.elementFromElement(element.ELEMENT, locator)); } - return elements; - } - - Element click() - { - api.clickElement(element.ELEMENT); - - return this; - } - - Element submit() - { - api.submitElement(element.ELEMENT); - return this; - } + immutable(Element)[] findMany(ElementLocator locator) + { + immutable(Element)[] elements; - Element sendKeys(string[] value) - { - api.sendKeys(element.ELEMENT, value); - return this; - } + foreach (webElement; api.elementsFromElement(element.ELEMENT, locator)) + { + elements ~= new immutable Element(api, webElement); + } - Element clear() - { - api.clearElementValue(element.ELEMENT); - return this; - } + return elements; + } - @property - { - string text() + inout(Element) click() { - return api.elementText(element.ELEMENT); + api.clickElement(element.ELEMENT); + + return this; } - string name() + inout(Element) submit() { - return api.elementName(element.ELEMENT); + api.submitElement(element.ELEMENT); + return this; } - bool isSelected() + inout(Element) sendKeys(string[] value) { - return api.elementSelected(element.ELEMENT); + api.sendKeys(element.ELEMENT, value); + return this; } - bool isEnabled() + inout(Element) clear() { - return api.elementSelected(element.ELEMENT); + api.clearElementValue(element.ELEMENT); + return this; } - bool isDisplayed() + @property { - return api.elementDisplayed(element.ELEMENT); + string text() + { + return api.elementText(element.ELEMENT); + } + + string name() + { + return api.elementName(element.ELEMENT); + } + + bool isSelected() + { + return api.elementSelected(element.ELEMENT); + } + + bool isEnabled() + { + return api.elementSelected(element.ELEMENT); + } + + bool isDisplayed() + { + return api.elementDisplayed(element.ELEMENT); + } + + Position position() + { + return api.elementLocation(element.ELEMENT); + } + + Position positionInView() + { + return api.elementLocationInView(element.ELEMENT); + } + + Size size() + { + return api.elementSize(element.ELEMENT); + } + + string seleniumId() inout + { + return element.ELEMENT; + } } - Position position() + string attribute(string name) { - return api.elementLocation(element.ELEMENT); + return api.elementValue(element.ELEMENT, name); } - Position positionInView() + string elementCssPropertyName(string name) { - return api.elementLocationInView(element.ELEMENT); + return api.elementValue(element.ELEMENT, name); } - Size size() + bool opEquals(ref Element other) const { - return api.elementSize(element.ELEMENT); + return api.elementEqualsOther(element.ELEMENT, other.seleniumId); } - string seleniumId() inout + bool opEquals(Element other) const { - return element.ELEMENT; + return api.elementEqualsOther(element.ELEMENT, other.seleniumId); } } - - string attribute(string name) - { - return api.elementValue(element.ELEMENT, name); - } - - string elementCssPropertyName(string name) - { - return api.elementValue(element.ELEMENT, name); - } - - bool opEquals(ref Element other) const - { - return api.elementEqualsOther(element.ELEMENT, other.seleniumId); - } - - bool opEquals(Element other) const - { - return api.elementEqualsOther(element.ELEMENT, other.seleniumId); - } }