From e159af7dd1a6aa5b7f161cb61b54f1a1881ba35f Mon Sep 17 00:00:00 2001 From: Alexandra Date: Sun, 3 Jan 2016 17:29:42 +0200 Subject: [PATCH] added some methods for cookies --- dub.json | 2 +- source/selenium.d | 49 ++++++++++++++++++++++++++++++++++++++++++++--- source/test.d | 11 ++++++++--- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/dub.json b/dub.json index dc45ef4..3509ca1 100755 --- a/dub.json +++ b/dub.json @@ -5,7 +5,7 @@ "authors": ["Szabo Bogdan"], "dependencies": { - "vibe-d": "~>0.7.26" + "vibe-d": "~>0.7.27-alpha.2" }, "versions": ["VibeCustomMain"] diff --git a/source/selenium.d b/source/selenium.d index c1ee190..0b6af8f 100755 --- a/source/selenium.d +++ b/source/selenium.d @@ -7,6 +7,11 @@ import vibe.d; import vibe.http.client; import vibe.data.json; +import std.typecons; + +//hack for development dub +alias Nint = Nullable!int; +Nint a; /// https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/url @@ -18,11 +23,11 @@ class SeleniumException : Exception { * Create the exception */ this(string msg, string file = __FILE__, ulong line = cast(ulong)__LINE__, Throwable next = null) { - super(msg, file, line, next); + super(msg); } this(Json data, string file = __FILE__, ulong line = cast(ulong)__LINE__, Throwable next = null) { - super("Selenium server error: " ~ data.value.message.to!string, file, line, next); + super("Selenium server error: " ~ data.value.message.to!string); } } @@ -160,6 +165,19 @@ struct Position { long y; } +struct Cookie { + string name; + string value; + + @optional { + string path; + string domain; + bool secure; + bool httpOnly; + long expiry; + } +} + struct SeleniumSession { string serverUrl; @@ -290,8 +308,26 @@ struct SeleniumSession { return this; } + auto cookie() { + return GET!(Cookie[])("/cookie"); + } + + auto setCookie(Cookie cookie) { + struct Body { + Cookie cookie; + } + + POST("/cookie", Body(cookie)); + return this; + } + + auto deleteAllCookies() { + DELETE("/cookie"); + return this; + } + + /* -/session/:sessionId/cookie /session/:sessionId/cookie/:name /session/:sessionId/source /session/:sessionId/title @@ -372,6 +408,13 @@ session/:sessionId/touch/flick values); } + void DELETE(string path) { + if(!isConnected) connect; + + makeRequest(HTTPMethod.DELETE, + serverUrl ~ "/session/" ~ session.webdriver_remote_sessionid ~ path); + } + void POST(T)(string path, T values) { if(!isConnected) connect; diff --git a/source/test.d b/source/test.d index 10d4664..0009998 100644 --- a/source/test.d +++ b/source/test.d @@ -1,4 +1,4 @@ -module seleniumTests; +module test; import selenium; import std.stdio; @@ -35,7 +35,7 @@ unittest { assert(session.executeAsync!int("arguments[0](1+1)") == 2); assert(session.executeAsync!int("arguments[2](arguments[0] + arguments[1])", params) == 3); - writeln("screenshot: ", session.screenshot); + session.screenshot; /* not suported by chrome writeln("available_engines: ", session.imeAvailableEngines); @@ -57,6 +57,11 @@ unittest { assert(session.windowSize(handle) != Size(400, 500)); assert(session.windowPosition(handle) != Position(100, 200)); - + assert(session.cookie.length > 0); + + auto cookie = Cookie("test", "value"); + session.setCookie(cookie); + session.deleteAllCookies; + session.disconnect; }