33import org .openqa .selenium .By ;
44import org .openqa .selenium .WebDriver ;
55import org .openqa .selenium .WebElement ;
6+ import org .openqa .selenium .chrome .ChromeDriver ;
67import org .openqa .selenium .chrome .ChromeOptions ;
78import org .openqa .selenium .remote .DesiredCapabilities ;
89import org .openqa .selenium .remote .RemoteWebDriver ;
910import org .testng .Assert ;
1011import org .testng .annotations .AfterMethod ;
1112import org .testng .annotations .BeforeMethod ;
1213import org .testng .annotations .Test ;
14+ import org .openqa .selenium .support .ui .WebDriverWait ;
15+ import org .openqa .selenium .support .ui .ExpectedConditions ;
1316
1417import java .net .URL ;
1518import java .time .Duration ;
@@ -59,6 +62,7 @@ public void setUp() throws Exception {
5962
6063 System .out .println ("Connecting to Selenium Grid at: " + HUB_URL );
6164 driver = new RemoteWebDriver (new URL (HUB_URL ), chromeOptions );
65+ // driver = new ChromeDriver(); // For local testing without Grid
6266
6367 driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (10 ));
6468 driver .manage ().window ().maximize ();
@@ -77,20 +81,27 @@ public void testAddToCartBStackDemo() {
7781 // Visit BStackDemo
7882 driver .get ("https://bstackdemo.com/" );
7983
80- // Get name of product to add to cart (first product)
81- WebElement productNameElem = driver .findElement (By .cssSelector ("#\\ 33 > p" ));
82- String productToAdd = productNameElem .getText ();
84+ // Get name of product to add to cart using provided XPath //*[@id="3"]/p
85+ WebDriverWait wait = new WebDriverWait (driver , Duration .ofSeconds (10 ));
86+ WebElement productNameElem = wait .until (
87+ ExpectedConditions .visibilityOfElementLocated (By .xpath ("//*[@id='3']/p" ))
88+ );
89+ String productToAdd = productNameElem .getText ().trim ();
8390
8491 // Click on add to cart
8592 WebElement addToCartBtn = driver .findElement (By .cssSelector ("#\\ 33 > .shelf-item__buy-btn" ));
8693 addToCartBtn .click ();
8794
88- // Get name of item in cart
89- WebElement productInCartElem = driver .findElement (By .cssSelector ("#__next > div > div > div.float-cart.float-cart--open > div.float-cart__content > div.float-cart__shelf-container > div > div.shelf-item__details > p.title" ));
90- String productInCart = productInCartElem .getText ();
95+ // Wait for cart to open and get the product name inside the cart
96+ WebElement productInCartElem = wait .until (
97+ ExpectedConditions .visibilityOfElementLocated (
98+ By .cssSelector (".float-cart.float-cart--open .float-cart__shelf-container .shelf-item__details p.title" )
99+ )
100+ );
101+ String productInCart = productInCartElem .getText ().trim ();
91102
92103 // Check if product in cart is same as one added
93- Assert .assertEquals (productInCart , productToAdd );
104+ Assert .assertEquals (productInCart , productToAdd , "Product in cart should match product selected." );
94105 System .out .println ("Test passed: Add to cart works!" );
95106 }
96107
@@ -99,6 +110,8 @@ public void testCheckoutFlowBStackDemo() {
99110 // Visit BStackDemo
100111 driver .get ("https://bstackdemo.com/" );
101112
113+ WebDriverWait wait = new WebDriverWait (driver , Duration .ofSeconds (10 ));
114+
102115 // Sign in
103116 driver .findElement (By .id ("signin" )).click ();
104117 driver .findElement (By .cssSelector ("#username svg" )).click ();
@@ -110,11 +123,25 @@ public void testCheckoutFlowBStackDemo() {
110123 // Wait for login
111124 try { Thread .sleep (500 ); } catch (InterruptedException e ) { Thread .currentThread ().interrupt (); }
112125
113- // Click on buy item
114- driver .findElement (By .cssSelector ("#\\ 31 > .shelf-item__buy-btn" )).click ();
115- driver .findElement (By .cssSelector ("div.float-cart__close-btn" )).click ();
116- driver .findElement (By .cssSelector ("#\\ 32 > .shelf-item__buy-btn" )).click ();
117- driver .findElement (By .cssSelector (".buy-btn" )).click ();
126+ // Click on first item (wait until clickable to avoid interactability issues)
127+ wait .until (ExpectedConditions .elementToBeClickable (By .cssSelector ("#\\ 31 > .shelf-item__buy-btn" ))).click ();
128+
129+ // Wait for cart overlay to open
130+ wait .until (ExpectedConditions .visibilityOfElementLocated (By .cssSelector (".float-cart.float-cart--open" )));
131+
132+ // Attempt to close cart safely (selector simplified to class only; element may not be a div)
133+ try {
134+ WebElement closeBtn = wait .until (ExpectedConditions .elementToBeClickable (By .cssSelector (".float-cart__close-btn" )));
135+ closeBtn .click ();
136+ } catch (Exception e ) {
137+ System .out .println ("[WARN] Could not close cart overlay: " + e .getMessage ());
138+ }
139+
140+ // Click on second item
141+ wait .until (ExpectedConditions .elementToBeClickable (By .cssSelector ("#\\ 32 > .shelf-item__buy-btn" ))).click ();
142+
143+ // Proceed to checkout (Buy button)
144+ wait .until (ExpectedConditions .elementToBeClickable (By .cssSelector (".buy-btn" ))).click ();
118145
119146 // Add address details
120147 driver .findElement (By .id ("firstNameInput" )).sendKeys ("first" );
@@ -123,9 +150,13 @@ public void testCheckoutFlowBStackDemo() {
123150 driver .findElement (By .id ("provinceInput" )).sendKeys ("province" );
124151 driver .findElement (By .id ("postCodeInput" )).sendKeys ("pincode" );
125152
126- // Checkout
127- driver .findElement (By .id ("checkout-shipping-continue" )).click ();
128- driver .findElement (By .xpath ("//*[text()='Continue']" )).click ();
153+ // Checkout
154+ wait .until (ExpectedConditions .elementToBeClickable (By .id ("checkout-shipping-continue" ))).click ();
155+ // Updated: use provided XPath for the Continue Shopping button
156+ WebElement continueShopping = wait .until (
157+ ExpectedConditions .elementToBeClickable (By .xpath ("//*[@id='checkout-app']/div/div/div/div/a/button" ))
158+ );
159+ continueShopping .click ();
129160 driver .findElement (By .xpath ("//*[text()='Orders']" )).click ();
130161
131162 System .out .println ("Test passed: Checkout flow works!" );
@@ -139,3 +170,4 @@ public void tearDown() {
139170 }
140171 }
141172}
173+ src /test /java /com /example /SimpleTest .java
0 commit comments