MetaMask / MetaMask/metamask-extension

Programmatically reloading extension

Open
#19,385 1 comment 0 reactions 0 assignees View on GitHub
team-extension-platform
Dominant language
TypeScript
Stars
13.2k
Forks
5.6k
Avg merge
2d 5h
Merged PRs (30d)
451

Description

At the moment we rely on manipulating the `chrome://extensions/` page DOM. This is suboptimal because there could be changes in the future that would break any tests relying on reloading chrome extensions in the future.

More importantly, this makes the test chrome specific, but we'll need to support and test for MV3 for Firefox in the not too distant future.

#### One approach we tried

We used the following approach but `browser.runtime.reload();` didn't cause the SW to restart.

On `test/e2e/helpers.js`

```js
async checkBrowserForEvents() {
const cdpConnection = await this.driver.createCDPConnection('page');

await this.driver.onLogEvent(cdpConnection, (event) => {
event.args.forEach((eventArg) => {
console.log(`[log event]: ${JSON.stringify(eventLog)}`);

// we should probably use `eventArg.value` here so that `waitForLogEvent`
// works properly, or change the implementation there.
this.events.push(eventArg);
});
});
}
```

On `test/e2e/helpers.js`

```js
async waitForLogEvent(eventName, timeout = this.timeout) {
await this.wait(this.events.includes(eventName), timeout);
}
```

On the test:

```js
async function reloadProgramatically(driver) {
await switchToWindow(driver, WINDOW_TITLES.ExtensionInFullScreenView);
await driver.executeScript('window.stateHooks.refreshBrowser()');
await driver.waitForLogEvent('Service Worker Restarted');
await driver.executeScript('window.location.reload()');
}
```

On `test/e2e/helpers.js`

```js
await driver.checkBrowserForEvents();
```

On `ui/index.js`

```js
window.stateHooks.refreshBrowser = function () {
if (process.env.IN_TEST) {
browser.runtime.reload();
}
};
```

Contributor guide

Open the contributing guide

Research direction

Start in test/e2e/helpers.js and ui/index.js, reviewing the existing checkBrowserForEvents, waitForLogEvent, and stateHooks.refreshBrowser code. Compare the browser.runtime.reload approach with the current chrome://extensions/ DOM manipulation, including the Service Worker Restarted event. Done means extension reload tests no longer depend on the Chrome extensions page and can support the stated Firefox MV3 direction.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
testing-qa
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.