Skip to content

Commit

Permalink
chore: provider leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
maximgeerinck committed Mar 30, 2023
1 parent 3a929c1 commit 8a2214a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 31 deletions.
3 changes: 2 additions & 1 deletion commands/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,14 +1305,15 @@ const metamask = {
password,
enableAdvancedSettings,
enableExperimentalSettings,
playwrightInstance,
}) {
const isCustomNetwork =
(process.env.NETWORK_NAME &&
process.env.RPC_URL &&
process.env.CHAIN_ID) ||
typeof network == 'object';

await playwright.init();
await playwright.init(playwrightInstance);
await playwright.assignWindows(PROVIDER);
await playwright.assignActiveTabName(PROVIDER);
await module.exports.getExtensionDetails();
Expand Down
23 changes: 14 additions & 9 deletions commands/phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ module.exports = {
);

// STEP: Input password, confirm and continue
await new Promise(resolve => setTimeout(resolve, 1000)); // the transitioning is too fast
await playwright.waitAndType(
PROVIDER,
firstTimeFlowImportPageElements.passwordInput,
Expand All @@ -166,13 +167,13 @@ module.exports = {
firstTimeFlowImportPageElements.continueAfterPasswordButton,
);
// shortcut confirmation
await new Promise(resolve => setTimeout(resolve, 500)); // the transitioning is too fast
await new Promise(resolve => setTimeout(resolve, 1000)); // the transitioning is too fast
await playwright.waitAndClick(
PROVIDER,
firstTimeFlowImportPageElements.continueOnShortcutConfirm,
);
// finish
await new Promise(resolve => setTimeout(resolve, 500)); // the transitioning is too fast
await new Promise(resolve => setTimeout(resolve, 1000)); // the transitioning is too fast
await playwright.waitAndClick(
PROVIDER,
firstTimeFlowImportPageElements.continueOnShortcutConfirm,
Expand Down Expand Up @@ -232,8 +233,12 @@ module.exports = {
await switchToCypressIfNotActive();
return walletAddress;
},
initialSetup: async ({ secretWordsOrPrivateKey, password }) => {
await playwright.init();
initialSetup: async ({
secretWordsOrPrivateKey,
password,
playwrightInstance,
}) => {
await playwright.init(playwrightInstance);
await playwright.assignWindows(PROVIDER);
await playwright.assignActiveTabName(PROVIDER);
await module.exports.getExtensionDetails();
Expand Down Expand Up @@ -378,19 +383,19 @@ module.exports = {
},
selectWallet: async (wallet = 'metamask', mode = 'once') => {
const notificationPage = await playwright.switchToNotification(PROVIDER);
if (wallet === 'metamask') {

if (mode === 'always') {
await playwright.waitAndClick(
PROVIDER,
selectWalletElements.buttons.continueWithMetamask,
selectWalletElements.buttons.alwaysUse,
notificationPage,
);
return true;
}

if (mode === 'always') {
if (wallet === 'metamask') {
await playwright.waitAndClick(
PROVIDER,
selectWalletElements.buttons.alwaysUsePhantom,
selectWalletElements.buttons.continueWithMetamask,
notificationPage,
);
return true;
Expand Down
61 changes: 42 additions & 19 deletions commands/playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
const chromium = playwrightInstance
? playwrightInstance
: require('@playwright/test').chromium;

const debuggerDetails = await fetch('http://127.0.0.1:9222/json/version'); //DevSkim: ignore DS137138
const debuggerDetailsConfig = await debuggerDetails.json();
const webSocketDebuggerUrl = debuggerDetailsConfig.webSocketDebuggerUrl;
Expand Down Expand Up @@ -127,29 +128,30 @@ module.exports = {
return true;
},
switchToWindow: async provider => {
if (pageWindows[provider].isClosed()) {
if (module.exports.windows(provider).isClosed()) {
const newPage = await browser.contexts()[0].newPage();
if (provider === 'phantom') {
await Promise.all([
newPage.waitForNavigation(),
newPage.goto(
pageWindows[provider]
module.exports
.windows(provider)
.url()
.replace('onboarding.html', 'popup.html'),
),
]);
} else {
await Promise.all([
newPage.waitForNavigation(),
newPage.goto(pageWindows[provider].url()),
newPage.goto(module.exports.windows(provider).url()),
]);
await newPage.waitUntilStable(provider);
}

pageWindows[provider] = newPage;
}

await pageWindows[provider].bringToFront();
await module.exports.windows(provider).bringToFront();
await module.exports.assignActiveTabName(provider);
return true;
},
Expand All @@ -168,7 +170,9 @@ module.exports = {
notificationWindows[provider] = page;
retries = 0;
await page.bringToFront();
await module.exports.waitUntilStable(provider, page);
if (provider == 'metamask') {
await module.exports.waitUntilStable(provider, page);
}
if (provider === 'phantom') {
await module.exports.waitFor(provider, app.root, page);
} else {
Expand All @@ -193,8 +197,10 @@ module.exports = {
);
}
},
async waitFor(provider, selector, page = pageWindows[provider]) {
await module.exports.waitUntilStable(provider, page);
async waitFor(provider, selector, page = module.exports.windows(provider)) {
if (provider == 'metamask') {
await module.exports.waitUntilStable(provider, page);
}
await page.waitForSelector(selector, { strict: false });
const element = page.locator(selector).first();
await element.waitFor();
Expand All @@ -211,7 +217,7 @@ module.exports = {
async waitAndClick(
provider,
selector,
page = pageWindows[provider],
page = module.exports.windows(provider),
args = {},
) {
const element = await module.exports.waitFor(provider, selector, page);
Expand Down Expand Up @@ -240,7 +246,7 @@ module.exports = {
} else {
await element.click({ force: args.force });
}
if (process.env.PROVIDER !== 'phantom') {
if (provider === 'metamask') {
await module.exports.waitUntilStable(provider);
}
return element;
Expand All @@ -249,19 +255,32 @@ module.exports = {
provider,
selector,
text,
page = pageWindows[provider],
page = module.exports.windows(provider),
) {
await module.exports.waitFor(provider, selector, page);
const element = page.locator(`text=${text}`);
await element.click();
await module.exports.waitUntilStable(provider);
if (provider === 'metamask') {
await module.exports.waitUntilStable(provider);
}
},
async waitAndType(provider, selector, value, page = pageWindows[provider]) {
async waitAndType(
provider,
selector,
value,
page = module.exports.windows(provider),
) {
const element = await module.exports.waitFor(provider, selector, page);
await element.type(value);
await module.exports.waitUntilStable(provider, page);
if (provider === 'metamask') {
await module.exports.waitUntilStable(provider, page);
}
},
async waitAndGetValue(provider, selector, page = pageWindows[provider]) {
async waitAndGetValue(
provider,
selector,
page = module.exports.windows(provider),
) {
const expect = global.expect
? global.expect
: require('@playwright/test').expect;
Expand All @@ -273,7 +292,11 @@ module.exports = {
const value = await element.innerText();
return value;
},
async waitAndGetInputValue(provider, selector, page = pageWindows[provider]) {
async waitAndGetInputValue(
provider,
selector,
page = module.exports.windows(provider),
) {
const expect = global.expect
? global.expect
: require('@playwright/test').expect;
Expand All @@ -286,7 +309,7 @@ module.exports = {
provider,
selector,
attribute,
page = pageWindows[provider],
page = module.exports.windows(provider),
) {
const expect = global.expect
? global.expect
Expand All @@ -300,7 +323,7 @@ module.exports = {
provider,
text,
selector,
page = pageWindows[provider],
page = module.exports.windows(provider),
) {
const element = await module.exports.waitFor(provider, selector, page);
await element.fill('');
Expand All @@ -311,7 +334,7 @@ module.exports = {
async waitAndClearWithBackspace(
provider,
selector,
page = pageWindows[provider],
page = module.exports.windows(provider),
) {
await module.exports.waitFor(provider, selector, page);
const inputValue = await page.evaluate(selector, el => el.value);
Expand All @@ -324,7 +347,7 @@ module.exports = {
provider,
text,
selector,
page = pageWindows[provider],
page = module.exports.windows(provider),
) {
const element = await module.exports.waitAndClick(
provider,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@phantom/synpress",
"version": "4.0.0-alpha.1",
"version": "4.0.0-alpha.6",
"description": "Synpress is e2e testing framework based around Cypress.io & playwright with included MetaMask support. Test your dapps with ease.",
"keywords": [
"Synpress",
Expand Down
2 changes: 1 addition & 1 deletion pages/phantom/select-wallet-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports.selectWalletElements = {
buttons: {
continueWithPhantom: '[data-testid="select_wallet--phantom"]',
continueWithMetamask: '[data-testid="select_wallet--metamask"]',
alwaysUsePhantom: '[data-testid="metamask_override-always_use_phantom"]',
alwaysUse: '[data-testid="select_wallet--always"]',
},
};

0 comments on commit 8a2214a

Please sign in to comment.