1+ // @ts -check
2+
3+ import { expect , type Locator , type Page } from '@playwright/test' ;
4+ import { UIReference , inputValues , outcomeMarker } from '@config' ;
5+ import { requireEnv } from "@utils/env.utils" ;
6+
7+ class AdminCustomers {
8+ readonly page : Page ;
9+
10+ constructor ( page : Page ) {
11+ this . page = page ;
12+ }
13+
14+ /**
15+ * @feature Customer Management
16+ * @scenario Check if a customer exists by email address
17+ * @given the admin is on the Magento dashboard
18+ * @when the admin navigates to Customers > All Customers
19+ * @and the customer table is fully loaded
20+ * @and the admin searches for a specific email address
21+ * @then the system returns whether a customer with that email exists in the customer list
22+ */
23+ async checkIfCustomerExists ( email : string ) {
24+ const mainMenuCustomersButton = this . page . getByRole ( 'link' , { name : UIReference . adminPage . navigation . customersButtonLabel } ) . first ( ) ;
25+ const allCustomersLink = this . page . getByRole ( 'link' , { name : UIReference . adminPage . subNavigation . allCustomersButtonLabel } ) ;
26+ const customersSearchField = this . page . getByRole ( 'textbox' , { name : UIReference . customerOverviewPage . tableSearchFieldLabel } ) ;
27+
28+ // loop clicking the 'Customers' button until clicking it show the subnavigation
29+ await expect ( async ( ) => {
30+ await mainMenuCustomersButton . press ( 'Enter' ) ;
31+ await expect ( allCustomersLink , `Link to "All Customers" is visible` ) . toBeVisible ( { timeout : 5000 } ) ;
32+ } ) . toPass ( ) ;
33+
34+ await allCustomersLink . click ( ) ;
35+
36+ // Wait for URL. If loading symbol is visible, wait for it to go away
37+ await this . page . waitForURL ( `**/${ requireEnv ( 'MAGENTO_ADMIN_SLUG' ) } /customer/index/**` ) ;
38+ if ( await this . page . locator ( UIReference . general . loadingSpinnerLocator ) . isVisible ( ) ) {
39+ await this . page . locator ( UIReference . general . loadingSpinnerLocator ) . waitFor ( { state : 'hidden' } ) ;
40+ }
41+
42+ await customersSearchField . waitFor ( ) ;
43+ await customersSearchField . fill ( email ) ;
44+ await this . page . getByRole ( 'button' , { name : UIReference . general . searchButtonLabel } ) . click ( ) ;
45+
46+
47+ if ( await this . page . locator ( UIReference . general . loadingSpinnerLocator ) . isVisible ( ) ) {
48+ await this . page . locator ( UIReference . general . loadingSpinnerLocator ) . waitFor ( { state : 'hidden' } ) ;
49+ }
50+
51+ // Loop to ensure the 'results found' text is visible
52+ await expect ( async ( ) => {
53+ await this . page . getByText ( outcomeMarker . customerOverviewPage . searchResultsFoundText ) . first ( ) ;
54+ } ) . toPass ( ) ;
55+
56+ // Return true (email found) or false (email not found)
57+ return await this . page . getByRole ( 'cell' , { name :email } ) . locator ( 'div' ) . isVisible ( ) ;
58+ }
59+
60+ /**
61+ * @feature Customer Management
62+ * @scenario Create a new customer account
63+ * @given the admin is on the Magento dashboard
64+ * @when the admin navigates to Customers > All Customers
65+ * @and clicks the 'Create New Customer' button
66+ * @then the admin fills in the mandatory fields and optional fields for a new customer account
67+ * @and the system saves the customer account and navigates to the account edit page
68+ * @and displays a confirmation message that the customer was saved
69+ */
70+ async createNewCustomerAccount (
71+ firstName : string ,
72+ lastName : string ,
73+ email : string
74+ ) {
75+ const createNewCustomersLink = this . page . getByRole ( 'button' , { name : UIReference . adminCustomers . createNewCustomerButtonLabel } ) ;
76+ await createNewCustomersLink . click ( ) ;
77+
78+ // Wait for URL. If loading symbol is visible, wait for it to go away
79+ await this . page . waitForURL ( `**/${ requireEnv ( 'MAGENTO_ADMIN_SLUG' ) } /customer/index/new/**` ) ;
80+ if ( await this . page . locator ( UIReference . adminGeneral . loadingSpinnerLocator ) . isVisible ( ) ) {
81+ await this . page . locator ( UIReference . adminGeneral . loadingSpinnerLocator ) . waitFor ( { state : 'hidden' } ) ;
82+ }
83+
84+ const accountCreationFirstNameField = this . page . getByLabel ( UIReference . personalInformation . firstNameLabel ) ;
85+ const accountCreationLastNameField = this . page . getByLabel ( UIReference . personalInformation . lastNameLabel ) ;
86+ const accountCreationEmailField = this . page . getByLabel ( UIReference . credentials . emailFieldLabel , { exact : true } ) ;
87+ const accountCreationConfirmButton = this . page . getByRole ( 'button' , { name : UIReference . adminCustomers . registration . createAccountSaveAndContinueButtonLabel } ) ;
88+ const customersSearchField = this . page . getByRole ( 'textbox' , { name : UIReference . adminGeneral . tableSearchFieldLabel } ) ;
89+
90+ // Optional fields:
91+ const allowBulkPurchaseSwitcher = this . page . locator ( UIReference . cartPriceRulesPage . activeStatusSwitcherLocator ) . first ( ) ;
92+
93+ await accountCreationFirstNameField . fill ( firstName ) ;
94+ await accountCreationLastNameField . fill ( lastName ) ;
95+ await accountCreationEmailField . fill ( email ) ;
96+ await allowBulkPurchaseSwitcher . click ( ) ;
97+ await accountCreationConfirmButton . click ( ) ;
98+
99+ await this . page . waitForURL ( `**/${ requireEnv ( 'MAGENTO_ADMIN_SLUG' ) } /customer/index/edit/**` ) ;
100+ if ( await this . page . locator ( UIReference . adminGeneral . loadingSpinnerLocator ) . isVisible ( ) ) {
101+ await this . page . locator ( UIReference . adminGeneral . loadingSpinnerLocator ) . waitFor ( { state : 'hidden' } ) ;
102+
103+ await expect (
104+ this . page . locator ( UIReference . general . messageLocator ) . filter ( { hasText : 'You saved the customer.' } )
105+ ) . toBeVisible ( ) ;
106+ }
107+
108+ await this . approveAccount ( email ) ;
109+ }
110+
111+ /**
112+ * @feature Customer Management
113+ * @scenario Approve a customer account
114+ * @given the admin is on the Magento dashboard
115+ * @when the admin navigates to Customers > All Customers
116+ * @and searches for a specific email address
117+ * @then the admin clicks on the 'Edit' link for the corresponding customer
118+ * @and approves the customer account
119+ * @and the system displays a confirmation message that the customer account has been approved
120+ */
121+ async approveAccount ( email : string ) {
122+
123+ const customersSearchField = this . page . getByRole ( 'textbox' , { name : UIReference . adminGeneral . tableSearchFieldLabel } ) ;
124+ const editAccountButton = this . page . getByRole ( 'link' , { name : 'Edit' } ) . first ( )
125+ const approvalButtonAccountEdit = this . page . getByRole ( 'button' , { name : 'Approve' } )
126+
127+ await customersSearchField . waitFor ( ) ;
128+ await customersSearchField . fill ( email ) ;
129+ await this . page . getByRole ( 'button' , { name : UIReference . adminGeneral . searchButtonLabel } ) . click ( ) ;
130+
131+ if ( await this . page . locator ( UIReference . adminGeneral . loadingSpinnerLocator ) . isVisible ( ) ) {
132+ await this . page . locator ( UIReference . adminGeneral . loadingSpinnerLocator ) . waitFor ( { state : 'hidden' } ) ;
133+ }
134+
135+ // Loop to ensure the 'results found' text is visible
136+ await expect ( async ( ) => {
137+ await this . page . getByText ( outcomeMarker . customerOverviewPage . searchResultsFoundText ) . first ( ) ;
138+ } ) . toPass ( ) ;
139+
140+ // Return true (email found) or false (email not found)
141+ await this . page . getByRole ( 'cell' , { name :email } ) . locator ( 'div' ) . isVisible ( ) ;
142+
143+ await expect ( async ( ) => {
144+ editAccountButton . click ( ) ;
145+ } ) . toPass ( ) ;
146+
147+ await this . page . waitForURL ( `**/${ requireEnv ( 'MAGENTO_ADMIN_SLUG' ) } /customer/index/edit/**` ) ;
148+ if ( await this . page . locator ( UIReference . general . loadingSpinnerLocator ) . isVisible ( ) ) {
149+ console . log ( 'Spinner is visible' ) ;
150+ await this . page . locator ( UIReference . general . loadingSpinnerLocator ) . waitFor ( { state : 'hidden' } ) ;
151+ }
152+
153+ // Press approval button when approval button is visible
154+ if ( await approvalButtonAccountEdit . isVisible ( ) ) {
155+ await approvalButtonAccountEdit . click ( ) ;
156+
157+ await this . page . waitForURL ( `**/${ requireEnv ( 'MAGENTO_ADMIN_SLUG' ) } /customer/index/edit/**` ) ;
158+ if ( await this . page . locator ( UIReference . general . loadingSpinnerLocator ) . isVisible ( ) ) {
159+ console . log ( 'Spinner is visible' ) ;
160+ await this . page . locator ( UIReference . general . loadingSpinnerLocator ) . waitFor ( { state : 'hidden' } ) ;
161+ }
162+ await expect (
163+ this . page
164+ . locator ( UIReference . general . messageLocator )
165+ . filter ( { hasText : 'Customer account has been approved!' } )
166+ ) . toBeVisible ( ) ;
167+ }
168+ }
169+ }
170+
171+ export default AdminCustomers ;
0 commit comments