|
| 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.DesiredCapabilities; |
| 8 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 9 | +import org.testng.Assert; |
| 10 | +import org.testng.annotations.AfterMethod; |
| 11 | +import org.testng.annotations.BeforeMethod; |
| 12 | +import org.testng.annotations.Test; |
| 13 | + |
| 14 | +import java.net.URL; |
| 15 | +import java.time.Duration; |
| 16 | + |
| 17 | +public class SimpleTest { |
| 18 | + |
| 19 | + private WebDriver driver; |
| 20 | + private static final String HUB_URL = "http://localhost:4444/wd/hub"; |
| 21 | + |
| 22 | + @BeforeMethod |
| 23 | + public void setUp() throws Exception { |
| 24 | + try { |
| 25 | + System.out.println("Setting up Chrome options..."); |
| 26 | + |
| 27 | + // Try a simpler approach first - just basic capabilities |
| 28 | + DesiredCapabilities caps = new DesiredCapabilities(); |
| 29 | + caps.setBrowserName("chrome"); |
| 30 | + |
| 31 | + // Add Chrome options as a map |
| 32 | + ChromeOptions chromeOptions = new ChromeOptions(); |
| 33 | + // Use timestamp for unique user data directory |
| 34 | + String uniqueUserDataDir = "/tmp/chrome-user-data-" + System.currentTimeMillis(); |
| 35 | + chromeOptions.addArguments( |
| 36 | + "--headless", |
| 37 | + "--no-first-run", |
| 38 | + "--no-default-browser-check", |
| 39 | + "--disable-extensions", |
| 40 | + "--disable-default-apps", |
| 41 | + "--disable-gpu", |
| 42 | + "--disable-dev-shm-usage", |
| 43 | + "--disable-software-rasterizer", |
| 44 | + "--no-sandbox", |
| 45 | + "--disable-background-timer-throttling", |
| 46 | + "--disable-backgrounding-occluded-windows", |
| 47 | + "--disable-renderer-backgrounding", |
| 48 | + "--disable-features=TranslateUI", |
| 49 | + "--disable-ipc-flooding-protection", |
| 50 | + "--disable-web-security", |
| 51 | + "--disable-features=VizDisplayCompositor", |
| 52 | + "--disable-logging", |
| 53 | + "--silent" |
| 54 | + ); |
| 55 | + |
| 56 | + System.out.println("Chrome options configured. User data dir: " + uniqueUserDataDir); |
| 57 | + |
| 58 | + // Merge the chrome options with capabilities |
| 59 | + |
| 60 | + System.out.println("Connecting to Selenium Grid at: " + HUB_URL); |
| 61 | + driver = new RemoteWebDriver(new URL(HUB_URL), chromeOptions); |
| 62 | + |
| 63 | + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); |
| 64 | + driver.manage().window().maximize(); |
| 65 | + |
| 66 | + System.out.println("Successfully connected to Selenium Grid!"); |
| 67 | + |
| 68 | + } catch (Exception e) { |
| 69 | + System.err.println("Error during setup: " + e.getMessage()); |
| 70 | + e.printStackTrace(); |
| 71 | + throw e; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testAddToCartBStackDemo() { |
| 77 | + // Visit BStackDemo |
| 78 | + driver.get("https://bstackdemo.com/"); |
| 79 | + |
| 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(); |
| 83 | + |
| 84 | + // Click on add to cart |
| 85 | + WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\33 > .shelf-item__buy-btn")); |
| 86 | + addToCartBtn.click(); |
| 87 | + |
| 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(); |
| 91 | + |
| 92 | + // Check if product in cart is same as one added |
| 93 | + Assert.assertEquals(productInCart, productToAdd); |
| 94 | + System.out.println("Test passed: Add to cart works!"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testCheckoutFlowBStackDemo() { |
| 99 | + // Visit BStackDemo |
| 100 | + driver.get("https://bstackdemo.com/"); |
| 101 | + |
| 102 | + // Sign in |
| 103 | + driver.findElement(By.id("signin")).click(); |
| 104 | + driver.findElement(By.cssSelector("#username svg")).click(); |
| 105 | + driver.findElement(By.id("react-select-2-option-0-0")).click(); |
| 106 | + driver.findElement(By.cssSelector("#password svg")).click(); |
| 107 | + driver.findElement(By.id("react-select-3-option-0-0")).click(); |
| 108 | + driver.findElement(By.id("login-btn")).click(); |
| 109 | + |
| 110 | + // Wait for login |
| 111 | + try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } |
| 112 | + |
| 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(); |
| 118 | + |
| 119 | + // Add address details |
| 120 | + driver.findElement(By.id("firstNameInput")).sendKeys("first"); |
| 121 | + driver.findElement(By.id("lastNameInput")).sendKeys("last"); |
| 122 | + driver.findElement(By.id("addressLine1Input")).sendKeys("address"); |
| 123 | + driver.findElement(By.id("provinceInput")).sendKeys("province"); |
| 124 | + driver.findElement(By.id("postCodeInput")).sendKeys("pincode"); |
| 125 | + |
| 126 | + // Checkout |
| 127 | + driver.findElement(By.id("checkout-shipping-continue")).click(); |
| 128 | + driver.findElement(By.xpath("//*[text()='Continue']")).click(); |
| 129 | + driver.findElement(By.xpath("//*[text()='Orders']")).click(); |
| 130 | + |
| 131 | + System.out.println("Test passed: Checkout flow works!"); |
| 132 | + } |
| 133 | + |
| 134 | + @AfterMethod |
| 135 | + public void tearDown() { |
| 136 | + if (driver != null) { |
| 137 | + driver.quit(); |
| 138 | + System.out.println("Browser closed"); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments