Skip to content

Commit

Permalink
feat: refactor registration task for guest users (#267)
Browse files Browse the repository at this point in the history
* feat: refactor registration task for guest users

* fix: add deprecation for isCommercial parameter
  • Loading branch information
yusufttur authored Jan 17, 2025
1 parent 12c672e commit ea374cc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/page-objects/storefront/Home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { PageObject } from '../../types/PageObject';

export class Home implements PageObject {

public readonly accountMenuButton: Locator;
public readonly closeGuestSessionButton: Locator;
public readonly productImages: Locator;
public readonly productListItems: Locator;
public readonly languagesDropdown: Locator;
Expand All @@ -25,6 +27,8 @@ export class Home implements PageObject {

constructor(public readonly page: Page) {

this.accountMenuButton = page.getByLabel('Your account');
this.closeGuestSessionButton = page.locator('.account-aside-btn');
this.productImages = page.locator('.product-image-wrapper');
this.productListItems = page.getByRole('listitem');
this.languagesDropdown = page.locator('.top-bar-language').filter({ has: page.getByRole('button') });
Expand Down
80 changes: 47 additions & 33 deletions src/tasks/shop-customer/Account/Register.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test as base } from '@playwright/test';
import type { Task } from '../../../types/Task';
import type { FixtureTypes} from '../../../types/FixtureTypes';
import type { FixtureTypes } from '../../../types/FixtureTypes';
import type { components } from '@shopware/api-client/admin-api-types';
import type { RegistrationData } from '../../../types/ShopwareTypes';
import { AdminApiContext } from 'src/services/AdminApiContext';
Expand All @@ -10,74 +10,88 @@ export const Register = base.extend<{ Register: Task }, FixtureTypes>({
let registeredEmail = '';

const defaultRegistrationData: RegistrationData = {
isCommercial: false,
isGuest: false,
salutation: 'Mr.',
firstName: 'Jeff',
lastName: 'Goldblum',
email: IdProvider.getIdPair().uuid + '@test.com',
email: `${IdProvider.getIdPair().uuid}@test.com`,
password: 'shopware',
street: 'Ebbinghof 10',
city: 'Schöppingen',
country: 'Germany',
postalCode: '48624',
company: 'shopware',
department: 'Operations',
company: 'shopware',
department: 'Operations',
vatRegNo: 'DE1234567890',
};

const registerTask = (overrides?: Partial<RegistrationData>, isCommercial?: boolean ) => {
return async function() {

const task = (overrides?: Partial<RegistrationData>,
/**
* @deprecated The 'isCommercial' argument is deprecated and will be removed in a future version.
* Please avoid using it and rely on the `isCommercial` field in `RegistrationData` instead.
*/
isCommercial?: boolean) => {
return async function Register() {
const registrationData = { ...defaultRegistrationData, ...overrides };

registeredEmail = registrationData.email;

await StorefrontAccountLogin.salutationSelect.selectOption(registrationData.salutation);
await StorefrontAccountLogin.firstNameInput.fill(registrationData.firstName);
await StorefrontAccountLogin.lastNameInput.fill(registrationData.lastName);

if (isCommercial) {
// Deprecation warning for the 'isCommercial' argument
if (registrationData.isCommercial || isCommercial) {
await StorefrontAccountLogin.accountTypeSelect.selectOption('Commercial');
await StorefrontAccountLogin.companyInput.fill(registrationData.company);
await StorefrontAccountLogin.departmentInput.fill(registrationData.department);
await StorefrontAccountLogin.vatRegNoInput.fill(registrationData.vatRegNo);
}

await StorefrontAccountLogin.salutationSelect.selectOption(registrationData.salutation);
await StorefrontAccountLogin.firstNameInput.fill(registrationData.firstName);
await StorefrontAccountLogin.lastNameInput.fill(registrationData.lastName);
await StorefrontAccountLogin.registerEmailInput.fill(registrationData.email);
await StorefrontAccountLogin.registerPasswordInput.fill(registrationData.password);

if (!registrationData.isGuest) {
await StorefrontAccountLogin.registerPasswordInput.fill(registrationData.password);
}

await StorefrontAccountLogin.streetAddressInput.fill(registrationData.street);
await StorefrontAccountLogin.postalCodeInput.fill(registrationData.postalCode);
await StorefrontAccountLogin.cityInput.fill(registrationData.city);
await StorefrontAccountLogin.countryInput.selectOption({ label: registrationData.country });

await StorefrontAccountLogin.registerButton.click();
}
};
};

await use(registerTask);
await use(task);

await deleteRegisteredUser(AdminApiContext, registeredEmail);

},
});

async function deleteRegisteredUser(adminApiContext: AdminApiContext, email: string): Promise<void> {
if (!email) return;

const response = await adminApiContext.post('search/customer', {
data: {
limit: 1,
filter: [
{
type: 'equals',
field: 'email',
value: email,
},
],
},
});
try {
const response = await adminApiContext.post('search/customer', {
data: {
limit: 1,
filter: [
{
type: 'equals',
field: 'email',
value: email,
},
],
},
});

const { data: customers } = (await response.json()) as { data: components['schemas']['Customer'][] };
const { data: customers } = (await response.json()) as { data: components['schemas']['Customer'][] };

for (const customer of customers) {
await adminApiContext.delete(`customer/${customer.id}`);
for (const customer of customers) {
await adminApiContext.delete(`customer/${customer.id}`);
}
} catch (error) {
console.error(`Error deleting user with email ${email}:`, error);
}

}
8 changes: 6 additions & 2 deletions src/tasks/shop-customer/Account/RegisterGuest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { test as base, expect } from '@playwright/test';
import type { Task } from '../../../types/Task';
import type { FixtureTypes} from '../../../types/FixtureTypes';
import type { FixtureTypes } from '../../../types/FixtureTypes';
import type { components } from '@shopware/api-client/admin-api-types';

/**
* @deprecated - Use `Register.ts` instead.
*/

export const RegisterGuest = base.extend<{ RegisterGuest: Task }, FixtureTypes>({
RegisterGuest: async ({ StorefrontAccountLogin, AdminApiContext }, use) => {

Expand All @@ -29,7 +33,7 @@ export const RegisterGuest = base.extend<{ RegisterGuest: Task }, FixtureTypes>(

await StorefrontAccountLogin.streetAddressInput.fill(registrationData.street);
await StorefrontAccountLogin.cityInput.fill(registrationData.city);
if(country != 'Germany') {
if (country != 'Germany') {
await StorefrontAccountLogin.countryInput.selectOption(country);

} else {
Expand Down
2 changes: 2 additions & 0 deletions src/types/ShopwareTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ export type SalesChannelAnalytics = components['schemas']['SalesChannelAnalytics
};

export interface RegistrationData {
isCommercial: boolean;
isGuest: boolean;
salutation: string;
firstName: string;
lastName: string;
Expand Down

0 comments on commit ea374cc

Please sign in to comment.