Skip to content

Commit

Permalink
Merge pull request #65 from angrykoala/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
angrykoala authored Feb 23, 2018
2 parents 6591f56 + cf137f5 commit 6394224
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 57 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
0.4.1 / 2018-02-23
==================

* browser.waitUntilNotVisible method
* href and not href assertions
* waitfor now waits until the element is visible
* Fixed bug where url wasn't updated by window history

0.4.0 / 2018-02-21
==================

Expand Down
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,19 @@ Returns the current url of the page
Waits for the given milliseconds.

**waitFor(selector, timeout=500)**
Waits for given css selector to exists, with the given timeout in milliseconds.
Waits for given selector to exists and be visible, with the given timeout in milliseconds.

```js
await browser.waitFor(".popup");
```

**WaitUntilNotVisible(selector, timeout=500)**
Waits until the given selector is no longer visible or doesn't exists, with the given timeout in milliseconds.

```js
await browser.waitUntilNotVisible(".toast");
```

**findByText(text)**
Returns an array with the elements with text content matching the given text.

Expand Down Expand Up @@ -336,15 +343,25 @@ await browser.assert.attribute(".hidden-class", "hidden", null, "hidden-class do

If the element doesn't exists, the assertion will fail.

**style(selector, style, expected, msg)**
**style(selector, style, expected, msg)**
Asserts that the first element matching the given selector has an style with the expected value. The assertion will throw an error if no element is found.

```js
await browser.assert.style("h1", "color", "rgb(0, 0, 0)");
```

**href(selector, expected, msg)**
Asserts that the first element matching the given selector contains an attribute href with expected value.

```js
browser.assert.href("a", "foo.html");
browser.assert.href("link", "styles.css");
```

> Same as `browser.assert.attribute(selector, "href", expected, msg)`
### Negative assertions
Most of the browser assertions have a negative version that can be used with `browser.assert.not`. Most of the behaviours of the "not" assertions are simply the inverse of the positive version.
Most of the browser assertions have a negative version that can be used with `browser.assert.not`. Most of the "not" assertions are simply the inverse of the positive version.

**not.exists(selector, msg)**
Asserts that no element matching given selector exists.
Expand Down Expand Up @@ -386,9 +403,14 @@ await browser.assert.not.attribute(".hidden-class", "href", null, "hidden-class
```
If the element doesn't exists, the assertion will fail.

**not.style(selector, style, expected, msg)**
**not.style(selector, style, expected, msg)**
Asserts the first element matching the selector doesn't has a style with given value.

**href(selector, expected, msg)**
Asserts that the first element matching the given selector doesn't contain an attribute href with the expected value.

> Same as `browser.assert.not.attribute(selector, "href", expected, msg)`
## Examples

**Testing a simple page with Mocha and Wendigo**
Expand Down
3 changes: 3 additions & 0 deletions lib/assertions/browser_assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ module.exports = class BrowserAssertions {
}
assert.strictEqual(value, expected, msg);
});
}

href(selector, expected, msg){
return this.attribute(selector, "href", expected, msg);
}
};
3 changes: 3 additions & 0 deletions lib/assertions/browser_not_assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ module.exports = class BrowserAssertions {
return invertify(() => {
return this._assertions.style(selector, style, expected);
}, msg);
}

href(selector, expected, msg) {
return this.attribute(selector, "href", expected, msg);
}
};
22 changes: 14 additions & 8 deletions lib/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ module.exports = class Browser extends BrowserBase {
this.assert = new BrowserAssertions(this);
}

get frame() {
return this.page.mainFrame();
}

open(url) {
return this.page.goto(url).then(() => {
return this.page.content().then((content) => {
Expand Down Expand Up @@ -72,9 +68,10 @@ module.exports = class Browser extends BrowserBase {
}

url() {
let url = this.page.url();
if(url === "about:blank") url = null;
return Promise.resolve(url);
return this.page.evaluate(() => window.location.href).then((url) => {
if(url === "about:blank") url = null;
return url;
});
}

wait(ms = 250) {
Expand All @@ -86,11 +83,20 @@ module.exports = class Browser extends BrowserBase {
}

waitFor(selector, timeout = 500) {
return this.frame.waitForSelector(selector, {timeout: timeout}).catch(() => {
return this.page.waitForSelector(selector, {timeout: timeout, visible: true}).catch(() => {
return Promise.reject(new Error(`Waiting for element "${selector}" failed, timeout of ${timeout}ms exceeded`));
});
}

waitUntilNotVisible(selector, timeout = 500) {
return this.page.waitForFunction((selector) => {
const element = WendigoUtils.queryElement(selector);
return !WendigoUtils.isVisible(element);
}, {timeout: timeout}, selector).catch(() => {
return Promise.reject(new Error(`Waiting for element "${selector}" not to be visible, timeout of ${timeout}ms exceeded`));
});
}

findByText(text) {
const xPath = `//*[text()='${text}']`;
return this.queryXPath(xPath);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wendigo",
"version": "0.4.0",
"version": "0.4.1",
"description": "A proper monster for front-end testing",
"engines": {
"node": ">=8.9.4"
Expand Down
66 changes: 66 additions & 0 deletions tests/assertions/assert_href.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use strict";

const assert = require('assert');
const Wendigo = require('../../lib/wendigo');
const utils = require('../utils');
const configUrls = require('../config.json').urls;

describe("Assert href", function() {
this.timeout(5000);
let browser;

before(async () => {
browser = await Wendigo.createBrowser();
await browser.open(configUrls.index);
});

after(async() => {
await browser.close();
});

it("Href", async() => {
await browser.assert.href("a", "html_simple.html");
});
it("Href From Node", async() => {
const node = await browser.query("a");
await browser.assert.href(node, "html_simple.html");
});

it("Href Throws", async() => {
assert(browser.assert.href);
await utils.assertThrowsAssertionAsync(async() => {
await browser.assert.href("a", "bad-link");
}, `Expected element "a" to have attribute "href" with value "bad-link", "html_simple.html" found.`);
});

it("Href With Custom Message", async() => {
assert(browser.assert.href);
await utils.assertThrowsAssertionAsync(async() => {
await browser.assert.href("a", "bad-link", "href fails");
}, `href fails`);
});

it("Href On Css Link", async() => {
await browser.assert.href("link", "styles.css");
});

it("Not Href", async() => {
await browser.assert.not.href("a", "not-a-link.html");
});
it("Not Href From Node", async() => {
const node = await browser.query("a");
await browser.assert.not.href(node, "not-a-link.html");
});
it("Not Href Throws", async() => {
assert(browser.assert.not.href);
await utils.assertThrowsAssertionAsync(async() => {
await browser.assert.not.href("a", "html_simple.html");
}, `Expected element "a" not to have attribute "href" with value "html_simple.html".`);
});
it("Not Href With Custom Message", async() => {
assert(browser.assert.not.href);
await utils.assertThrowsAssertionAsync(async() => {
await browser.assert.not.href("a", "html_simple.html", "not href fails");
}, `not href fails`);
});
});
17 changes: 0 additions & 17 deletions tests/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,4 @@ describe("Browser", function() {
const elements = await browser.findByTextContaining("paragraph");
assert.strictEqual(elements.length, 2);
});

it("Url", async() => {
await browser.open(configUrls.index);
assert.strictEqual(await browser.url(), "http://localhost:3456/index.html");
});

it("Changing Url", async() => {
await browser.open(configUrls.index);
assert.strictEqual(await browser.url(), "http://localhost:3456/index.html");
await browser.open(configUrls.click);
assert.strictEqual(await browser.url(), "http://localhost:3456/click.html");
});

it("Url Before Opening", async() => {
const browser2 = await Wendigo.createBrowser();
assert.strictEqual(await browser2.url(), null);
});
});
51 changes: 51 additions & 0 deletions tests/browser/url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

const assert = require('assert');
const Wendigo = require('../../lib/wendigo');
const configUrls = require('../config.json').urls;

describe("Url", function() {
this.timeout(5000);
let browser;

before(async () => {
browser = await Wendigo.createBrowser();
});

after(async() => {
await browser.close();
});


it("Url", async() => {
await browser.open(configUrls.index);
assert.strictEqual(await browser.url(), "http://localhost:3456/index.html");
});

it("Changing Url", async() => {
await browser.open(configUrls.index);
assert.strictEqual(await browser.url(), "http://localhost:3456/index.html");
await browser.open(configUrls.click);
assert.strictEqual(await browser.url(), "http://localhost:3456/click.html");
});

it("Url Before Opening", async() => {
const browser2 = await Wendigo.createBrowser();
assert.strictEqual(await browser2.url(), null);
browser2.close();
});

it("Dynamic Url Update", async() => {
await browser.open(configUrls.url);
assert.strictEqual(await browser.url(), "http://localhost:3456/url_history.html");
await browser.click(".btn");
assert.strictEqual(await browser.url(), "http://localhost:3456/new-url");
});

it("Click Link And Url Update", async() => {
await browser.open(configUrls.index);
await browser.click("a");
await browser.wait();
await browser.assert.url(configUrls.simple);
});
});
57 changes: 57 additions & 0 deletions tests/browser/wait_for.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"use strict";

const Wendigo = require('../../lib/wendigo');
const configUrls = require('../config.json').urls;
const utils = require('../utils');

describe("Wait For", function() {
this.timeout(5000);
let browser;

before(async () => {
browser = await Wendigo.createBrowser();
});

after(async() => {
await browser.close();
});

it("Wait For", async () => {
await browser.open(configUrls.click);
await browser.assert.not.exists("#switch.off");
await browser.assert.exists("#switch.on");
await browser.click(".btn2");
await browser.waitFor("#switch.off", 600);
await browser.assert.exists("#switch.off");
await browser.assert.text("#switch", "Off");
await browser.assert.not.exists("#switch.on");
});

it("Wait For Timeout", async() => {
await browser.open(configUrls.click);
await browser.assert.exists("#switch.on");
await browser.click(".btn2");
await utils.assertThrowsAsync(async () => {
await browser.waitFor("#switch.off", 10);
}, `Error: Waiting for element "#switch.off" failed, timeout of 10ms exceeded`);
await browser.assert.not.exists("#switch.off");
await browser.assert.exists("#switch.on");
await browser.assert.text("#switch", "On");
});

it("Wait For With Existent Element", async () => {
await browser.open(configUrls.click);
await browser.assert.not.exists("#switch.off");
await browser.assert.exists("#switch.on");
await browser.waitFor("#switch.on");
await browser.assert.exists("#switch.on");
});

it("Wait For Fails With Invisible Element", async() => {
await browser.open(configUrls.index);
await browser.assert.exists(".hidden-text2");
await utils.assertThrowsAsync(async () => {
await browser.waitFor(".hidden-text2", 10);
});
});
});
42 changes: 42 additions & 0 deletions tests/browser/wait_until_not_visible.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

const Wendigo = require('../../lib/wendigo');
const configUrls = require('../config.json').urls;
const utils = require('../utils');

describe("Wait Until Not Visible", function() {
this.timeout(5000);
let browser;

before(async () => {
browser = await Wendigo.createBrowser();
});

after(async() => {
await browser.close();
});

it("Wait Until Not Visible", async () => {
await browser.open(configUrls.click);
await browser.click(".btn2");
await browser.waitUntilNotVisible("#switch.on", 600);
await browser.assert.not.exists("#switch.on");
await browser.assert.exists("#switch.off");
});

it("Wait Until Not Visible With Non Existant Element", async () => {
await browser.open(configUrls.click);
await browser.waitUntilNotVisible(".not-exists");
await browser.assert.not.exists(".not-exists");
});

it("Wait Until Not Visible Timeout", async() => {
await browser.open(configUrls.click);
await browser.click(".btn2");
await utils.assertThrowsAsync(async () => {
await browser.waitUntilNotVisible("#switch.on", 10);
}, `Error: Waiting for element "#switch.on" not to be visible, timeout of 10ms exceeded`);
await browser.assert.not.exists("#switch.off");
await browser.assert.exists("#switch.on");
});
});
Loading

0 comments on commit 6394224

Please sign in to comment.