|
| 1 | +const { Builder, By, until } = require("selenium-webdriver"); |
| 2 | + |
| 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. |
| 6 | +const 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 | +}; |
| 25 | + |
| 26 | +describe("BStackDemo test checkout flow", () => { |
| 27 | + let driver; |
| 28 | + |
| 29 | + beforeAll(async () => { |
| 30 | + driver = await new Builder() |
| 31 | + .usingServer(HUB_URL) |
| 32 | + .forBrowser("chrome") |
| 33 | + .build(); |
| 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 }); |
| 37 | + }); |
| 38 | + |
| 39 | + afterAll(async () => { |
| 40 | + if (driver) await driver.quit(); |
| 41 | + }); |
| 42 | + |
| 43 | + test("should complete checkout flow successfully", async () => { |
| 44 | + // visit the site |
| 45 | + await driver.get("https://bstackdemo.com/"); |
| 46 | + |
| 47 | + // sign in |
| 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 | + ); |
| 63 | + |
| 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 | + } |
| 70 | + |
| 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")); |
| 76 | + |
| 77 | + // add address details |
| 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"); |
| 83 | + |
| 84 | + // checkout |
| 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); |
| 97 | + }); |
| 98 | +}); |
0 commit comments