11import { Page , Locator , expect } from "@playwright/test" ;
22import CoreLocators from "../../locator/CoreLocators" ;
3- import { PersonData } from "../persons/PersonsPage" ;
3+ import { personData , PersonData } from "../persons/PersonsPage" ;
4+ import { productData , ProductData , ProductPage } from "../products/ProductPage" ;
5+ import { generateDescription , generateName } from "../../utils/faker" ;
6+
7+
48export type LeadData = {
5- title : string ,
6- description : string ,
7- email :string ,
8- phone : string ,
9- person :PersonData ,
9+ title : string ;
10+ description : string ;
11+ value : string ;
12+ expectedCloseDate : string ; // ISO date string
13+ person : PersonData ;
14+ organizationName : string ;
15+ product : ProductData ;
16+ } ;
17+
18+ export const leadData :LeadData = {
19+ title : generateName ( ) ,
20+ description : generateDescription ( ) ,
21+ value : ( Math . floor ( Math . random ( ) * 10000 ) ) . toString ( ) ,
22+ expectedCloseDate :"2028-12-31" ,
23+ person :personData ,
24+ product :productData ,
25+ organizationName :personData . organizationName
1026 } ;
27+
1128export class LeadPage extends CoreLocators {
1229 readonly page : Page ;
1330
@@ -19,9 +36,9 @@ export class LeadPage extends CoreLocators {
1936 readonly typeDropdown : Locator ;
2037 readonly userDropdown : Locator ;
2138 readonly leadValueInput : Locator ;
22- readonly addPersonButton : Locator ;
39+
2340 readonly personSearchInput : Locator ;
24- readonly addAsNewButton : Locator ;
41+
2542 readonly personEmailInput : Locator ;
2643 readonly personPhoneInput : Locator ;
2744 readonly addOrganizationButton : Locator ;
@@ -32,9 +49,9 @@ export class LeadPage extends CoreLocators {
3249 readonly leadSuccessToast : Locator ;
3350 readonly listViewButton : Locator ;
3451 readonly agreeButton : Locator ;
35- readonly editLeadButton :Locator ;
36- readonly deleteLeadButton :Locator ;
37- readonly listSearchInput :Locator ;
52+ readonly editLeadButton : Locator ;
53+ readonly deleteLeadButton : Locator ;
54+ readonly listSearchInput : Locator ;
3855
3956 // ---- Lead Tabs Locators ----
4057 readonly mailButton : Locator ;
@@ -69,7 +86,7 @@ export class LeadPage extends CoreLocators {
6986 readonly createLeadButton : Locator ;
7087 readonly searchInput : Locator ;
7188 // ------ Validation Message
72- readonly expectedCloseDateMustBeDateAfter :Locator
89+ readonly expectedCloseDateMustBeDateAfter : Locator
7390
7491
7592
@@ -90,12 +107,12 @@ export class LeadPage extends CoreLocators {
90107 this . userDropdown = page . locator ( 'select[name="user_id"]' ) ;
91108 this . leadValueInput = page . locator ( 'input[name="lead_value"]' ) ;
92109 this . searchInput = page . getByRole ( 'textbox' , { name : 'Search by Title' } ) ;
93- this . listSearchInput = page . getByRole ( 'textbox' , { name :'Search' } )
110+ this . listSearchInput = page . getByRole ( 'textbox' , { name : 'Search' } )
94111
95112 // Add person
96- this . addPersonButton = page . locator ( 'div' , { hasText : / ^ C l i c k t o A d d $ / } ) . nth ( 1 ) ;
113+
97114 this . personSearchInput = page . getByRole ( 'textbox' , { name : 'Search...' } ) ;
98- this . addAsNewButton = page . getByText ( 'Add as New' ) ;
115+
99116 this . personEmailInput = page . locator ( 'input[name="person[emails][0][value]"]' ) ;
100117 this . personPhoneInput = page . locator ( 'input[name="person[contact_numbers][0][value]"]' ) ;
101118
@@ -110,7 +127,7 @@ export class LeadPage extends CoreLocators {
110127 this . editLeadButton = page . getByRole ( 'link' , { name : '' } ) . first ( ) ;
111128 this . listViewButton = page . getByRole ( 'link' , { name : '' } ) ;
112129 this . agreeButton = page . getByRole ( 'button' , { name : 'Agree' , exact : true } ) ;
113- this . deleteLeadButton = page . locator ( '.cursor-pointer.rounded-md.p-1\\.5.text-2xl.transition-all.hover\\:bg-gray-200.dark\\:hover\\:bg-gray-800.max-sm\\:place-self-center.icon-delete' ) . first ( ) ;
130+ this . deleteLeadButton = page . locator ( '.cursor-pointer.rounded-md.p-1\\.5.text-2xl.transition-all.hover\\:bg-gray-200.dark\\:hover\\:bg-gray-800.max-sm\\:place-self-center.icon-delete' ) . first ( ) ;
114131
115132
116133 // Tabs
@@ -146,47 +163,58 @@ export class LeadPage extends CoreLocators {
146163
147164 // validation message
148165
149- this . expectedCloseDateMustBeDateAfter = page . getByText ( 'The expected close date must be a date after' ) ;
166+ this . expectedCloseDateMustBeDateAfter = page . getByText ( 'The expected close date must be a date after' ) ;
150167 }
151168 async navigateToLeadList ( ) {
152169 await this . page . goto ( "admin/leads" ) ;
153170 }
154- async getLeadByTitle ( title :string )
155- {
171+ async getLeadByTitle ( title : string ) {
156172 return this . page . getByRole ( 'link' , { name : ` ${ title } ` } ) ;
157173 }
158- async createLead ( leadData :LeadData )
159- {
174+ async createLead ( leadData : LeadData ) {
175+ const productPage = new ProductPage ( this . page ) ;
176+ await productPage . createProduct ( leadData . product ) ;
160177
161178 await this . navigateToLeadList ( ) ;
162-
163179 await this . createLeadButton . click ( ) ;
164180
165181 await this . titleInput . fill ( leadData . title ) ;
166182 await this . descriptionTextarea . fill ( leadData . description ) ;
167183 await this . sourceDropdown . selectOption ( "1" ) ;
168184 await this . typeDropdown . selectOption ( "1" ) ;
169185 await this . userDropdown . selectOption ( "1" ) ;
170- await this . leadValueInput . fill ( "1000" ) ;
186+ await this . leadValueInput . fill ( leadData . value ) ;
171187
172188 await this . addPersonButton . click ( ) ;
173189 await this . personSearchInput . fill ( leadData . person . name ) ;
174- await this . selectListItmeByName ( leadData . person . name ) ;
175- await this . personEmailInput . fill ( leadData . email ) ;
176- await this . personPhoneInput . fill ( leadData . phone ) ;
177-
178- await this . addOrganizationButton . click ( ) ;
179- await this . organizationSearchInput . fill ( leadData . title ) ;
180- await this . addAsNewButton . click ( ) ;
181-
182-
183-
184- ( await this . getElementByTypeAndName ( 'button' , "Save" ) ) . click ( ) ;
190+ const personItem = ( await this . selectListItmeByName ( leadData . person . name ) ) . first ( ) ;
191+ await this . page . waitForTimeout ( 1000 ) ;
192+ const isPersonAlreadyPresent = await personItem . isVisible ( ) ;
193+
194+ if ( isPersonAlreadyPresent ) {
195+ await personItem . click ( ) ;
196+ console . log ( "person allready present" ) ;
197+ }
198+ else {
199+
200+ await this . addAsNewButton . click ( ) ;
201+ await this . personEmailInput . fill ( leadData . person . emails ) ;
202+ await this . personPhoneInput . fill ( leadData . person . contactNumber ) ;
203+ await this . addOrganizationButton . click ( ) ; // this an issue add organiztion is not working if person allready present so that i have added this into else condition.
204+ await this . organizationSearchInput . fill ( leadData . title ) ;
205+ await this . addAsNewButton . click ( ) ;
206+
207+ }
208+ await this . leadProductAddMoreButton . click ( ) ;
209+ await this . leadProductSelect . click ( ) ;
210+ ( await this . getElementByTypeAndName ( 'textbox' , 'Search...' ) ) . fill ( leadData . product . name ) ;
211+ await this . page . locator ( `//li[@class="cursor-pointer px-4 py-2 text-gray-800 transition-colors hover:bg-blue-100 dark:text-white dark:hover:bg-gray-900"]` ) . first ( ) . click ( ) ;
212+ ( await this . getElementByTypeAndName ( 'button' , "Save" ) ) . click ( ) ;
185213 await this . searchInput . fill ( leadData . title ) ;
186214 await this . page . keyboard . press ( 'Enter' ) ;
187215 await expect ( ( await this . getLeadByTitle ( leadData . title ) ) ) . toBeVisible ( ) ;
188216
189-
217+
190218 await expect ( this . leadSuccessToast ) . toBeVisible ( ) ;
191219
192220 }
0 commit comments