Skip to content

Commit

Permalink
Updated session abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Szabo Bogdan committed Aug 12, 2016
1 parent 69fa63b commit 692bee6
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 30 deletions.
1 change: 1 addition & 0 deletions source/api.d
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ class SeleniumApi {
connection.POST("/timeouts/implicit_wait", ["ms": Json(ms)]);
return this;
}

auto windowHandle() {
return connection.GET!string("/window_handle");
}
Expand Down
263 changes: 235 additions & 28 deletions source/session.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,253 @@ module selenium.session;
import selenium.api;
import std.stdio;

class SeleniumSession
immutable class SeleniumSession
{
immutable SeleniumApi api;
SeleniumApi api;

this(string serverUrl, Capabilities desiredCapabilities,
Capabilities requiredCapabilities = Capabilities(), Capabilities session = Capabilities())
{

auto connector = new SeleniumApiConnector(serverUrl, desiredCapabilities, requiredCapabilities, session);
auto connector = new SeleniumApiConnector(serverUrl,
desiredCapabilities, requiredCapabilities, session);
api = connector.api;
}

SeleniumWindow currentWindow()
@property
{
immutable(SeleniumWindow) currentWindow()
{
return new immutable SeleniumWindow(api, this);
}

immutable(SeleniumNavigation) navigation()
{
return new immutable SeleniumNavigation(api, this);
}

immutable(SeleniumCookie) cookie()
{
return new immutable SeleniumCookie(api, this);
}

auto windowHandles()
{
return api.windowHandles;
}
}

immutable(Element) findOne(immutable ElementLocator locator)
{
return Element.findOne(api, locator);
}

immutable(Element)[] findMany(immutable ElementLocator locator)
{
return Element.findMany(api, locator);
}

immutable(Element) getActiveElement()
{
return Element.getActive(api);
}

auto frame(string id)
{
api.frame(id);
return this;
}

auto frame(long id)
{
api.frame(id);
return this;
}

auto frame(WebElement element)
{
return new SeleniumWindow(api);
api.frame(element);
return this;
}

auto frameParent()
{
api.frameParent;
return this;
}

void close()
{
api.connection.disconnect;
}
}

class SeleniumWindow
class SeleniumCookie
{
protected const
immutable
{
SeleniumApi api;
SeleniumSession parent;
}

this(const SeleniumApi api)
this(immutable SeleniumApi api, immutable SeleniumSession parent) immutable
{
this.api = api;
this.parent = parent;
}

auto size()
@property immutable
{
return api.windowSize;
}
auto all() {
return api.cookie;
}

void size(Size value)
{
api.windowSize(value);
auto set(string name, string value) {
api.setCookie(Cookie(name, value));
return parent;
}

auto set(Cookie cookie) {
api.setCookie(cookie);
return parent;
}

auto deleteAll() {
api.deleteAllCookies();
return parent;
}

auto deleteByName(string name) {
api.deleteCookie(name);
return parent;
}
}
}

void maximize()
immutable class SeleniumNavigation
{
immutable
{
api.windowMaximize;
SeleniumApi api;
SeleniumSession parent;
}

auto screenshot()
this(immutable SeleniumApi api, immutable SeleniumSession parent)
{
api.screenshot;
this.api = api;
this.parent = parent;
}

void close()
@property
{
api.windowClose;
auto url(string url)
{
api.url(url);
return parent;
}

auto url()
{
return api.url;
}

auto forward()
{
api.forward;
return parent;
}

auto back()
{
api.back;
return parent;
}

auto refresh()
{
api.refresh;
return parent;
}
}
}

auto source()
immutable class SeleniumWindow
{
SeleniumApi api;
SeleniumSession parent;

this(immutable SeleniumApi api, immutable SeleniumSession parent)
{
api.source;
this.api = api;
this.parent = parent;
}

auto title()
immutable
{
api.title;
auto select(string name)
{
api.selectWindow(name);
return parent;
}

auto handle()
{
return api.windowHandle;
}

auto size()
{
return api.windowSize;
}

auto size(Size value)
{
api.windowSize(value);
return parent;
}

auto maximize()
{
api.windowMaximize;
return parent;
}

auto screenshot()
{
return api.screenshot;
}

auto close()
{
api.windowClose;
return parent;
}

auto source()
{
return api.source;
}

auto title()
{
return api.title;
}

auto execute(T = string)(string script, Json args = Json.emptyArray)
{
return api.execute!T(script, args);
}

auto executeAsync(T = string)(string script, Json args = Json.emptyArray)
{
return api.executeAsync!T(script, args);
}
}
}

@("Session currentWindow")
unittest
{
auto session = new SeleniumSession("http://127.0.0.1:4444/wd/hub", Capabilities.chrome);
auto session = new immutable SeleniumSession("http://127.0.0.1:4444/wd/hub", Capabilities.chrome);

session.currentWindow.size = Size(400, 500);
assert(session.currentWindow.size == Size(400, 500));
Expand All @@ -83,13 +261,16 @@ unittest
session.currentWindow.title;

session.currentWindow.close;

session.close;
}

class Element
{
alias opEquals = Object.opEquals;

private immutable {
private immutable
{
SeleniumApi api;
WebElement element;
}
Expand Down Expand Up @@ -130,7 +311,8 @@ class Element
}
}

inout {
inout
{
immutable(Element) findOne(ElementLocator locator)
{
return new immutable Element(api, api.elementFromElement(element.ELEMENT, locator));
Expand Down Expand Up @@ -242,3 +424,28 @@ class Element
}
}
}

@("Session find one element")
unittest
{
auto session = new immutable SeleniumSession("http://127.0.0.1:4444/wd/hub", Capabilities.chrome);
session.navigation.url("https://www.amazon.com/All-Light-We-Cannot-See/dp/1476746583/");

session.findOne(ElementLocator(LocatorStrategy.ClassName, "contributorNameID")).click;

assert(session.navigation.url
== "https://www.amazon.com/Anthony-Doerr/e/B000APOX62/ref=dp_byline_cont_book_1");

session.close;
}

@("Session find many elements")
unittest
{
auto session = new immutable SeleniumSession("http://127.0.0.1:4444/wd/hub", Capabilities.chrome);
session.navigation.url("https://www.amazon.com/All-Light-We-Cannot-See/dp/1476746583/");

assert(session.findMany(ElementLocator(LocatorStrategy.TagName, "img")).length > 0);

session.close;
}
4 changes: 2 additions & 2 deletions source/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vibe.data.json;

import std.datetime;

unittest {
unittest {/+
auto url1 = "https://www.amazon.com/All-Light-We-Cannot-See/dp/1476746583/";
auto url2 = "https://www.amazon.com/The-Boys-Boat-Americans-Olympics/dp/0143125478/";
Expand Down Expand Up @@ -184,5 +184,5 @@ unittest {
//session.applicationCacheStatus();
connector.connection.disconnect;
connector.connection.disconnect;+/
}

0 comments on commit 692bee6

Please sign in to comment.