Skip to content

Commit 78264d2

Browse files
committed
test(browser): cover document-start inpage injection
1 parent 60c9ba3 commit 78264d2

4 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>Document Start Injection Test Dapp</title>
5+
<script>
6+
window.__mmEthereumAtFirstInlineScript =
7+
typeof window.ethereum !== 'undefined';
8+
9+
if (!window.__mmEthereumAtFirstInlineScript) {
10+
throw new Error('window.ethereum should be available synchronously');
11+
}
12+
</script>
13+
</head>
14+
<body>
15+
<main>
16+
<h1>Document Start Injection Test Dapp</h1>
17+
<p id="document-start-status">Waiting for result</p>
18+
</main>
19+
<script>
20+
const status = document.getElementById('document-start-status');
21+
22+
if (window.__mmEthereumAtFirstInlineScript) {
23+
status.textContent =
24+
'window.ethereum was available before the first inline script completed';
25+
} else {
26+
status.textContent =
27+
'window.ethereum was missing before the first inline script completed';
28+
}
29+
</script>
30+
</body>
31+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { BrowserViewSelectorsIDs } from '../../../app/components/Views/BrowserTab/BrowserView.testIds';
2+
import { Assertions, Matchers } from '../../framework';
3+
import {
4+
DocumentStartDappSelectorsText,
5+
DocumentStartDappSelectorsWebIDs,
6+
} from '../../selectors/Browser/DocumentStartDapp.selectors';
7+
8+
class DocumentStartDapp {
9+
get documentStartStatus(): WebElement {
10+
return Matchers.getElementByWebID(
11+
BrowserViewSelectorsIDs.BROWSER_WEBVIEW_ID,
12+
DocumentStartDappSelectorsWebIDs.DOCUMENT_START_STATUS,
13+
);
14+
}
15+
16+
async expectEthereumAvailableBeforeFirstInlineScript(): Promise<void> {
17+
await Assertions.expectElementToContainText(
18+
this.documentStartStatus,
19+
DocumentStartDappSelectorsText.ETHEREUM_AVAILABLE_BEFORE_INLINE_SCRIPT,
20+
{
21+
description:
22+
'window.ethereum should be available before the first inline script completes',
23+
},
24+
);
25+
}
26+
}
27+
28+
export default new DocumentStartDapp();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const DocumentStartDappSelectorsWebIDs = {
2+
DOCUMENT_START_STATUS: 'document-start-status',
3+
} as const;
4+
5+
export const DocumentStartDappSelectorsText = {
6+
ETHEREUM_AVAILABLE_BEFORE_INLINE_SCRIPT:
7+
'window.ethereum was available before the first inline script completed',
8+
} as const;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// eslint-disable-next-line import-x/no-nodejs-modules
2+
import path from 'path';
3+
import { SmokeBrowser } from '../../../tags.js';
4+
import FixtureBuilder from '../../../framework/fixtures/FixtureBuilder';
5+
import { withFixtures } from '../../../framework/fixtures/FixtureHelper';
6+
import { DappVariants } from '../../../framework/Constants';
7+
import { getDappUrl } from '../../../framework/fixtures/FixtureUtils';
8+
import { loginToApp } from '../../../flows/wallet.flow';
9+
import { navigateToBrowserView } from '../../../flows/browser.flow';
10+
import Browser from '../../../page-objects/Browser/BrowserView';
11+
import DocumentStartDapp from '../../../page-objects/Browser/DocumentStartDapp';
12+
13+
const INPAGE_INJECTION_FIXTURES_PATH = path.resolve(
14+
__dirname,
15+
'../../../fixtures/inpage-injection',
16+
);
17+
18+
describe(SmokeBrowser('Browser Inpage Injection'), () => {
19+
beforeEach(() => {
20+
jest.setTimeout(150000);
21+
});
22+
23+
it('injects window.ethereum before the first dapp script runs', async () => {
24+
await withFixtures(
25+
{
26+
dapps: [
27+
{
28+
dappVariant: DappVariants.TEST_DAPP,
29+
dappPath: INPAGE_INJECTION_FIXTURES_PATH,
30+
},
31+
],
32+
fixture: new FixtureBuilder().build(),
33+
restartDevice: true,
34+
},
35+
async () => {
36+
await loginToApp();
37+
await navigateToBrowserView();
38+
39+
await Browser.tapUrlInputBox();
40+
await Browser.navigateToURL(`${getDappUrl(0)}/document-start.html`);
41+
42+
await DocumentStartDapp.expectEthereumAvailableBeforeFirstInlineScript();
43+
},
44+
);
45+
});
46+
});

0 commit comments

Comments
 (0)