|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import org.openqa.selenium.By; |
| 4 | +import org.openqa.selenium.WebDriver; |
| 5 | +import org.openqa.selenium.WebElement; |
| 6 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 7 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 8 | + |
| 9 | +import java.net.URL; |
| 10 | +import java.time.Duration; |
| 11 | + |
| 12 | +public class SimpleTest { |
| 13 | + |
| 14 | + private static final String HUB_URL = "http://localhost:4444/wd/hub"; |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + SimpleTest test = new SimpleTest(); |
| 18 | + |
| 19 | + try { |
| 20 | + System.out.println("Starting Add to Cart test..."); |
| 21 | + test.testAddToCartBStackDemo(); |
| 22 | + System.out.println("Add to Cart test completed successfully!"); |
| 23 | + |
| 24 | + System.out.println("\nStarting Checkout Flow test..."); |
| 25 | + test.testCheckoutFlowBStackDemo(); |
| 26 | + System.out.println("Checkout Flow test completed successfully!"); |
| 27 | + |
| 28 | + System.out.println("\nAll tests passed!"); |
| 29 | + } catch (Exception e) { |
| 30 | + System.err.println("Test failed: " + e.getMessage()); |
| 31 | + e.printStackTrace(); |
| 32 | + System.exit(1); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private WebDriver createDriver() throws Exception { |
| 37 | + try { |
| 38 | + System.out.println("Setting up Chrome options..."); |
| 39 | + ChromeOptions chromeOptions = new ChromeOptions(); |
| 40 | + chromeOptions.addArguments( |
| 41 | + "--headless", |
| 42 | + "--no-first-run", |
| 43 | + "--no-default-browser-check", |
| 44 | + "--disable-extensions", |
| 45 | + "--disable-default-apps", |
| 46 | + "--disable-gpu", |
| 47 | + "--disable-dev-shm-usage", |
| 48 | + "--disable-software-rasterizer", |
| 49 | + "--no-sandbox", |
| 50 | + "--disable-background-timer-throttling", |
| 51 | + "--disable-backgrounding-occluded-windows", |
| 52 | + "--disable-renderer-backgrounding", |
| 53 | + "--disable-features=TranslateUI", |
| 54 | + "--disable-ipc-flooding-protection", |
| 55 | + "--disable-web-security", |
| 56 | + "--disable-features=VizDisplayCompositor", |
| 57 | + "--disable-logging", |
| 58 | + "--silent" |
| 59 | + ); |
| 60 | + |
| 61 | + System.out.println("Connecting to Selenium Grid at: " + HUB_URL); |
| 62 | + WebDriver driver = new RemoteWebDriver(new URL(HUB_URL), chromeOptions); |
| 63 | + |
| 64 | + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); |
| 65 | + driver.manage().window().maximize(); |
| 66 | + System.out.println("Successfully connected to Selenium Grid!"); |
| 67 | + return driver; |
| 68 | + } catch (Exception e) { |
| 69 | + System.err.println("Error during setup: " + e.getMessage()); |
| 70 | + e.printStackTrace(); |
| 71 | + throw e; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public void testAddToCartBStackDemo() throws Exception { |
| 76 | + WebDriver driver = createDriver(); |
| 77 | + |
| 78 | + try { |
| 79 | + driver.get("https://bstackdemo.com/"); |
| 80 | + |
| 81 | + WebElement productNameElem = driver.findElement(By.cssSelector("#\\33 > p")); |
| 82 | + |
| 83 | + String productToAdd = productNameElem.getText(); |
| 84 | + |
| 85 | + WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\33 > .shelf-item__buy-btn")); |
| 86 | + addToCartBtn.click(); |
| 87 | + |
| 88 | + try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } |
| 89 | + |
| 90 | + 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")); |
| 91 | + String productInCart = productInCartElem.getText(); |
| 92 | + |
| 93 | + if (!productToAdd.equals(productInCart)) { |
| 94 | + throw new AssertionError("Expected: " + productToAdd + ", but got: " + productInCart); |
| 95 | + } |
| 96 | + System.out.println("Test passed: Add to cart works!"); |
| 97 | + } finally { |
| 98 | + if (driver != null) { |
| 99 | + driver.quit(); |
| 100 | + System.out.println("Browser closed"); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public void testCheckoutFlowBStackDemo() throws Exception { |
| 106 | + WebDriver driver = createDriver(); |
| 107 | + try { |
| 108 | + driver.get("https://bstackdemo.com/"); |
| 109 | + |
| 110 | + driver.findElement(By.id("signin")).click(); |
| 111 | + driver.findElement(By.cssSelector("#username svg")).click(); |
| 112 | + driver.findElement(By.id("react-select-2-option-0-0")).click(); |
| 113 | + driver.findElement(By.cssSelector("#password svg")).click(); |
| 114 | + driver.findElement(By.id("react-select-3-option-0-0")).click(); |
| 115 | + driver.findElement(By.id("login-btn")).click(); |
| 116 | + |
| 117 | + try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } |
| 118 | + |
| 119 | + driver.findElement(By.cssSelector("#\\31 > .shelf-item__buy-btn")).click(); |
| 120 | + driver.findElement(By.cssSelector("div.float-cart__close-btn")).click(); |
| 121 | + driver.findElement(By.cssSelector("#\\32 > .shelf-item__buy-btn")).click(); |
| 122 | + driver.findElement(By.cssSelector(".buy-btn")).click(); |
| 123 | + |
| 124 | + driver.findElement(By.id("firstNameInput")).sendKeys("first"); |
| 125 | + driver.findElement(By.id("lastNameInput")).sendKeys("last"); |
| 126 | + driver.findElement(By.id("addressLine1Input")).sendKeys("address"); |
| 127 | + driver.findElement(By.id("provinceInput")).sendKeys("province"); |
| 128 | + driver.findElement(By.id("postCodeInput")).sendKeys("pincode"); |
| 129 | + |
| 130 | + driver.findElement(By.id("checkout-shipping-continue")).click(); |
| 131 | + String checkoutMessage = driver.findElement(By.id("confirmation-message")).getText(); |
| 132 | + |
| 133 | + if (!"Your Order has been successfully placed.".equals(checkoutMessage)) { |
| 134 | + throw new AssertionError("Expected: 'Your Order has been successfully placed.', but got: " + checkoutMessage); |
| 135 | + } |
| 136 | + System.out.println("Test passed: Checkout flow works!"); |
| 137 | + } finally { |
| 138 | + if (driver != null) { |
| 139 | + driver.quit(); |
| 140 | + System.out.println("Browser closed"); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments