1
+ const { test, expect, request} = require ( '@playwright/test' ) ;
2
+ const loginPayload = { userEmail :
"[email protected] " , userPassword :
"Password1" } ;
3
+ const loginURL = "https://rahulshettyacademy.com/api/ecom/auth/login" ;
4
+
5
+ test . beforeAll ( async ( ) => {
6
+ const apiContext = await request . newContext ( ) ;
7
+ const loginResponse = await apiContext . post ( loginURL , {
8
+ data :loginPayload
9
+ } ) ;
10
+ expect ( loginResponse . ok ( ) ) . toBeTruthy ( ) ;
11
+ const loginResponseJson = loginResponse . json ( ) ;
12
+ const token = loginResponseJson . token ;
13
+
14
+ } ) ;
15
+ // test.beforeEach( ()=>{
16
+ // //code
17
+ // })
18
+
19
+
20
+ test ( 'Browser check' , async ( { page} ) =>
21
+ {
22
+ const products = page . locator ( ".card-body" ) ;
23
+ const searchForProductName = 'ZARA COAT 3' ;
24
+ const email = "[email protected] " ;
25
+
26
+ await page . goto ( "https://rahulshettyacademy.com/client" ) ;
27
+ await page . locator ( "#userEmail" ) . fill ( email ) ;
28
+ await page . locator ( "#userPassword" ) . fill ( "Password1" ) ;
29
+ await page . locator ( "[value='Login']" ) . click ( ) ;
30
+ await page . locator ( ".card-body b" ) . first ( ) . waitFor ( ) ; // products to loadup
31
+
32
+ const titles = await page . locator ( ".card-body b" ) . allTextContents ( ) ;
33
+ console . log ( titles ) ;
34
+ // await page.pause();
35
+ //hunt for product 'Zara Coat 4' to click on it
36
+ const countProducts = await products . count ( ) ;
37
+ for ( let i = 0 ; i < countProducts ; ++ i )
38
+ {
39
+ if ( await products . nth ( i ) . locator ( "b" ) . textContent ( ) === searchForProductName )
40
+ {
41
+ //logic to add product to cart, since we've identified the one we search for
42
+ await products . nth ( i ) . locator ( "text= Add To Cart" ) . click ( ) ;
43
+ break ; //found it, no need to continue loop
44
+ }
45
+ }
46
+ // await page.pause();
47
+ //goto cart
48
+ await page . waitForLoadState ( 'networkidle' )
49
+ await page . locator ( "button[routerlink='/dashboard/cart']" ) . dispatchEvent ( 'click' ) ;
50
+ await page . locator ( "div li" ) . first ( ) . waitFor ( ) ; //wait until 1st item of list of items show up (1st item in list is populated with data)
51
+
52
+ const bool = await page . locator ( "h3:has-text('Zara Coat 3')" ) . isVisible ( ) ; //check that it exists -- returns boolean value
53
+ expect ( bool ) . toBeTruthy ( ) ; //TODO: check evauluation if not fixed at end of course
54
+ await page . locator ( "li[class='totalRow'] button[type='button']" ) . click ( ) ; //checkout button
55
+ // await page.pause();
56
+
57
+ //checkout page
58
+ await page . locator ( "[placeholder*='Country']" ) . pressSequentially ( "ro" , { delay :100 } ) ; // input field, shows results per input so pressing 1letter at a time
59
+ const countryDropdown = page . locator ( ".ta-results" ) ;
60
+ await countryDropdown . waitFor ( ) ; //wait until is shown
61
+ const countryDropdownCount = await countryDropdown . locator ( "button" ) . count ( ) ;
62
+ for ( let i = 0 ; i < countryDropdownCount ; ++ i ) {
63
+ const text = await countryDropdown . locator ( "button" ) . nth ( i ) . textContent ( ) ;
64
+ if ( text === " Romania" ) { //could use trim to trim spaces, or can use the 'includes' option
65
+ await countryDropdown . locator ( "button" ) . nth ( i ) . click ( ) ;
66
+ break ;
67
+ }
68
+ }
69
+ // await page.pause();
70
+
71
+ //asertions for the page
72
+ expect ( page . locator ( ".user__name [type='text']" ) . first ( ) ) . toHaveText ( email ) ;
73
+ await page . locator ( ".action__submit" ) . click ( ) ; // place order button
74
+
75
+ //thankyou for oder page
76
+ await expect ( page . locator ( ".hero-primary" ) ) . toHaveText ( " Thankyou for the order. " ) ;
77
+ const orderID = await page . locator ( ".em-spacer-1 .ng-star-inserted" ) . textContent ( ) ;
78
+ console . log ( "Order ID is: " + orderID ) ;
79
+
80
+ //orders page
81
+ await page . locator ( "button[routerlink='/dashboard/myorders']" ) . dispatchEvent ( 'click' ) ;
82
+ await page . locator ( "tbody" ) . waitFor ( ) ; //waiting for whole table-body to loadup, prior to making the evaluation #async breakage
83
+ const rows = await page . locator ( "tbody tr" ) ; //waiting for table data to be loaded up
84
+
85
+ for ( let i = 0 ; i < await rows . count ( ) ; i ++ ) {
86
+ const rowOrderId = await rows . nth ( i ) . locator ( "th" ) . textContent ( ) ;
87
+ if ( orderID . includes ( rowOrderId ) )
88
+ {
89
+ await rows . nth ( i ) . locator ( "button" ) . first ( ) . click ( ) ;
90
+ break ;
91
+ }
92
+ }
93
+ const orderIdDetails = await page . locator ( ".col-text" ) . textContent ( ) ;
94
+
95
+ expect ( orderID . includes ( orderIdDetails ) ) . toBeTruthy ( ) ;
96
+ } )
0 commit comments