Skip to content

Commit 48295ec

Browse files
syntax cleanup
1 parent d523f71 commit 48295ec

File tree

3 files changed

+88
-89
lines changed

3 files changed

+88
-89
lines changed

autotests/pages/startPage.ts

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, type Locator, type Page} from '@playwright/test';
1+
import { expect, type Locator, type Page } from '@playwright/test';
22

33
export class StartPage {
44
//readonly page: Page;
@@ -7,15 +7,16 @@ export class StartPage {
77
readonly allupperButtons: Locator;
88
readonly manageTokenButton: Locator;
99
readonly allTokensNames: Locator;
10-
1110

1211
constructor(readonly page: Page) {
13-
// this.page = page;
12+
// this.page = page;
1413
//expect(page.url()).toContain("options.html");
1514
this.balance = page.locator('div.BalanceContainer-sc-1ks78fh > p');
1615
this.tokens = page.locator('h1.CoinBalanceText-sc-wr88cs');
1716
this.allupperButtons = page.locator('div.RowButtonContainer-sc-1cgcddu > div');
18-
this.manageTokenButton = page.locator('button, input[type="button"] >> text="Manage token list"');
17+
this.manageTokenButton = page.locator(
18+
'button, input[type="button"] >> text="Manage token list"',
19+
);
1920
this.allTokensNames = page.locator('h1.SubText-sc-18704il');
2021
}
2122

@@ -25,41 +26,45 @@ export class StartPage {
2526
await expect(this.balance).toBeVisible();
2627
await expect(this.manageTokenButton).toBeVisible();
2728
// Check if all 4 buttons (send, receive, swap, buy) are visible
28-
// TODO : create more stable selector for all 4 buttons to be able to check all 4 indivudially
29+
// TODO : create more stable selector for all 4 buttons to be able to check all 4 individually
2930
await expect(this.allupperButtons).toHaveCount(4);
3031
}
3132

3233
async getBalancefloat(): Promise<number> {
33-
//Check balance
34-
const Balance = await this.balance.textContent();
35-
const cleaned_balance = Balance.replace(/[^\d.-]/g, '');
36-
const balanceFloat = parseFloat(cleaned_balance);
37-
return balanceFloat
34+
const Balance = await this.balance.textContent();
35+
if (Balance !== null) {
36+
const cleaned_balance = Balance.replace(/[^\d.-]/g, '');
37+
const balanceFloat = parseFloat(cleaned_balance);
38+
return balanceFloat;
39+
} else {
40+
// Handle the case where Balance is null
41+
console.error('Failed to retrieve balance text content');
42+
return 0;
43+
}
3844
}
3945

4046
async getBalancefloat_fromAllTokens(): Promise<number> {
41-
// Get the count of elements matching the locator
4247
const count = await this.tokens.count();
4348
// Initialize a variable to hold the sum of all amounts
4449
let totalSum = 0;
4550
// Iterate through all found elements
4651
for (let i = 0; i < count; i++) {
4752
// Get the amount from the text
4853
const balanceToken = await this.tokens.nth(i).textContent();
54+
if (balanceToken !== null) {
55+
// Use a regular expression to remove non-numeric characters except the decimal point as the format is "$ 0.00 USD"
56+
const cleanedText = balanceToken.replace(/[^\d.-]/g, '');
57+
const amountFloat = parseFloat(cleanedText);
4958

50-
// Use a regular expression to remove non-numeric characters except the decimal point as the format is "$ 0.00 USD"
51-
const cleanedText = balanceToken.replace(/[^\d.-]/g, '');
52-
53-
// Parse the cleaned text as a float
54-
const amountFloat = parseFloat(cleanedText);
55-
56-
// Check if the parsed value is a valid number before adding to the total sum
57-
if (!isNaN(amountFloat)) {
58-
totalSum += amountFloat;
59+
// Check if the parsed value is a valid number before adding to the total sum
60+
if (!isNaN(amountFloat)) {
61+
totalSum += amountFloat;
62+
}
63+
} else {
64+
// Handle the case where Balance is null
65+
console.error('Failed to retrieve balance text content');
5966
}
6067
}
61-
return totalSum
62-
}
63-
64-
68+
return totalSum;
69+
}
6570
}

autotests/pages/token.ts

+42-44
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
import { expect, type Locator, type Page} from '@playwright/test';
1+
import { expect, type Locator, type Page } from '@playwright/test';
22

33
export class Token {
4-
//readonly page: Page;
54
readonly allTokenImg: Locator;
65
readonly allUnselectedToken: Locator;
76
readonly allSelectedToken: Locator;
87
readonly backButton: Locator;
9-
108

119
constructor(readonly page: Page) {
12-
// this.page = page;
13-
//expect(page.url()).toContain("options.html");
1410
this.allTokenImg = page.locator('div > div.TokenContainer-sc-19ldcpb > div img');
1511
this.allUnselectedToken = page.locator('h1.UnSelectedCoinTitleText-sc-1kql8uo');
1612
this.allSelectedToken = page.locator('h1.SelectedCoinTitleText-sc-hkryj5');
1713
this.backButton = page.locator('img[alt="back button"]');
1814
}
1915

2016
async checkVisuals(selected: number, unselected: number) {
21-
await expect(this.page.url()).toContain("manage-tokens");
22-
17+
await expect(this.page.url()).toContain('manage-tokens');
18+
2319
//check if all token provider are displayed
2420
// TODO check if that number of token provider is fixed or instable and might need to change this check
2521
await expect(this.allTokenImg).toHaveCount(13);
@@ -28,44 +24,46 @@ export class Token {
2824
}
2925

3026
async enableARandomToken(): Promise<string> {
31-
// Same number of unselected_tokens as switch toggle to them
32-
const switch_toggle = this.page.locator('div.RowContainer-sc-nvec8f:has(h1.UnSelectedCoinTitleText-sc-1kql8uo) div.react-switch-handle');
33-
const number_of_unselected_tokens = await this.allUnselectedToken.count();
34-
35-
// Generate a random number within the range of available select elements
36-
const chosen_number = Math.floor(Math.random() * number_of_unselected_tokens) + 1;
37-
38-
// Access the nth select element (note the adjustment for zero-based indexing)
39-
const adjust_chosen_number = chosen_number - 1
40-
const chosen_switch_toggle = switch_toggle.nth(adjust_chosen_number);
41-
const chosen_unselected_token = this.allUnselectedToken.nth(adjust_chosen_number);
42-
const enabled_token_name = (await chosen_unselected_token.innerText()).toString()
43-
// Example action on the chosen select element
44-
// This could be clicking it, selecting an option, etc., depending on your needs
45-
await chosen_switch_toggle.click();
46-
await expect (this.allSelectedToken.getByText(enabled_token_name)).toBeVisible();
47-
return enabled_token_name
48-
}
27+
// Same number of unselected_tokens as switch toggle to them
28+
const switch_toggle = this.page.locator(
29+
'div.RowContainer-sc-nvec8f:has(h1.UnSelectedCoinTitleText-sc-1kql8uo) div.react-switch-handle',
30+
);
31+
const number_of_unselected_tokens = await this.allUnselectedToken.count();
4932

50-
async disableARandomToken(): Promise<string> {
51-
// Same number of unselected_tokens as switch toggle to them
52-
const switch_toggle = this.page.locator('div.RowContainer-sc-nvec8f:has(h1.SelectedCoinTitleText-sc-hkryj5) div.react-switch-handle');
53-
const number_of_selected_tokens = await this.allSelectedToken.count();
54-
55-
// Generate a random number within the range of available select elements
56-
const chosen_number = Math.floor(Math.random() * number_of_selected_tokens) + 1;
57-
58-
// Access the nth select element (note the adjustment for zero-based indexing)
59-
const adjust_chosen_number = chosen_number - 1
60-
const chosen_switch_toggle = switch_toggle.nth(adjust_chosen_number);
61-
const chosen_selected_token = this.allSelectedToken.nth(adjust_chosen_number);
62-
const disabled_token_name = (await chosen_selected_token.innerText()).toString()
63-
// Example action on the chosen select element
64-
// This could be clicking it, selecting an option, etc., depending on your needs
65-
await chosen_switch_toggle.click();
66-
await expect (this.allUnselectedToken.getByText(disabled_token_name)).toBeVisible();
67-
return disabled_token_name
68-
}
33+
// Generate a random number within the range of available select elements
34+
const chosen_number = Math.floor(Math.random() * number_of_unselected_tokens) + 1;
6935

36+
// Access the nth select element (note the adjustment for zero-based indexing)
37+
const adjust_chosen_number = chosen_number - 1;
38+
const chosen_switch_toggle = switch_toggle.nth(adjust_chosen_number);
39+
const chosen_unselected_token = this.allUnselectedToken.nth(adjust_chosen_number);
40+
const enabled_token_name = (await chosen_unselected_token.innerText()).toString();
41+
// Example action on the chosen select element
42+
// This could be clicking it, selecting an option, etc., depending on your needs
43+
await chosen_switch_toggle.click();
44+
await expect(this.allSelectedToken.getByText(enabled_token_name)).toBeVisible();
45+
return enabled_token_name;
46+
}
7047

48+
async disableARandomToken(): Promise<string> {
49+
// Same number of unselected_tokens as switch toggle to them
50+
const switch_toggle = this.page.locator(
51+
'div.RowContainer-sc-nvec8f:has(h1.SelectedCoinTitleText-sc-hkryj5) div.react-switch-handle',
52+
);
53+
const number_of_selected_tokens = await this.allSelectedToken.count();
54+
55+
// Generate a random number within the range of available select elements
56+
const chosen_number = Math.floor(Math.random() * number_of_selected_tokens) + 1;
57+
58+
// Access the nth select element (note the adjustment for zero-based indexing)
59+
const adjust_chosen_number = chosen_number - 1;
60+
const chosen_switch_toggle = switch_toggle.nth(adjust_chosen_number);
61+
const chosen_selected_token = this.allSelectedToken.nth(adjust_chosen_number);
62+
const disabled_token_name = (await chosen_selected_token.innerText()).toString();
63+
// Example action on the chosen select element
64+
// This could be clicking it, selecting an option, etc., depending on your needs
65+
await chosen_switch_toggle.click();
66+
await expect(this.allUnselectedToken.getByText(disabled_token_name)).toBeVisible();
67+
return disabled_token_name;
68+
}
7169
}

autotests/tests/manageTokens.spec.ts

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { expect, test } from '../fixtures/base';
22
import { data } from '../fixtures/data';
33
import Onboarding from '../pages/onboarding';
4-
import {StartPage} from '../pages/startPage';
5-
import {Token} from '../pages/token';
4+
import { StartPage } from '../pages/startPage';
5+
import { Token } from '../pages/token';
66

77
//Main Issue with this testcase is that it depends currently on the successfull creation of a wallet (code for that was copied from onboarding.spec.ts)
88
// TODO add either a mock for a created wallet or put creating of a wallet into a pageObject
99
test.describe('Manage Tokens', () => {
1010
let onboarding: Onboarding;
11-
//let startPage: StartPage;
1211
test.beforeEach(async ({ page, extensionId }) => {
1312
await page.goto(`chrome-extension://${extensionId}/popup.html`);
1413
});
@@ -46,52 +45,49 @@ test.describe('Manage Tokens', () => {
4645
await page.goto(`chrome-extension://${extensionId}/options.html`);
4746
let startPage = new StartPage(page);
4847
// Variable to check the changed balance after enable and disable a token
49-
let balanceFloat = await startPage.getBalancefloat();;
50-
let balanceTokensFloat = await startPage.getBalancefloat_fromAllTokens();
48+
let balanceFloat = await startPage.getBalancefloat();
49+
let balanceTokensFloat = await startPage.getBalancefloat_fromAllTokens();
5150
await startPage.checkVisuals();
5251
await test.step('manage Tokens', async () => {
5352
await startPage.manageTokenButton.click();
5453
let token = new Token(page);
55-
await token.checkVisuals(0,13);
54+
await token.checkVisuals(0, 13);
5655

57-
await test.step('enable a random Tokens', async () => {
56+
await test.step('enable a random Tokens', async () => {
5857
// TODO add a check if unselected tokens are avaible to enable a token better
5958
const enabled_token_name = await token.enableARandomToken();
6059
await token.backButton.click();
6160
await startPage.checkVisuals();
62-
await expect (startPage.allTokensNames.getByText(enabled_token_name)).toBeVisible();
63-
64-
const newBalanceTokensFloat = await startPage.getBalancefloat_fromAllTokens();
61+
await expect(startPage.allTokensNames.getByText(enabled_token_name)).toBeVisible();
62+
63+
const newBalanceTokensFloat = await startPage.getBalancefloat_fromAllTokens();
6564
const newBalanceFloat = await startPage.getBalancefloat();
6665
await expect(newBalanceFloat).toEqual(newBalanceTokensFloat);
67-
const differenceInBalance = balanceTokensFloat + newBalanceTokensFloat
66+
const differenceInBalance = balanceTokensFloat + newBalanceTokensFloat;
6867
await expect(newBalanceFloat).toEqual(balanceFloat + differenceInBalance);
6968
});
7069

71-
72-
await test.step('disable a random Tokens', async () => {
70+
await test.step('disable a random Tokens', async () => {
7371
// when switching from startpage to manage token the app had sometimes the problem with the status change and didn't show the enabled button, with adding this addiotnoial visual check the test got more stable be adding one more check
7472
// TODO figure out why app doesn't show enabled token in the fast switch --> potential bug !!!!
7573
// TODO change the check as the number only works if that was the frist token to be added
76-
await expect (startPage.allTokensNames).toHaveCount(3);
77-
// disable a token
74+
await expect(startPage.allTokensNames).toHaveCount(3);
7875
await startPage.manageTokenButton.click();
79-
await token.checkVisuals(1,12);
76+
await token.checkVisuals(1, 12);
8077

8178
const disabled_token_name = await token.disableARandomToken();
8279
await token.backButton.click();
8380
await expect(startPage.balance).toBeVisible();
84-
await expect (startPage.allTokensNames.getByText(disabled_token_name)).toBeHidden();
81+
await expect(startPage.allTokensNames.getByText(disabled_token_name)).toBeHidden();
8582

8683
// Check if the new Balance is correct and compare with the original values
87-
const newBalanceTokensFloat = await startPage.getBalancefloat_fromAllTokens();
84+
const newBalanceTokensFloat = await startPage.getBalancefloat_fromAllTokens();
8885
const newBalanceFloat = await startPage.getBalancefloat();
8986

9087
await expect(newBalanceFloat).toEqual(newBalanceTokensFloat);
91-
const differenceInBalance = balanceTokensFloat - newBalanceTokensFloat
88+
const differenceInBalance = balanceTokensFloat - newBalanceTokensFloat;
9289
await expect(newBalanceFloat).toEqual(balanceFloat - differenceInBalance);
9390
});
94-
9591
});
9692
});
97-
});
93+
});

0 commit comments

Comments
 (0)