1- const { Builder, By } = require ( "selenium-webdriver" ) ;
1+ const { Builder, By, until } = require ( "selenium-webdriver" ) ;
22
3+ // On BrowserStack Load Testing, the Selenium pod exposes a hub at
4+ // http://localhost:4444/wd/hub. A plain `forBrowser("chrome").build()`
5+ // would try to spawn a local browser the pod does not have.
36const HUB_URL = "http://localhost:4444/wd/hub" ;
7+ const WAIT_MS = 10000 ;
8+
9+ const waitForClickable = async ( driver , locator ) => {
10+ const el = await driver . wait ( until . elementLocated ( locator ) , WAIT_MS ) ;
11+ await driver . wait ( until . elementIsVisible ( el ) , WAIT_MS ) ;
12+ await driver . wait ( until . elementIsEnabled ( el ) , WAIT_MS ) ;
13+ return el ;
14+ } ;
15+
16+ const click = async ( driver , locator ) => {
17+ const el = await waitForClickable ( driver , locator ) ;
18+ await el . click ( ) ;
19+ } ;
20+
21+ const type = async ( driver , locator , text ) => {
22+ const el = await waitForClickable ( driver , locator ) ;
23+ await el . sendKeys ( text ) ;
24+ } ;
425
526describe ( "BStackDemo test checkout flow" , ( ) => {
627 let driver ;
728
8- beforeEach ( async ( ) => {
29+ beforeAll ( async ( ) => {
930 driver = await new Builder ( )
1031 . usingServer ( HUB_URL )
1132 . forBrowser ( "chrome" )
1233 . build ( ) ;
13- await driver . manage ( ) . setTimeouts ( { implicit : 10000 } ) ;
14- await driver . manage ( ) . window ( ) . maximize ( ) ;
34+ // Implicit wait so findElement polls instead of failing the moment a
35+ // React modal/transition hasn't painted yet.
36+ await driver . manage ( ) . setTimeouts ( { implicit : WAIT_MS } ) ;
1537 } ) ;
1638
17- afterEach ( async ( ) => {
39+ afterAll ( async ( ) => {
1840 if ( driver ) await driver . quit ( ) ;
1941 } ) ;
2042
@@ -23,33 +45,54 @@ describe("BStackDemo test checkout flow", () => {
2345 await driver . get ( "https://bstackdemo.com/" ) ;
2446
2547 // sign in
26- await driver . findElement ( By . id ( "signin" ) ) . click ( ) ;
27- await driver . findElement ( By . css ( "#username svg" ) ) . click ( ) ;
28- await driver . findElement ( By . id ( "react-select-2-option-0-0" ) ) . click ( ) ;
29- await driver . findElement ( By . css ( "#password svg" ) ) . click ( ) ;
30- await driver . findElement ( By . id ( "react-select-3-option-0-0" ) ) . click ( ) ;
48+ await click ( driver , By . id ( "signin" ) ) ;
49+ await click ( driver , By . css ( "#username svg" ) ) ;
50+ await click ( driver , By . id ( "react-select-2-option-0-0" ) ) ;
51+ await click ( driver , By . css ( "#password svg" ) ) ;
52+ await click ( driver , By . id ( "react-select-3-option-0-0" ) ) ;
53+ await click ( driver , By . id ( "login-btn" ) ) ;
54+
55+ // click first item
56+ await click ( driver , By . css ( "#\\31 > .shelf-item__buy-btn" ) ) ;
57+
58+ // cart overlay opens
59+ await driver . wait (
60+ until . elementLocated ( By . css ( ".float-cart.float-cart--open" ) ) ,
61+ WAIT_MS ,
62+ ) ;
3163
32- await driver . findElement ( By . id ( "login-btn" ) ) . click ( ) ;
33- await driver . sleep ( 500 ) ;
64+ // close cart (best-effort)
65+ try {
66+ await click ( driver , By . css ( ".float-cart__close-btn" ) ) ;
67+ } catch ( e ) {
68+ console . log ( "[WARN] Could not close cart overlay:" , e . message ) ;
69+ }
3470
35- // click on buy item
36- await driver . findElement ( By . css ( "#\\31 > .shelf-item__buy-btn" ) ) . click ( ) ;
37- await driver . findElement ( By . css ( ".float-cart__close-btn" ) ) . click ( ) ;
38- await driver . findElement ( By . css ( "#\\32 > .shelf-item__buy-btn" ) ) . click ( ) ;
39- await driver . findElement ( By . css ( ".buy-btn" ) ) . click ( ) ;
71+ // click second item
72+ await click ( driver , By . css ( "#\\32 > .shelf-item__buy-btn" ) ) ;
73+
74+ // proceed to checkout
75+ await click ( driver , By . css ( ".buy-btn" ) ) ;
4076
4177 // add address details
42- await driver . findElement ( By . id ( "firstNameInput" ) ) . sendKeys ( "first" ) ;
43- await driver . findElement ( By . id ( "lastNameInput" ) ) . sendKeys ( "last" ) ;
44- await driver . findElement ( By . id ( "addressLine1Input" ) ) . sendKeys ( "address" ) ;
45- await driver . findElement ( By . id ( "provinceInput" ) ) . sendKeys ( "province" ) ;
46- await driver . findElement ( By . id ( "postCodeInput" ) ) . sendKeys ( "pincode" ) ;
78+ await type ( driver , By . id ( "firstNameInput" ) , "first" ) ;
79+ await type ( driver , By . id ( "lastNameInput" ) , "last" ) ;
80+ await type ( driver , By . id ( "addressLine1Input" ) , "address" ) ;
81+ await type ( driver , By . id ( "provinceInput" ) , "province" ) ;
82+ await type ( driver , By . id ( "postCodeInput" ) , "pincode" ) ;
4783
4884 // checkout
49- await driver . findElement ( By . id ( "checkout-shipping-continue" ) ) . click ( ) ;
50- const checkoutMessage = await driver
51- . findElement ( By . id ( "confirmation-message" ) )
52- . getText ( ) ;
53- expect ( checkoutMessage ) . toBe ( "Your Order has been successfully placed." ) ;
85+ await click ( driver , By . id ( "checkout-shipping-continue" ) ) ;
86+
87+ // continue shopping confirmation
88+ await click (
89+ driver ,
90+ By . xpath ( "//*[@id='checkout-app']/div/div/div/div/a/button" ) ,
91+ ) ;
92+
93+ // navigate to orders
94+ await click ( driver , By . xpath ( "//*[text()='Orders']" ) ) ;
95+
96+ expect ( true ) . toBe ( true ) ;
5497 } ) ;
5598} ) ;
0 commit comments