|
| 1 | +import assert from 'assert/strict'; |
| 2 | +import { Browser } from 'selenium-webdriver'; |
| 3 | +import { withFixtures } from './helpers'; |
| 4 | +import { errorMessages } from './webdriver/driver'; |
| 5 | +import StartOnboardingPage from './page-objects/pages/onboarding/start-onboarding-page'; |
| 6 | +import { hasProperty, isObject } from '@metamask/utils'; |
| 7 | + |
| 8 | +// Window handle adjustments will need to be made for Non-MV3 Firefox |
| 9 | +// due to OffscreenDocument. |
| 10 | +const IS_FIREFOX = process.env.SELENIUM_BROWSER === Browser.FIREFOX; |
| 11 | + |
| 12 | +// The number of windows to expect to be open |
| 13 | +const baseWindows = |
| 14 | + 1 + // The primary window, starts out on extensions page |
| 15 | + (IS_FIREFOX ? 0 : 1); // The offscreen document, only on Chrome/MV3 |
| 16 | + |
| 17 | +describe('First install', function () { |
| 18 | + it('opens new window upon install, but not on subsequent reloads', async function () { |
| 19 | + await withFixtures( |
| 20 | + { |
| 21 | + disableServerMochaToBackground: true, |
| 22 | + }, |
| 23 | + async ({ driver }) => { |
| 24 | + // Wait for MetaMask to automatically open a new tab |
| 25 | + await driver.waitUntilXWindowHandles(baseWindows + 1); |
| 26 | + |
| 27 | + // We cannot use customized driver functions related to window handles, as they depend upon |
| 28 | + // the background websocket connection enabled only for E2E test builds. This test runs on |
| 29 | + // a production-like build, which doesn't have this websocket connection. |
| 30 | + let windowHandles = await driver.driver.getAllWindowHandles(); |
| 31 | + |
| 32 | + // Switch to new tab and verify it's the start onboarding page |
| 33 | + await driver.driver.switchTo().window(windowHandles[baseWindows]); |
| 34 | + const startOnboardingPage = new StartOnboardingPage(driver); |
| 35 | + await startOnboardingPage.check_pageIsLoaded(); |
| 36 | + |
| 37 | + await driver.executeScript('window.stateHooks.reloadExtension()'); |
| 38 | + |
| 39 | + // Wait for extension to reload, signified by the onboarding tab closing |
| 40 | + await driver.waitUntilXWindowHandles(baseWindows); |
| 41 | + |
| 42 | + // Test to see if it re-opens |
| 43 | + try { |
| 44 | + await driver.waitUntilXWindowHandles(baseWindows + 1); |
| 45 | + } catch (error) { |
| 46 | + if ( |
| 47 | + isObject(error) && |
| 48 | + hasProperty(error, 'message') && |
| 49 | + typeof error.message === 'string' && |
| 50 | + error.message.startsWith( |
| 51 | + errorMessages.waitUntilXWindowHandlesTimeout, |
| 52 | + ) |
| 53 | + ) { |
| 54 | + // Ignore timeout error, it's expected here in the success case |
| 55 | + console.log('Onboarding tab not opened'); |
| 56 | + } else { |
| 57 | + throw error; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Ensure new tab is not open |
| 62 | + windowHandles = await driver.driver.getAllWindowHandles(); |
| 63 | + assert.equal( |
| 64 | + windowHandles.length, |
| 65 | + baseWindows, |
| 66 | + `Expected ${baseWindows} windows, found ${windowHandles.length}`, |
| 67 | + ); |
| 68 | + }, |
| 69 | + ); |
| 70 | + }); |
| 71 | +}); |
0 commit comments