Skip to content

Commit 4a1a0f3

Browse files
committed
Merge pull request #2 from coding-kata/refactoring-page-object
Refactoring test by page object
2 parents cb4eb9b + 5236886 commit 4a1a0f3

File tree

4 files changed

+60
-26
lines changed

4 files changed

+60
-26
lines changed

index.html

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@
1616

1717
<div class="todo">
1818
<h1>Todos</h1>
19-
<form class="todoForm pure-form">
19+
<label>
20+
<input type="checkbox">
21+
</label>
22+
23+
<form class="todoForm pure-form">
2024
<input type="text" class="todoText" placeholder="What needs to be done?">
2125
<input type="submit" class="todoBtn pure-button pure-button-primary" value="Add New">
2226
</form>
2327

2428
<ul class="todoList"></ul>
29+
<label>
30+
<input type="checkbox">
31+
</label>
2532
</div>
2633

2734
</body>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"mocha": "^2.1.0",
3232
"phantomjs": "^1.9.13",
3333
"power-assert": "^0.10.1",
34-
"testium": "^2.3.0",
34+
"testium": "^2.4.0",
3535
"watchify": "^2.2.1"
3636
}
3737
}

test/app-e2e-test.js

+26-24
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,58 @@
22
"use strict";
33
var injectBrowser = require('testium/mocha');
44
var assert = require("power-assert");
5+
var AppPage = require("./page-objects/app-page");
56
var browser;
6-
function addTodo(text) {
7-
browser.setValue('.todoText', text);
8-
browser.click('.todoBtn');
9-
}
107
describe("app-test", function () {
11-
var text = 'todo text';
8+
var inputText = 'todo text';
9+
var page;
1210
before(injectBrowser());
1311
beforeEach(function () {
1412
browser = this.browser;
15-
this.browser.navigateTo("/");
13+
page = new AppPage(this.browser);
1614
});
1715
context("when テキストボックスに文字を入れて送信した時", function () {
1816
beforeEach(function () {
19-
addTodo(text)
17+
page.addTodo(inputText)
2018
});
2119
it("should li要素が作成されている", function () {
22-
var list = browser.getElements('.todoList li');
23-
assert(list.length > 0);
20+
var list = page.getTodoItems();
21+
assert(list.length === 1);
2422
});
2523

2624
it("should リストアイテムのテキストは送信したものと一致している", function () {
27-
browser.assert.elementHasText('.todoList li', text)
25+
var todo = page.getTodoItems()[0];
26+
var text = todo.get("text");
27+
assert.equal(text, inputText);
2828
});
2929
});
30-
describe("todoについて", function () {
30+
describe("todo", function () {
3131
beforeEach(function () {
32-
addTodo(text);
32+
page.addTodo(inputText);
3333
});
34-
context("checkboxをクリックしたら", function () {
35-
it("should `is-complete`が追加される", function () {
36-
browser.click('.todoList li input[type="checkbox"]');
34+
context("when click the checkbox", function () {
35+
it("should added `is-complete`", function () {
36+
var todo = page.getTodoItems()[0];
37+
page.toggleTodo(todo);
3738
browser.assert.elementExists(".is-complete");
3839
});
3940
});
40-
context("removeBtnをクリックして、confirmでキャンセルしても", function () {
41-
it("li要素は消えない", function () {
41+
context("when click removeBtn, then cancel confirm", function () {
42+
it("should have todo item", function () {
43+
var todo = page.getTodoItems()[0];
4244
// confirmがfalseを返すようにする = キャンセル
4345
browser.evaluate("return window.confirm = function() { return " + false + "; };");
44-
45-
browser.click('.todoList li .removeBtn');
46-
browser.assert.elementExists(".todoList li");
46+
page.removeTodo(todo);
47+
assert(page.getTodoItems().length > 0);
4748
});
4849
});
49-
context("removeBtnをクリックしてconfirmでOKしたら", function () {
50-
it("li要素が消える", function () {
50+
context("when click removeBtn, then ok to confirm", function () {
51+
it("should have nottodo item", function () {
52+
var todo = page.getTodoItems()[0];
5153
// confirmがtrueを返すようにする = OK
5254
browser.evaluate("return window.confirm = function() { return " + true + "; };");
53-
browser.click('.todoList li .removeBtn');
54-
browser.assert.elementDoesntExist(".todoList li");
55+
page.removeTodo(todo);
56+
assert(page.getTodoItems().length === 0);
5557
});
5658
});
5759
});

test/page-objects/app-page.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
function AppPage(browser) {
4+
this.browser = browser;
5+
this.browser.navigateTo("/");
6+
}
7+
AppPage.prototype.addTodo = function addTodo(text) {
8+
this.browser.setValue('.todoText', text);
9+
this.browser.click('.todoBtn');
10+
};
11+
AppPage.prototype.getTodoItems = function () {
12+
return this.browser.getElements('.todoList li');
13+
};
14+
/**
15+
* @param todo the todo Element
16+
*/
17+
AppPage.prototype.toggleTodo = function (todo) {
18+
var input = todo.getElement('input[type="checkbox"]');
19+
input.click();
20+
};
21+
AppPage.prototype.removeTodo = function (todo) {
22+
var input = todo.getElement('.removeBtn');
23+
input.click();
24+
};
25+
module.exports = AppPage;

0 commit comments

Comments
 (0)