html-browser-tester 0.0.6 → 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 +17 -5
- package/lib/main.js +4 -0
- package/package.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
@@ -46,6 +46,10 @@ class BrowserTester {
|
|
46
46
|
clearTests() {
|
47
47
|
this.tests = [];
|
48
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
|
+
}
|
49
53
|
run() {
|
50
54
|
return new Promise((resolve) => {
|
51
55
|
const blob = new Blob([this.html], { type: "text/html" });
|
@@ -66,12 +70,20 @@ class BrowserTester {
|
|
66
70
|
for (const b of this.beforeEachCallbacks) {
|
67
71
|
await b(iframe.contentWindow, iframe.contentDocument);
|
68
72
|
}
|
69
|
-
await t.callback(iframe.contentWindow, iframe.contentDocument);
|
70
73
|
const { description } = t;
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
+
}
|
75
87
|
this.expects.clean();
|
76
88
|
for (const a of this.afterEachCallbacks) {
|
77
89
|
await a(iframe.contentWindow, iframe.contentDocument);
|
package/lib/main.js
CHANGED
@@ -37,6 +37,10 @@ const main = async () => {
|
|
37
37
|
const h2 = doc.querySelector('h2');
|
38
38
|
browserTest.expect(window.getComputedStyle(h2).color).toBe('rgb(255, 0, 0)');
|
39
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
|
+
// })
|
40
44
|
const results = await browserTest.run();
|
41
45
|
console.log(results);
|
42
46
|
};
|