|
| 1 | +package stepdefinitions; |
| 2 | + |
| 3 | +import io.cucumber.java.After; |
| 4 | +import io.cucumber.java.en.And; |
| 5 | +import io.cucumber.java.en.Given; |
| 6 | +import io.cucumber.java.en.Then; |
| 7 | +import io.cucumber.java.en.When; |
| 8 | +import org.openqa.selenium.By; |
| 9 | +import org.openqa.selenium.WebDriver; |
| 10 | +import org.openqa.selenium.WebElement; |
| 11 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 12 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 13 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 14 | +import org.testng.Assert; |
| 15 | + |
| 16 | +import java.net.URL; |
| 17 | +import java.time.Duration; |
| 18 | + |
| 19 | +public class bstack_test_add_to_cart { |
| 20 | + |
| 21 | + private RemoteWebDriver driver; |
| 22 | + private String notedProductName; |
| 23 | + |
| 24 | + @Given("I open the bstack demo homepage") |
| 25 | + public void i_open_the_bstack_demo_homepage() throws Exception { |
| 26 | + DesiredCapabilities caps = new DesiredCapabilities(); |
| 27 | + caps.setBrowserName("chrome"); |
| 28 | + |
| 29 | + // Set the BrowserStack HUB URL |
| 30 | + driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps); |
| 31 | + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); |
| 32 | + driver.get("https://www.bstackdemo.com"); |
| 33 | + } |
| 34 | + |
| 35 | + @Then("the bstack demo title should be {string}") |
| 36 | + public void the_bstack_demo_title_should_be(String expected) { |
| 37 | + Assert.assertTrue(driver.getTitle().contains(expected), "Expected title to contain: " + expected + " but was: " + driver.getTitle()); |
| 38 | + } |
| 39 | + |
| 40 | + @When("I note the name of the first product") |
| 41 | + public void i_note_the_name_of_the_first_product() { |
| 42 | + WebElement nameEl = driver.findElement(By.xpath("//*[@id='1']/p")); |
| 43 | + notedProductName = nameEl.getText(); |
| 44 | + Assert.assertNotNull(notedProductName, "Product name should not be null"); |
| 45 | + } |
| 46 | + |
| 47 | + @And("I add the first product to the cart") |
| 48 | + public void i_add_the_first_product_to_the_cart() { |
| 49 | + driver.findElement(By.xpath("//*[@id='1']/div[4]")) |
| 50 | + .click(); |
| 51 | + } |
| 52 | + |
| 53 | + @Then("the mini cart should be displayed") |
| 54 | + public void the_mini_cart_should_be_displayed() { |
| 55 | + WebElement cart = driver.findElement(By.cssSelector(".float\\-cart__content")); |
| 56 | + Assert.assertTrue(cart.isDisplayed(), "Mini cart not displayed"); |
| 57 | + } |
| 58 | + |
| 59 | + @And("the product name in the cart should match noted name") |
| 60 | + public void the_product_name_in_the_cart_should_match_noted_name() { |
| 61 | + WebElement nameInCart = driver.findElement(By.xpath("//*[@id='__next']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]")); |
| 62 | + String cartName = nameInCart.getText(); |
| 63 | + Assert.assertEquals(cartName, notedProductName, "Product names differ between main page and cart"); |
| 64 | + } |
| 65 | + |
| 66 | + @After |
| 67 | + public void tearDown() { |
| 68 | + if (driver != null) { |
| 69 | + try { driver.quit(); } catch (Exception ignored) {} |
| 70 | + driver = null; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments