Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet Intereaction for Disconnecting al the DAPs #5

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 98 additions & 33 deletions commands/keplr.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,50 @@ const keplr = {
};
},

async importWallet(secretWords, password) {
async importWallet(secretWords, password, newAccount) {
await playwright.waitAndClickByText(
onboardingElements.createWalletButton,
await playwright.keplrWindow(),
);
await playwright.waitAndClickByText(
onboardingElements.importRecoveryPhraseButton,
await playwright.keplrWindow(),
);
await playwright.waitAndClickByText(
onboardingElements.useRecoveryPhraseButton,
newAccount
? onboardingElements.createWalletButton
: onboardingElements.existingWalletButton,
await playwright.keplrWindow(),
);

await playwright.waitAndClickByText(
onboardingElements.phraseCount24,
newAccount
? onboardingElements.importRecoveryPhraseButton
: onboardingElements.useRecoveryPhraseButton,
await playwright.keplrWindow(),
);

for (const [index, word] of secretWords.split(' ').entries()) {
await playwright.waitAndTypeByLocator(
onboardingElements.textAreaSelector,
word,
index,
);
}
newAccount &&
(await playwright.waitAndClickByText(
onboardingElements.useRecoveryPhraseButton,
await playwright.keplrWindow(),
));

await playwright.waitAndClick(
onboardingElements.submitPhraseButton,
await playwright.keplrWindow(),
);
if (secretWords.includes(' ')) {
await module.exports.importWalletWithPhrase(secretWords, password);
} else {
await module.exports.importWalletWithPrivateKey(secretWords, password);
}

await playwright.waitAndType(
onboardingElements.walletInput,
onboardingElements.walletName,
);
await playwright.waitAndType(onboardingElements.passwordInput, password);
await playwright.waitAndType(
onboardingElements.confirmPasswordInput,
password,

const passwordFieldExists = await playwright.doesElementExist(
onboardingElements.passwordInput,
);

if (passwordFieldExists) {
await playwright.waitAndType(onboardingElements.passwordInput, password);
await playwright.waitAndType(
onboardingElements.confirmPasswordInput,
password,
);
}

await playwright.waitAndClick(
onboardingElements.submitWalletDataButton,
await playwright.keplrWindow(),
Expand Down Expand Up @@ -105,30 +108,88 @@ const keplr = {

return true;
},
async importWalletWithPhrase(secretWords) {
await playwright.waitAndClickByText(
onboardingElements.phraseCount24,
await playwright.keplrWindow(),
);

for (const [index, word] of secretWords.split(' ').entries()) {
await playwright.waitAndTypeByLocator(
onboardingElements.textAreaSelector,
word,
index,
);
}

await playwright.waitAndClick(
onboardingElements.submitPhraseButton,
await playwright.keplrWindow(),
);
},
async importWalletWithPrivateKey(privateKey) {
await playwright.clickByText(onboardingElements.phrasePrivateKey);

await playwright.waitAndTypeByLocator(
onboardingElements.textAreaSelector,
privateKey,
);

await playwright.waitAndClick(
onboardingElements.submitPhraseButton,
await playwright.keplrWindow(),
);
},
async acceptAccess() {
const notificationPage = await playwright.switchToKeplrNotification();
const result = await playwright.switchToKeplrNotification();

if (
result.err &&
result.message === 'Unable to Switch to Notification Window'
) {
let pages = await playwright.browser().contexts()[0].pages();

for (const page of pages) {
const pageContent = await page.textContent('html');
if (
pageContent.includes('agoric1p2aqakv3ulz4qfy2nut86j9gx0dx0yw09h96md')
) {
return true;
}
}

return false;
}

await playwright.waitAndClick(
notificationPageElements.approveButton,
notificationPage,
result.page,
{ waitForEvent: 'close' },
);
return true;
},

async confirmTransaction() {
const notificationPage = await playwright.switchToKeplrNotification();
const result = await playwright.switchToKeplrNotification();
await playwright.waitAndClick(
notificationPageElements.approveButton,
notificationPage,
result.page,
{ waitForEvent: 'close' },
);
return true;
},

async disconnectWallet() {
await playwright.clickByText(
'Disconnect All',
playwright.keplrPermissionWindow(),
);
return true;
},

async initialSetup(
playwrightInstance,
{ secretWordsOrPrivateKey, password },
{ secretWordsOrPrivateKey, password, newAccount },
) {
if (playwrightInstance) {
await playwright.init(playwrightInstance);
Expand All @@ -139,8 +200,12 @@ const keplr = {
await playwright.assignWindows();
await playwright.assignActiveTabName('keplr');
await module.exports.getExtensionDetails();
await module.exports.importWallet(secretWordsOrPrivateKey, password);
await module.exports.importWallet(
secretWordsOrPrivateKey,
password,
newAccount,
);
},
};

module.exports = keplr;
module.exports = keplr;
64 changes: 53 additions & 11 deletions commands/playwright-keplr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ let browser;
let mainWindow;
let keplrWindow;
let keplrNotificationWindow;
let keplrRegistrationWindow;
let keplrPermissionWindow;
let activeTabName;
let extensionsData = {};
let retries = 0;
Expand All @@ -19,6 +21,8 @@ module.exports = {
keplrWindow = undefined;
activeTabName = undefined;
keplrNotificationWindow = undefined;
keplrRegistrationWindow = undefined;
keplrPermissionWindow = undefined;
retries = 0;
extensionsData = {};
},
Expand All @@ -27,7 +31,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 debuggerDetails = await fetch('http://127.0.0.1:9222/json/version');
const debuggerDetailsConfig = await debuggerDetails.json();
const webSocketDebuggerUrl = debuggerDetailsConfig.webSocketDebuggerUrl;
if (process.env.SLOW_MODE) {
Expand Down Expand Up @@ -77,6 +81,14 @@ module.exports = {
return keplrNotificationWindow;
},

browser() {
return browser;
},

async clickByText(text, page = keplrWindow) {
await page.click(`text="${text}"`);
},

async waitAndClickByText(text, page = keplrWindow) {
await module.exports.waitForByText(text, page);
const element = `:is(:text-is("${text}"), :text("${text}"))`;
Expand Down Expand Up @@ -170,16 +182,13 @@ module.exports = {
},

async waitUntilStable(page) {
const keplrExtensionData = (await module.exports.getExtensionsData())
.keplr;
const keplrExtensionData = (await module.exports.getExtensionsData()).keplr;

if (
page &&
page
.url()
.includes(
`chrome-extension://${keplrExtensionData.id}/register.html`,
)
.includes(`chrome-extension://${keplrExtensionData.id}/register.html`)
) {
await page.waitForLoadState('load');
await page.waitForLoadState('domcontentloaded');
Expand Down Expand Up @@ -216,6 +225,14 @@ module.exports = {
}
return element;
},
async doesElementExist(selector, timeout = 1000, page = keplrWindow) {
try {
await page.waitForSelector(selector, { timeout });
return true;
} catch (error) {
return false;
}
},
async waitForByText(text, page = keplrWindow) {
await module.exports.waitUntilStable(page);
// await page.waitForSelector(selector, { strict: false });
Expand Down Expand Up @@ -347,6 +364,30 @@ module.exports = {
return extensionsData;
},

keplrPermissionWindow() {
return keplrPermissionWindow;
},

async switchToKeplrPermissionWindow() {
const keplrExtensionData = (await module.exports.getExtensionsData()).keplr;
const browserContext = await browser.contexts()[0];
keplrPermissionWindow = await browserContext.newPage();
await keplrPermissionWindow.goto(
`chrome-extension://${keplrExtensionData.id}/popup.html#/setting/security/permission`,
);
return true;
},

async switchToKeplrRegistrationWindow() {
const keplrExtensionData = (await module.exports.getExtensionsData()).keplr;
const browserContext = await browser.contexts()[0];
keplrRegistrationWindow = await browserContext.newPage();
await keplrRegistrationWindow.goto(
`chrome-extension://${keplrExtensionData.id}/register.html`,
);
return true;
},

async switchToKeplrNotification() {
const keplrExtensionData = (await module.exports.getExtensionsData()).keplr;

Expand All @@ -361,7 +402,7 @@ module.exports = {
retries = 0;
await page.bringToFront();
await module.exports.waitUntilStable(page);
return page;
return { page, err: false };
}
}
await sleep(200);
Expand All @@ -370,9 +411,10 @@ module.exports = {
return await module.exports.switchToKeplrNotification();
} else if (retries >= 50) {
retries = 0;
throw new Error(
'[switchToKeplrNotification] Max amount of retries to switch to keplr notification window has been reached. It was never found.',
);
return {
err: true,
message: 'Unable to Switch to Notification Window',
};
}
},
};
};
6 changes: 5 additions & 1 deletion pages/keplr/first-time-flow-page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const createWalletButton = 'Create a new wallet';
const importRecoveryPhraseButton = 'Import existing recovery phrase';
const existingWalletButton = 'Import an existing wallet';
const importRecoveryPhraseButton = 'Import existing recovery phrase';
const useRecoveryPhraseButton = 'Use recovery phrase or private key';
const phraseCount24 = '24 words';
const phrasePrivateKey = 'Private key';
const walletInput = 'input[name="name"]:focus';
const passwordInput = 'input[name="password"]';
const confirmPasswordInput = 'input[name="confirmPassword"]';
Expand All @@ -15,10 +17,12 @@ const textAreaSelector = 'textbox';
const submitPhraseButton = 'button[type="submit"]';

module.exports.onboardingElements = {
existingWalletButton,
createWalletButton,
importRecoveryPhraseButton,
useRecoveryPhraseButton,
phraseCount24,
phrasePrivateKey,
walletInput,
walletName,
passwordInput,
Expand Down
11 changes: 5 additions & 6 deletions plugins/keplr-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,23 @@ module.exports = (on, config) => {
clearWindows: playwright.clearWindows,
isCypressWindowActive: playwright.isCypressWindowActive,
switchToExtensionWindow: playwright.switchToKeplrWindow,
switchToExtensionRegistrationWindow: playwright.switchToKeplrRegistrationWindow,
switchToExtensionPermissionWindow: playwright.switchToKeplrPermissionWindow,

// keplr commands
importWallet: keplr.importWallet,
acceptAccess: keplr.acceptAccess,
confirmTransaction: keplr.confirmTransaction,
disconnectWallet: keplr.disconnectWallet,
setupWallet: async ({
secretWordsOrPrivateKey,
network,
password,
enableAdvancedSettings,
enableExperimentalSettings,
newAccount,
}) => {
await keplr.initialSetup(null, {
secretWordsOrPrivateKey,
network,
password,
enableAdvancedSettings,
enableExperimentalSettings,
newAccount
});
return true;
},
Expand Down
Loading
Loading