Skip to content

Commit

Permalink
added check method
Browse files Browse the repository at this point in the history
  • Loading branch information
Szabo Bogdan committed Aug 27, 2016
1 parent d8c60b3 commit 8d7c148
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 32 deletions.
45 changes: 33 additions & 12 deletions source/api.d
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,39 @@ struct ElementLocator {
string value;
}


ElementLocator classLocator(string name) {
return ElementLocator(LocatorStrategy.ClassName, name);
}

ElementLocator cssLocator(string name) {
return ElementLocator(LocatorStrategy.CssSelector, name);
}

ElementLocator idLocator(string name) {
return ElementLocator(LocatorStrategy.Id, name);
}

ElementLocator nameLocator(string name) {
return ElementLocator(LocatorStrategy.Name, name);
}

ElementLocator linkTextLocator(string name) {
return ElementLocator(LocatorStrategy.LinkText, name);
}

ElementLocator partialLinkTextLocator(string name) {
return ElementLocator(LocatorStrategy.PartialLinkText, name);
}

ElementLocator tagLocator(string name) {
return ElementLocator(LocatorStrategy.TagName, name);
}

ElementLocator xpathLocator(string name) {
return ElementLocator(LocatorStrategy.XPath, name);
}

struct WebElement {
string ELEMENT;
}
Expand Down Expand Up @@ -819,8 +852,6 @@ private Json makeRequest(T)(HTTPMethod method, string path, T data) {
Json result;
bool done = false;

logInfo("REQUEST: %s %s %s", method, path, data.serializeToJson.toPrettyString);

requestHTTP(path,
(scope req) {
req.method = method;
Expand All @@ -831,15 +862,11 @@ private Json makeRequest(T)(HTTPMethod method, string path, T data) {

if(res.statusCode == 500) {
throw new SeleniumException(result);
} else {
logInfo("Response: %d %s", res.statusCode, result.toPrettyString);
}
done = true;
}
);

writeln("RESULT: ", result);

return result;
}

Expand All @@ -852,8 +879,6 @@ private Json makeRequest(HTTPMethod method, string path) {
Json result;
bool done = false;

logInfo("REQUEST: %s %s", method, path);

requestHTTP(path,
(scope req) {
req.method = method;
Expand All @@ -863,13 +888,9 @@ private Json makeRequest(HTTPMethod method, string path) {

if(res.statusCode == 500) {
throw new SeleniumException(result);
} else {
logInfo("Response: %d %s", res.statusCode, result.toPrettyString);
}
done = true;
}
);

writeln("RESULT: ", result);
return result;
}
108 changes: 88 additions & 20 deletions source/workflow.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@ import std.algorithm.searching;
import selenium.session;
import selenium.api;

import std.string;
import std.conv;

import vibe.core.log;

class SeleniumPage {
protected {
immutable SeleniumSession session;
}

this(immutable SeleniumSession session) {
this.session = session;
}

bool isPresent() {
return true;
}
}


Expand All @@ -23,7 +37,39 @@ class WorkflowCheck(T, U) : Workflow!(T, U) {
}

auto opDispatch(string name, T...)(T props) if(name != "define") {
return this;
alias member = child.opDispatch!name;

static if(is(ReturnType!member == bool)) {
assert(child.opDispatch!name(props), "Check `" ~ name ~ "`` faield.");
return this;
} else {
auto val = child.opDispatch!name(props);

return new WorkflowCheck!(typeof(val), void*)(val, null);
}
}
}

class WorkflowNamed(string workflowName, T ,U) : Workflow!(T, U) {

this(T child, U cls) {
super(child, cls);
}

auto opDispatch(string name, T...)(T props) if(name != "define" && name != "hasStep") {
static if(workflowName == name) {
return new Workflow!(typeof(this), U)(this, cls);
} else {
return super.opDispatch!name(props);
}
}

bool hasStep(string name)() {
static if(workflowName == name) {
return true;
} else {
return child.hasStep!name;
}
}
}

Expand All @@ -50,11 +96,9 @@ class Workflow(T, U) {
return this;
}

private bool hasStep(string name)() {
static if(!is(U == void*)) {
static if(__traits(hasMember, cls, name)) {
return true;
}
public bool hasStep(string name)() {
static if(!is(U == void*) && __traits(hasMember, cls, name)) {
return true;
} else static if(!is(T == void*)) {
return child.hasStep!name;
} else {
Expand All @@ -63,6 +107,8 @@ class Workflow(T, U) {
}

auto check()() {
logInfo("=> check");

static if(is(T == void*)) {
assert(false, "Can not check void workflows.");
} else static if(!__traits(isSame, TemplateOf!(T), WorkflowCheck)) {
Expand All @@ -72,12 +118,30 @@ class Workflow(T, U) {
}
}

auto opDispatch(string name, T...)(T props) if(name != "define" ) {
auto opDispatch(string name, T...)(T props) if(name != "define" && name != "hasStep") {
enforce(hasStep!name, "The step `" ~ name ~ "` is undefined.");

string stringParams = "";

static if(T.length == 0) {
logInfo("=> " ~ name);
} else {
foreach(prop; props) {
stringParams ~= " " ~ prop.to!string;
}

logInfo("=> " ~ name ~ ":" ~ stringParams);
}

static if(!is(U == void*)) {
static if(__traits(hasMember, cls, name)) {
alias finalProps = AliasSeq!(session, props);
alias expectedParam = Parameters!(__traits(getMember, cls, name));

static if(expectedParam.length == props.length + 1 && is(expectedParam[0] == typeof(session))) {
alias finalProps = AliasSeq!(session, props);
} else {
alias finalProps = props;
}

static if(is(ReturnType!(__traits(getMember, cls, name)) == void)) {
__traits(getMember, cls, name)(finalProps);
Expand All @@ -97,15 +161,13 @@ auto define(immutable SeleniumSession session) {
}

auto define(string name, T, U)(T workflow, U obj) {
return workflow;
return new WorkflowNamed!(name, T, U)(workflow, obj);
}

auto define(T, U)(T workflow, U obj) {
return new Workflow!(T, U)(workflow, obj);
}



@("some workflow examples")
unittest
{
Expand Down Expand Up @@ -134,23 +196,29 @@ unittest
}
}

SeleniumPage productPage = new SeleniumPage();
class ProductPage : SeleniumPage {
this(immutable SeleniumSession session) {
super(session);
}

override bool isPresent() {
return session.findOne("#title".cssLocator).text.indexOf("Maggy London Women's") != -1;
}
}

SeleniumPage productPage = new ProductPage(session);

auto workflow = define(session).define!"productPage"(productPage).define(new Steps);

workflow
.goTo("https://www.amazon.com")
.search("Maggy London Womens")
.opDispatch!"search"("Maggy London Womens")
.selectResultNumber(2)
.check
.productPage
.isPresent;
.opDispatch!"productPage".check.isPresent;

workflow
auto chk = workflow
.goTo("https://www.amazon.com")
.search("Maggy London Womens")
.selectFirstResult
.check
.productPage
.isPresent;
.check.productPage.isPresent;
}

0 comments on commit 8d7c148

Please sign in to comment.