Skip to content

Commit

Permalink
Added FakerJS to address tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shayfaber committed Jan 22, 2025
1 parent 52bef10 commit 8a41fbc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 47 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"license": "ISC",
"description": "",
"devDependencies": {
"@faker-js/faker": "^9.4.0",
"@playwright/test": "^1.47.2",
"@types/node": "^22.7.4"
},
Expand Down
39 changes: 10 additions & 29 deletions tests/base/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,7 @@ test.describe('Account address book actions', { annotation: {type: 'Account Dash
testInfo.skip(await addNewAddressTitle.isHidden(), `Heading "Add New Addres" is not found, please check if an address has already been added.`);
const accountPage = new AccountPage(page);

let phoneNumberValue = inputvalues.firstAddress.firstPhoneNumberValue;
let addressValue = inputvalues.firstAddress.firstStreetAddressValue;
let zipCodeValue = inputvalues.firstAddress.firstZipCodeValue;
let cityNameValue = inputvalues.firstAddress.firstCityValue;
let stateValue = inputvalues.firstAddress.firstProvinceValue;

await accountPage.addNewAddress(phoneNumberValue, addressValue, zipCodeValue, cityNameValue, stateValue);

await accountPage.addNewAddress();
});

/**
Expand All @@ -118,17 +111,8 @@ test.describe('Account address book actions', { annotation: {type: 'Account Dash
*/
test('I can add another address',{ tag: '@address-actions', }, async ({page}) => {
await page.goto(slugs.account.addressNewSlug);

const accountPage = new AccountPage(page);

let phoneNumberValue = inputvalues.secondAddress.secondPhoneNumberValue;
let addressValue = inputvalues.secondAddress.secondStreetAddressValue;
let zipCodeValue = inputvalues.secondAddress.secondZipCodeValue;
let cityNameValue = inputvalues.secondAddress.secondCityValue;
let stateValue = inputvalues.secondAddress.secondProvinceValue;

await accountPage.addNewAddress(phoneNumberValue, addressValue, zipCodeValue, cityNameValue, stateValue);

await accountPage.addNewAddress();
});

/**
Expand All @@ -143,21 +127,18 @@ test.describe('Account address book actions', { annotation: {type: 'Account Dash
* @then I should see a notification my address has been updated.
* @and The updated address should be visible in the addres book page.
*/
test('I can edit an existing address',{ tag: '@address-actions', }, async ({page}, testInfo) => {
test('I can edit an existing address',{ tag: '@address-actions', }, async ({page}) => {
const accountPage = new AccountPage(page);
let newFirstName = inputvalues.editedAddress.editfirstNameValue;
let newLastName = inputvalues.editedAddress.editLastNameValue;
let newStreet = inputvalues.editedAddress.editStreetAddressValue;
let newZipCode = inputvalues.editedAddress.editZipCodeValue;
let newCity = inputvalues.editedAddress.editCityValue;
let newState = inputvalues.editedAddress.editStateValue;

await page.goto(slugs.account.addressNewSlug);
let editAddressButton = page.getByRole('link', {name: selectors.accountDashboard.editAddressIconButton}).first();
testInfo.skip(await editAddressButton.isHidden(), `Button to edit Address is not found, please check if an address has been added.`);

await page.goto(slugs.account.addressBookSlug);
await accountPage.editExistingAddress(newFirstName, newLastName, newStreet, newZipCode, newCity, newState);
if(await editAddressButton.isHidden()){
// The edit address button was not found, add another address first.
await accountPage.addNewAddress();
}

await page.goto(slugs.account.addressBookSlug);
await accountPage.editExistingAddress();
});

/**
Expand Down
37 changes: 19 additions & 18 deletions tests/base/fixtures/account.page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, type Locator, type Page} from '@playwright/test';
import {faker} from '@faker-js/faker';

import selectors from '../config/selectors/selectors.json';
import verify from '../config/expected/expected.json';
Expand Down Expand Up @@ -39,7 +40,7 @@ export class AccountPage {
*/

// TODO: Update these functionalities to be able to take in non-required fields.

constructor(page: Page){
this.page = page;
this.accountDashboardTitle = page.getByRole('heading', { name: selectors.accountDashboard.accountDashboardTitleLabel });
Expand Down Expand Up @@ -79,49 +80,49 @@ export class AccountPage {
this.editAddressButton = page.getByRole('link', {name: selectors.accountDashboard.editAddressIconButton}).first();
}

//TODO: Add ability to choose different country other than US
async addNewAddress(phonenumber: string,streetName: string, zipCode: string, cityName: string, state: string){
async addNewAddress(){
let addressAddedNotification = verify.address.newAddressAddedNotifcation;

let streetName = faker.location.streetAddress();

// Name should be filled in automatically.
await expect(this.firstNameField).not.toBeEmpty();
await expect(this.lastNameField).not.toBeEmpty();

await this.phoneNumberField.fill(phonenumber);
await this.phoneNumberField.fill(faker.phone.number());
await this.streetAddressField.fill(streetName);
await this.zipCodeField.fill(zipCode);
await this.cityField.fill(cityName);
await this.stateSelectorField.selectOption(state);
await this.zipCodeField.fill(faker.location.zipCode());
await this.cityField.fill(faker.location.city());
await this.stateSelectorField.selectOption(faker.location.state());
await this.saveAddressButton.click();
await this.page.waitForLoadState();

await expect(this.page.getByText(addressAddedNotification)).toBeVisible();
await expect.soft(this.page.getByText(addressAddedNotification)).toBeVisible();
await expect(this.page.getByText(streetName).last()).toBeVisible();
}


async editExistingAddress(firstName: string, lastName: string, streetName: string, zipCode: string, cityName: string, state: string){
async editExistingAddress(){
// the notification for a modified address is the same as the notification for a new address.
let addressModifiedNotification = verify.address.newAddressAddedNotifcation;
let streetName = faker.location.streetAddress();

// .click() replaced by .press("Enter") as a workaround for webkit issues
await this.editAddressButton.click();

// Name should be filled in automatically, but editable.
await expect(this.firstNameField).not.toBeEmpty();
await expect(this.lastNameField).not.toBeEmpty();

await this.firstNameField.fill(firstName);
await this.lastNameField.fill(lastName);
await this.firstNameField.fill(faker.person.firstName());
await this.lastNameField.fill(faker.person.lastName());
await this.streetAddressField.fill(streetName);
await this.zipCodeField.fill(zipCode);
await this.cityField.fill(cityName);
await this.stateSelectorField.selectOption(state);
// .click() replaced by .press("Enter") as a workaround for webkit issues
await this.zipCodeField.fill(faker.location.zipCode());
await this.cityField.fill(faker.location.city());
await this.stateSelectorField.selectOption(faker.location.state());

await this.saveAddressButton.click();
await this.page.waitForLoadState();

await expect(this.page.getByText(addressModifiedNotification)).toBeVisible();
await expect.soft(this.page.getByText(addressModifiedNotification)).toBeVisible();
await expect(this.page.getByText(streetName).last()).toBeVisible();
}

Expand Down

0 comments on commit 8a41fbc

Please sign in to comment.