-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathquote-page.ts
More file actions
401 lines (339 loc) · 12.7 KB
/
quote-page.ts
File metadata and controls
401 lines (339 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import { strict as assert } from 'assert';
import { Hex } from '@metamask/utils';
import { Key } from 'selenium-webdriver';
import { toAssetId } from '../../../../../shared/lib/asset-utils';
import { ASSET_ROUTE } from '../../../../../shared/lib/deep-links/routes/route';
import { toChecksumHexAddress } from '../../../../../shared/lib/hexstring-utils';
import { Driver } from '../../../webdriver/driver';
import TokenOverviewPage from '../token-overview-page';
export type BridgeQuote = {
amount: string;
tokenFrom?: string;
tokenTo?: string;
fromChain?: string;
toChain?: string;
unapproved?: boolean;
};
class BridgeQuotePage {
protected driver: Driver;
public sourceAssetPickerButton = '[data-testid="bridge-source-button"]';
public destinationAssetPickerButton =
'[data-testid="bridge-destination-button"]';
private mutlichainAssetPicker =
'[data-testid="multichain-asset-picker__network"]';
public assetPrickerSearchInput =
'[data-testid="bridge-asset-picker-search-input"]';
private sourceAmount = '[data-testid="from-amount"]';
private destinationAmount = '[data-testid="to-amount"]';
private lineaNetwork = '[data-testid="Linea"]';
public tokenButton = '[data-testid^="bridge-asset--"]';
private submitButton = '[data-testid="bridge-cta-button"]';
private insufficientFundsButton = {
text: 'Insufficient funds',
css: '[data-testid="bridge-cta-button"]',
};
private backButton = '[aria-label="Back"]';
private gasIncludedIndicator = '[data-testid="network-fees-included"]';
private gasSponsoredIndicator = '[data-testid="network-fees-sponsored"]';
private maxButton = { text: 'Max' };
private networkSelector = '[data-testid="multichain-asset-picker__network"]';
private networkFees = '[data-testid="network-fees"]';
private applyButton = { text: 'Apply', tag: 'button' };
private confirmButton =
'[data-testid="confirm-sign-and-send-transaction-confirm-snap-footer-button"]';
private noOptionAvailable = '[data-testid="bridge-no-options-available"]';
private moreETHneededForGas =
'[data-testid="bridge-insufficient-gas-for-quote"]';
private switchTokensButton = '[data-testid="switch-tokens"]';
private slippageEditButton = '[data-testid="slippage-edit-button"]';
private slippageCustomButton =
'[data-testid="bridge__tx-settings-modal-custom-button"]';
private slippageCustomInput =
'input[data-testid="bridge__tx-settings-modal-custom-input"]';
private networkNameSelector = (network: string) =>
`[data-testid="${network}"]`;
constructor(driver: Driver) {
this.driver = driver;
}
/**
* Checks that the bridge quote page is loaded.
*
* @param timeout - Optional timeout in milliseconds. Defaults to 10000.
*/
async checkPageIsLoaded(timeout: number = 10000): Promise<void> {
try {
await this.driver.waitForSelector(this.sourceAssetPickerButton, {
timeout,
});
} catch (e) {
console.log(
'Timeout while waiting for bridge quote page to be loaded',
e,
);
throw e;
}
console.log('Bridge quote page is loaded');
}
enterBridgeQuote = async (quote: BridgeQuote) => {
// Source
if (quote.tokenFrom || quote.fromChain) {
await this.driver.clickElement(this.sourceAssetPickerButton);
if (quote.fromChain) {
await this.driver.clickElement(this.networkSelector);
await this.driver.clickElement(`[data-testid="${quote.fromChain}"]`);
}
if (quote.tokenFrom) {
await this.driver.fill(this.assetPrickerSearchInput, quote.tokenFrom);
await this.driver.clickElement({
text: quote.tokenFrom,
css: this.tokenButton,
});
}
}
// Destination
if (quote.tokenTo || quote.toChain) {
await this.driver.waitForSelector(this.destinationAssetPickerButton);
await this.driver.clickElement(this.destinationAssetPickerButton);
// After clicking destination, we might see either:
// 1. Network selection modal (if destination is pre-populated and different from desired network)
// 2. Token picker with network badge (if destination is empty or on the correct network)
if (quote.toChain) {
// We're in token picker, need to click network badge first
await this.driver.waitForSelector(this.networkSelector);
await this.driver.clickElement(this.networkSelector);
// Now select the destination network
await this.driver.clickElementAndWaitToDisappear({
text: quote.toChain,
});
}
if (quote.tokenTo) {
await this.driver.fill(this.assetPrickerSearchInput, quote.tokenTo);
await this.driver.clickElementAndWaitToDisappear({
text: quote.tokenTo,
css: this.tokenButton,
});
}
}
// QTY
await this.driver.fill(this.sourceAmount, quote.amount);
await this.driver.assertElementNotPresent(
{
tag: 'p',
text: 'Fetching quotes...',
},
{ waitAtLeastGuard: 500 },
);
};
searchForAsset = async (
token: string,
assetPicker = this.sourceAssetPickerButton,
) => {
console.log(`Opening asset picker`);
await this.driver.clickElement(assetPicker);
await this.driver.fill(this.assetPrickerSearchInput, token);
console.log(`Filled search input with ${token}`);
const assetElement = await this.driver.findElement({
css: this.tokenButton,
text: token,
});
return assetElement;
};
goToAssetPage = async (
token: string,
chainId: Hex,
address: string,
assetPicker = this.sourceAssetPickerButton,
) => {
const expectedAssetId = toAssetId(address, chainId)?.toLowerCase();
const expectedUrl = `${ASSET_ROUTE}/${chainId}/${encodeURIComponent(toChecksumHexAddress(address))}`;
console.log(`Opening asset picker`);
await this.driver.clickElement(assetPicker);
await this.driver.fill(this.assetPrickerSearchInput, token);
console.log(`Filled search input with ${token}`);
const assetElement = await this.driver.findElement({
tag: 'button',
testId: `bridge-asset-info-icon-${expectedAssetId}`,
});
console.log(`Clicked link to the asset page`);
await assetElement.click();
await this.driver.waitForUrlContaining({
url: expectedUrl,
});
const assetPage = new TokenOverviewPage(this.driver);
await assetPage.checkPageIsLoaded();
};
checkAssetsAreSelected = async (sourceToken: string, destToken: string) => {
await this.driver.waitForSelector({
css: this.sourceAssetPickerButton,
text: sourceToken,
});
console.log(`Expected source asset ${sourceToken} is selected`);
await this.driver.waitForSelector({
css: this.destinationAssetPickerButton,
text: destToken,
});
console.log(`Expected dest asset ${destToken} is selected`);
};
checkAssetPickerModalIsReopened = async () => {
await this.driver.waitForSelector({
testId: 'bridge-asset-picker-modal',
});
console.log('Asset picker modal is visible');
await this.driver.clickElementAndWaitToDisappear('[aria-label="Close"]');
console.log('Asset picker modal closed');
};
waitForQuote = async () => {
await this.driver.waitForSelector(this.submitButton, { timeout: 30000 });
};
submitQuote = async () => {
await this.driver.clickElement(this.submitButton);
};
confirmBridgeTransaction = async () => {
await this.driver.clickElement(this.confirmButton);
};
goBack = async () => {
await this.driver.waitForSelector(this.backButton);
await this.driver.clickElement(this.backButton);
};
async searchAssetAndVerifyCount(
searchInput: string,
count: number,
): Promise<void> {
console.log(`Fill search input with ${searchInput}`);
await this.driver.pasteIntoField(this.assetPrickerSearchInput, searchInput);
await this.driver.elementCountBecomesN(this.tokenButton, count);
}
async checkTokenIsDisabled() {
const [tkn] = await this.driver.findElements(this.tokenButton);
await tkn.click();
const isSelected = await tkn.isSelected();
assert.equal(isSelected, false);
}
async checkNoTradeRouteMessageIsDisplayed(): Promise<void> {
try {
await this.driver.waitForSelector(this.noOptionAvailable);
} catch (e) {
console.log(
`Expected message that "no trade route is available" is not present`,
);
throw e;
}
console.log('The message "no trade route is available" is displayed');
}
async checkInsufficientFundsButtonIsDisplayed(): Promise<void> {
try {
await this.driver.waitForSelector(this.insufficientFundsButton);
} catch (e) {
console.log(`Expected button "Insufficient funds" is not present`);
throw e;
}
console.log('The button "Insufficient funds" is displayed');
}
async checkMoreETHneededIsDisplayed(): Promise<void> {
try {
await this.driver.waitForSelector(this.moreETHneededForGas);
} catch (e) {
console.log(
`Expected message that "More ETH needed for gas" is not present`,
);
throw e;
}
console.log('The message "More ETH needed for gas" is displayed');
}
async checkExpectedNetworkFeeIsDisplayed(): Promise<void> {
try {
const balance = await this.driver.waitForSelector(this.networkFees);
const currentBalanceText = await balance.getText();
// Verify that the text matches the pattern $XXX.XX
const pricePattern = /^\$\d+\.\d{2}$/u;
if (!pricePattern.test(currentBalanceText)) {
throw new Error(`Price format is not valid: ${currentBalanceText}`);
}
} catch (e: unknown) {
console.log(
`Error checking price format: ${
e instanceof Error ? e.message : String(e)
}`,
);
throw e;
}
console.log('Price matches expected format');
}
async checkGasIncludedIsDisplayed(): Promise<void> {
try {
await this.driver.waitForSelector(this.gasIncludedIndicator, {
timeout: 30000,
});
} catch (e) {
console.log('Expected "Gas fees included" indicator is not present');
throw e;
}
console.log('Gas fees included indicator is displayed');
}
async checkGasSponsoredIsDisplayed(): Promise<void> {
try {
await this.driver.waitForSelector(this.gasSponsoredIndicator, {
timeout: 30000,
});
} catch (e) {
console.log('Expected "Gas fees sponsored" indicator is not present');
throw e;
}
console.log('Gas fees sponsored indicator is displayed');
}
async clickMaxButton(): Promise<void> {
await this.driver.waitForSelector(this.maxButton, { timeout: 30000 });
await this.driver.clickElement(this.maxButton);
console.log('Clicked Max button');
}
checkDestAmount = async (amount: string) => {
const destAmount = await this.driver.findElement(this.destinationAmount);
assert.equal(await destAmount.getAttribute('value'), amount);
};
async switchTokens(): Promise<void> {
await this.driver.clickElement(this.switchTokensButton);
}
async checkTokenRiskWarningIsDisplayed(
title: string,
description: string,
): Promise<void> {
await this.driver.waitForSelector({ text: title }, { timeout: 30000 });
await this.driver.waitForSelector({ text: description });
}
async setCustomSlippage(value: string): Promise<void> {
await this.driver.clickElement(this.slippageEditButton);
await this.driver.clickElement(this.slippageCustomButton);
const input = await this.driver.waitForSelector(this.slippageCustomInput, {
timeout: 1000,
});
await input.sendKeys(Key.BACK_SPACE);
await this.driver.fill(this.slippageCustomInput, value);
await this.driver.executeScript(`
const input = document.querySelector('${this.slippageCustomInput}');
if (input) { input.blur(); }
`);
}
async selectSrcToken(token: string): Promise<void> {
await this.driver.waitForSelector(this.sourceAssetPickerButton);
await this.driver.clickElement(this.sourceAssetPickerButton);
await this.driver.fill(this.assetPrickerSearchInput, token);
await this.driver.clickElementAndWaitToDisappear({
text: token,
css: this.tokenButton,
});
}
async selectDestToken(token: string): Promise<void> {
await this.driver.waitForSelector(this.destinationAssetPickerButton);
await this.driver.clickElement(this.destinationAssetPickerButton);
await this.driver.fill(this.assetPrickerSearchInput, token);
await this.driver.clickElementAndWaitToDisappear({
text: token,
css: this.tokenButton,
});
}
async selectNetwork(network: string): Promise<void> {
await this.driver.clickElement(this.networkSelector);
await this.driver.clickElement(this.networkNameSelector(network));
}
}
export default BridgeQuotePage;