html-browser-tester 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -1
- package/lib/browser-test.d.ts +1 -0
- package/lib/browser-test.js +24 -8
- package/lib/expect/assert.js +5 -1
- package/lib/expect/index.js +7 -3
- package/lib/main.js +8 -2
- package/package.json +1 -1
- package/tsconfig.json +1 -1
package/README.md
CHANGED
@@ -57,7 +57,39 @@ const main = async () => {
|
|
57
57
|
const results = await browserTester.run()
|
58
58
|
|
59
59
|
console.log(results)
|
60
|
+
/*
|
61
|
+
[
|
62
|
+
{ description: 'h1,h2 textContent should have right textContent', result: true },
|
63
|
+
{ description: 'title should have right textContent', result: true },
|
64
|
+
{ description: 'h2 should have red text', result: true }
|
65
|
+
]
|
66
|
+
*/
|
60
67
|
}
|
61
68
|
|
62
69
|
main()
|
63
|
-
```
|
70
|
+
```
|
71
|
+
|
72
|
+
### Evaluate
|
73
|
+
|
74
|
+
You can also evaluate template literals to run tests
|
75
|
+
|
76
|
+
```js
|
77
|
+
browserTest.evaluate(`
|
78
|
+
test('h1,h2 textContent should have right textContent', async (_, doc) => {
|
79
|
+
const h1 = doc.querySelector('h1')
|
80
|
+
const h2 = doc.querySelector('h2')
|
81
|
+
expect(h1?.textContent).toBe('Title1')
|
82
|
+
expect(h2?.textContent).toBe('Title2')
|
83
|
+
})
|
84
|
+
|
85
|
+
test('title should have right textContent', async (_, doc) => {
|
86
|
+
const title = doc.querySelector('title')
|
87
|
+
expect(title?.textContent).toBe('Hello')
|
88
|
+
})
|
89
|
+
|
90
|
+
test('h2 should have red text', async (window, doc) => {
|
91
|
+
const h2 = doc.querySelector('h2')
|
92
|
+
expect(window.getComputedStyle(h2).color).toBe('rgb(255, 0, 0)')
|
93
|
+
})
|
94
|
+
`)
|
95
|
+
```
|
package/lib/browser-test.d.ts
CHANGED
package/lib/browser-test.js
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.BrowserTester = void 0;
|
4
|
+
const expect_1 = require("./expect");
|
5
|
+
class BrowserTester {
|
3
6
|
html = '';
|
4
7
|
width;
|
5
8
|
height;
|
6
9
|
iframe;
|
7
10
|
tests = [];
|
8
|
-
expects = new Expect();
|
11
|
+
expects = new expect_1.Expect();
|
9
12
|
beforeEachCallbacks = [];
|
10
13
|
afterEachCallbacks = [];
|
11
14
|
constructor({ html = '', width, height, }) {
|
@@ -43,6 +46,10 @@ export class BrowserTester {
|
|
43
46
|
clearTests() {
|
44
47
|
this.tests = [];
|
45
48
|
}
|
49
|
+
evaluate(code) {
|
50
|
+
const func = new Function('test', 'expect', 'beforeEach', 'afterEach', code);
|
51
|
+
func(this.test.bind(this), this.expect.bind(this), this.beforeEach.bind(this), this.afterEach.bind(this));
|
52
|
+
}
|
46
53
|
run() {
|
47
54
|
return new Promise((resolve) => {
|
48
55
|
const blob = new Blob([this.html], { type: "text/html" });
|
@@ -63,12 +70,20 @@ export class BrowserTester {
|
|
63
70
|
for (const b of this.beforeEachCallbacks) {
|
64
71
|
await b(iframe.contentWindow, iframe.contentDocument);
|
65
72
|
}
|
66
|
-
await t.callback(iframe.contentWindow, iframe.contentDocument);
|
67
73
|
const { description } = t;
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
74
|
+
try {
|
75
|
+
await t.callback(iframe.contentWindow, iframe.contentDocument);
|
76
|
+
results.push({
|
77
|
+
description,
|
78
|
+
result: this.expects.isAllPassed()
|
79
|
+
});
|
80
|
+
}
|
81
|
+
catch (e) {
|
82
|
+
results.push({
|
83
|
+
description,
|
84
|
+
result: false,
|
85
|
+
});
|
86
|
+
}
|
72
87
|
this.expects.clean();
|
73
88
|
for (const a of this.afterEachCallbacks) {
|
74
89
|
await a(iframe.contentWindow, iframe.contentDocument);
|
@@ -84,3 +99,4 @@ export class BrowserTester {
|
|
84
99
|
});
|
85
100
|
}
|
86
101
|
}
|
102
|
+
exports.BrowserTester = BrowserTester;
|
package/lib/expect/assert.js
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.assert = void 0;
|
4
|
+
const assert = (expected) => ({
|
2
5
|
toBe: (resut) => {
|
3
6
|
return expected === resut;
|
4
7
|
},
|
@@ -60,3 +63,4 @@ export const assert = (expected) => ({
|
|
60
63
|
return false;
|
61
64
|
},
|
62
65
|
});
|
66
|
+
exports.assert = assert;
|
package/lib/expect/index.js
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Expect = void 0;
|
4
|
+
const assert_1 = require("./assert");
|
5
|
+
class Expect {
|
3
6
|
expects = [];
|
4
7
|
expect(expected) {
|
5
|
-
const assertedObject = assert(expected);
|
8
|
+
const assertedObject = (0, assert_1.assert)(expected);
|
6
9
|
const returnObject = {
|
7
10
|
not: {},
|
8
11
|
};
|
@@ -25,3 +28,4 @@ export class Expect {
|
|
25
28
|
return this.expects.every(e => e);
|
26
29
|
}
|
27
30
|
}
|
31
|
+
exports.Expect = Expect;
|
package/lib/main.js
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const browser_test_1 = require("./browser-test");
|
2
4
|
const html = `
|
3
5
|
<!DOCTYPE html>
|
4
6
|
<html lang="ja">
|
@@ -20,7 +22,7 @@ const html = `
|
|
20
22
|
</html>
|
21
23
|
`;
|
22
24
|
const main = async () => {
|
23
|
-
const browserTest = new BrowserTester({ html, width: 980, height: 980 });
|
25
|
+
const browserTest = new browser_test_1.BrowserTester({ html, width: 980, height: 980 });
|
24
26
|
browserTest.test('h1,h2 textContent should have right textContent', async (_, doc) => {
|
25
27
|
const h1 = doc.querySelector('h1');
|
26
28
|
const h2 = doc.querySelector('h2');
|
@@ -35,6 +37,10 @@ const main = async () => {
|
|
35
37
|
const h2 = doc.querySelector('h2');
|
36
38
|
browserTest.expect(window.getComputedStyle(h2).color).toBe('rgb(255, 0, 0)');
|
37
39
|
});
|
40
|
+
// browserTest.test('can insert text to h5', async (window, doc) => {
|
41
|
+
// const h5 = doc.querySelector('h5') as HTMLHeadingElement
|
42
|
+
// h5.textContent = 'aaaa'
|
43
|
+
// })
|
38
44
|
const results = await browserTest.run();
|
39
45
|
console.log(results);
|
40
46
|
};
|
package/package.json
CHANGED