Skip to content

Commit bc866c9

Browse files
committed
Add e2e tests
1 parent cd12846 commit bc866c9

File tree

9 files changed

+151
-22
lines changed

9 files changed

+151
-22
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function TextAppenderUtil(elementId) {
2+
this.elementID = elementId;
3+
}
4+
5+
TextAppenderUtil.prototype.appendText = function(textToAppend) {
6+
document.getElementById(this.elementID).innerText += textToAppend;
7+
};

package-lock.json

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@angular/platform-browser": "~8.2.8",
2020
"@angular/platform-browser-dynamic": "~8.2.8",
2121
"@angular/router": "~8.2.8",
22+
"lodash": "^4.17.15",
2223
"rxjs": "~6.4.0",
2324
"tslib": "^1.10.0",
2425
"zone.js": "~0.9.1"
@@ -29,9 +30,10 @@
2930
"@angular/cli": "~8.3.6",
3031
"@angular/compiler-cli": "~8.2.8",
3132
"@angular/language-service": "~8.2.8",
32-
"@types/node": "~8.9.4",
3333
"@types/jasmine": "~3.3.8",
3434
"@types/jasminewd2": "~2.0.3",
35+
"@types/lodash": "^4.14.149",
36+
"@types/node": "~8.9.4",
3537
"codelyzer": "^5.0.0",
3638
"jasmine-core": "~3.4.0",
3739
"jasmine-spec-reporter": "~4.2.1",

projects/ng-lazyload-script-example/e2e/src/app.e2e-spec.ts

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {AppPage} from './app.po';
22
import {browser, ExpectedConditions, logging} from 'protractor';
3+
import { uniq } from 'lodash';
34

45
describe('example app', () => {
56
let page: AppPage;
@@ -8,21 +9,87 @@ describe('example app', () => {
89
page = new AppPage();
910
});
1011

11-
it('should eventually change text', () => {
12+
it('should eventually change text (one click)', () => {
1213
page.navigateTo();
1314

14-
expect(page.getTextBox().getText()).toEqual('A new text should appear here...');
15+
expect(page.getChangerTextBox().getText()).toEqual('A new text should appear here...');
1516

16-
page.clickButton();
17+
page.clickTextChangerButton();
1718

1819
browser.wait(
1920
ExpectedConditions.textToBePresentInElement(
20-
page.getTextBox(),
21+
page.getChangerTextBox(),
2122
'A new text (set by text changer util which was lazy loaded)'),
2223
10000);
2324
});
2425

26+
it('should eventually append text twice (two immediate clicks)', () => {
27+
page.navigateTo();
28+
29+
expect(page.getAppenderTextBox().getText()).toEqual('Text should be appended here...');
30+
31+
page.clickTextAppenderButton();
32+
page.clickTextAppenderButton();
33+
34+
browser.wait(
35+
ExpectedConditions.textToBePresentInElement(
36+
page.getAppenderTextBox(),
37+
'Text should be appended here...appended textappended text'),
38+
10000);
39+
});
40+
41+
it('should eventually append text twice (two clicks with wait between)', () => {
42+
page.navigateTo();
43+
44+
expect(page.getAppenderTextBox().getText()).toEqual('Text should be appended here...');
45+
46+
page.clickTextAppenderButton();
47+
48+
browser.wait(
49+
ExpectedConditions.textToBePresentInElement(
50+
page.getAppenderTextBox(),
51+
'Text should be appended here...appended text'),
52+
10000);
53+
54+
page.clickTextAppenderButton();
55+
56+
browser.wait(
57+
ExpectedConditions.textToBePresentInElement(
58+
page.getAppenderTextBox(),
59+
'Text should be appended here...appended textappended text'),
60+
10000);
61+
});
62+
63+
it('should eventually change text and eventually append text', () => {
64+
page.navigateTo();
65+
66+
expect(page.getAppenderTextBox().getText()).toEqual('Text should be appended here...');
67+
68+
page.clickTextAppenderButton();
69+
page.clickTextChangerButton();
70+
71+
browser.wait(
72+
ExpectedConditions.textToBePresentInElement(
73+
page.getChangerTextBox(),
74+
'A new text (set by text changer util which was lazy loaded)'),
75+
10000);
76+
77+
browser.wait(
78+
ExpectedConditions.textToBePresentInElement(
79+
page.getAppenderTextBox(),
80+
'Text should be appended here...appended text'),
81+
10000);
82+
});
83+
2584
afterEach(async () => {
85+
// Check that there are no duplicate script tags
86+
const allScriptTags = page.getAllScriptTags();
87+
88+
const allScriptSources = await allScriptTags.getAttribute('src');
89+
const uniqueScriptSources = uniq(allScriptSources);
90+
91+
expect(allScriptSources.length).toBe(uniqueScriptSources.length);
92+
2693
// Assert that there are no errors emitted from the browser
2794
const logs = await browser.manage().logs().get(logging.Type.BROWSER);
2895
expect(logs).not.toContain(jasmine.objectContaining({
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
import { browser, by, element, WebElementPromise } from 'protractor';
1+
import {browser, by, element} from 'protractor';
22

33
export class AppPage {
44
navigateTo() {
55
return browser.get(browser.baseUrl) as Promise<any>;
66
}
77

8-
getTextBox() {
9-
return element(by.id('text-box'));
8+
getChangerTextBox() {
9+
return element(by.id('text-box-text-changer'));
1010
}
1111

12-
clickButton() {
13-
return element(by.id('button')).click();
12+
getAppenderTextBox() {
13+
return element(by.id('text-box-text-appender'));
14+
}
15+
16+
clickTextChangerButton() {
17+
return element(by.id('textChangerButton')).click();
18+
}
19+
20+
clickTextAppenderButton() {
21+
return element(by.id('textAppenderButton')).click();
22+
}
23+
24+
getAllScriptTags() {
25+
return element.all(by.tagName('script'));
1426
}
1527
}

projects/ng-lazyload-script-example/src/app/app.component.html

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@ <h1>
33
</h1>
44

55
<h2>
6-
Text box
6+
Lazy loaded text changer script
77
</h2>
8-
<p id="text-box">
8+
<p>
9+
As soon as this button is clicked, an external script is loaded on the fly and the loaded
10+
library is used to change the text of the green text box.
11+
</p>
12+
13+
<p id="text-box-text-changer" class="text-box green">
914
A new text should appear here...
1015
</p>
1116

17+
<button type="button" id="textChangerButton" (click)="loadTextChangerAndChangeText()">
18+
Load script and change text
19+
</button>
20+
1221
<h2>
13-
Lazy load script button
22+
Lazy loaded text appender script
1423
</h2>
1524
<p>
1625
As soon as this button is clicked, an external script is loaded on the fly and the loaded
17-
library is used to change the text of the text box.
26+
library is used to append text to the blue text box.
1827
</p>
19-
<button type="button" id="button" (click)="loadScript()">
20-
Load script and change text
28+
29+
<p id="text-box-text-appender" class="text-box blue">
30+
Text should be appended here...
31+
</p>
32+
33+
34+
<button type="button" id="textAppenderButton" (click)="loadTextAppenderAndChangeText()">
35+
Load script and append text
2136
</button>
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
#text-box {
2-
border: 1px solid black;
1+
.text-box {
2+
border: 2px solid black;
33
padding: 5px;
44
}
5+
6+
.green {
7+
border-color: green;
8+
}
9+
10+
.blue {
11+
border-color: blue;
12+
}

projects/ng-lazyload-script-example/src/app/app.component.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ export class AppComponent implements OnInit {
1414
ngOnInit() {
1515
}
1616

17-
loadScript() {
17+
loadTextChangerAndChangeText() {
1818
// Load the external script on the fly
1919
this.ngLazyScriptService.loadScript('http://ng-lazy-script.uapps.ch/text-changer-util.js').subscribe(() => {
20-
const textChangerUtil = new TextChangerUtil('text-box');
20+
const textChangerUtil = new TextChangerUtil('text-box-text-changer');
2121
textChangerUtil.changeText('A new text (set by text changer util which was lazy loaded)');
2222
});
2323
}
24+
25+
loadTextAppenderAndChangeText() {
26+
// Load the external script on the fly
27+
this.ngLazyScriptService.loadScript('http://ng-lazy-script.uapps.ch/text-appender-util.js').subscribe(() => {
28+
const textChangerUtil = new TextAppenderUtil('text-box-text-appender');
29+
textChangerUtil.appendText('appended text');
30+
});
31+
}
2432
}
2533

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare class TextAppenderUtil {
2+
constructor(elementId: string);
3+
4+
appendText(newText: string);
5+
}

0 commit comments

Comments
 (0)