|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import io.cucumber.java.After; |
| 4 | +import io.cucumber.java.Before; |
| 5 | +import io.cucumber.java.en.And; |
| 6 | +import io.cucumber.java.en.Given; |
| 7 | +import io.cucumber.java.en.Then; |
| 8 | +import io.cucumber.java.en.When; |
| 9 | +import org.openqa.selenium.By; |
| 10 | +import org.openqa.selenium.WebDriver; |
| 11 | +import org.openqa.selenium.WebElement; |
| 12 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 13 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 14 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 15 | + |
| 16 | +import java.net.URL; |
| 17 | +import java.time.Duration; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | + |
| 21 | +public class StepDefs { |
| 22 | + |
| 23 | + private WebDriver driver; |
| 24 | + private String addedProductName; |
| 25 | + private static final String HUB_URL = "http://localhost:4444/wd/hub"; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void setUp() throws Exception { |
| 29 | + ChromeOptions chromeOptions = new ChromeOptions(); |
| 30 | + chromeOptions.addArguments( |
| 31 | + "--headless", |
| 32 | + "--no-first-run", |
| 33 | + "--no-default-browser-check", |
| 34 | + "--disable-extensions", |
| 35 | + "--disable-default-apps", |
| 36 | + "--disable-gpu", |
| 37 | + "--disable-dev-shm-usage", |
| 38 | + "--disable-software-rasterizer", |
| 39 | + "--no-sandbox", |
| 40 | + "--disable-background-timer-throttling", |
| 41 | + "--disable-backgrounding-occluded-windows", |
| 42 | + "--disable-renderer-backgrounding", |
| 43 | + "--disable-features=TranslateUI", |
| 44 | + "--disable-ipc-flooding-protection", |
| 45 | + "--disable-web-security", |
| 46 | + "--disable-features=VizDisplayCompositor", |
| 47 | + "--disable-logging", |
| 48 | + "--silent" |
| 49 | + ); |
| 50 | + |
| 51 | + driver = new RemoteWebDriver(new URL(HUB_URL), chromeOptions); |
| 52 | + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); |
| 53 | + driver.manage().window().maximize(); |
| 54 | + } |
| 55 | + |
| 56 | + @After |
| 57 | + public void tearDown() { |
| 58 | + if (driver != null) { |
| 59 | + driver.quit(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Given("I open the BrowserStack demo store") |
| 64 | + public void iOpenTheBrowserStackDemoStore() { |
| 65 | + driver.get("https://bstackdemo.com/"); |
| 66 | + } |
| 67 | + |
| 68 | + @And("I sign in to BrowserStack demo") |
| 69 | + public void iSignInToBrowserStackDemo() throws InterruptedException { |
| 70 | + driver.findElement(By.id("signin")).click(); |
| 71 | + driver.findElement(By.cssSelector("#username svg")).click(); |
| 72 | + driver.findElement(By.id("react-select-2-option-0-0")).click(); |
| 73 | + driver.findElement(By.cssSelector("#password svg")).click(); |
| 74 | + driver.findElement(By.id("react-select-3-option-0-0")).click(); |
| 75 | + driver.findElement(By.id("login-btn")).click(); |
| 76 | + Thread.sleep(500); |
| 77 | + } |
| 78 | + |
| 79 | + @When("I add the product at index {string} to my cart") |
| 80 | + public void iAddTheProductAtIndexToMyCart(String index) { |
| 81 | + WebElement productNameElem = driver.findElement(By.cssSelector("#\\3" + index + " > p")); |
| 82 | + addedProductName = productNameElem.getText(); |
| 83 | + WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\3" + index + " > .shelf-item__buy-btn")); |
| 84 | + addToCartBtn.click(); |
| 85 | + } |
| 86 | + |
| 87 | + @Then("the same product should appear in my cart") |
| 88 | + public void theSameProductShouldAppearInMyCart() { |
| 89 | + By cartProductTitle = By.cssSelector( |
| 90 | + "#__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 | + ); |
| 92 | + // The cart panel slides in async after the buy click; wait for its text to populate |
| 93 | + // before asserting, otherwise getText() can return an empty string. |
| 94 | + new WebDriverWait(driver, Duration.ofSeconds(10)) |
| 95 | + .until(d -> !d.findElement(cartProductTitle).getText().isEmpty()); |
| 96 | + assertEquals(addedProductName, driver.findElement(cartProductTitle).getText()); |
| 97 | + } |
| 98 | + |
| 99 | + @And("I close the cart panel") |
| 100 | + public void iCloseTheCartPanel() { |
| 101 | + driver.findElement(By.cssSelector("div.float-cart__close-btn")).click(); |
| 102 | + } |
| 103 | + |
| 104 | + @And("I proceed to checkout") |
| 105 | + public void iProceedToCheckout() { |
| 106 | + driver.findElement(By.cssSelector(".buy-btn")).click(); |
| 107 | + } |
| 108 | + |
| 109 | + @And("I fill in the shipping address") |
| 110 | + public void iFillInTheShippingAddress() { |
| 111 | + driver.findElement(By.id("firstNameInput")).sendKeys("first"); |
| 112 | + driver.findElement(By.id("lastNameInput")).sendKeys("last"); |
| 113 | + driver.findElement(By.id("addressLine1Input")).sendKeys("address"); |
| 114 | + driver.findElement(By.id("provinceInput")).sendKeys("province"); |
| 115 | + driver.findElement(By.id("postCodeInput")).sendKeys("pincode"); |
| 116 | + driver.findElement(By.id("checkout-shipping-continue")).click(); |
| 117 | + } |
| 118 | + |
| 119 | + @Then("I should see the confirmation message {string}") |
| 120 | + public void iShouldSeeTheConfirmationMessage(String expected) { |
| 121 | + String message = driver.findElement(By.id("confirmation-message")).getText(); |
| 122 | + assertEquals(expected, message); |
| 123 | + } |
| 124 | +} |
0 commit comments