E - Commerce
package guvinewtask;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration;
public class BaseTest { protected WebDriver driver; protected WebDriverWait wait;
@BeforeClass public void setup() { driver = new ChromeDriver(); driver.manage().window().maximize(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.get("https://www.bestbuy.com/"); }
@AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
class LoginTest extends BaseTest { @Test public void testLogin() { driver.get("https://www.bestbuy.com/identity/signin"); WebElement emailInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("fld-e"))); WebElement passwordInput = driver.findElement(By.id("fld-p1"));
emailInput.sendKeys("suganyukanss@gmail.com"); passwordInput.sendKeys("Suganya@123"); passwordInput.submit();
Assert.assertTrue(driver.getPageSource().contains("Welcome"), "Login failed"); } }
class NavigationTest extends BaseTest { @Test public void testMainMenuNavigation() { WebElement menuOption = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Menu']"))); menuOption.click();
WebElement departmentLink = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Shop by Department"))); departmentLink.click();
Assert.assertTrue(driver.getCurrentUrl().contains("departments")); } }
class CartTest extends BaseTest { @Test public void testAddItemToCart() { WebElement searchBox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("gh-search-input"))); searchBox.sendKeys("Laptop"); searchBox.submit();
WebElement firstItem = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".sku-item"))); firstItem.click();
WebElement addToCartButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".add-to-cart-button"))); addToCartButton.click();
WebElement cartCount = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".cart-count"))); Assert.assertTrue(Integer.parseInt(cartCount.getText()) > 0, "Cart is empty"); } }
class CheckoutTest extends BaseTest { @Test public void testCheckout() { WebElement cartIcon = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href*='cart']"))); cartIcon.click();
WebElement checkoutButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-button-id='checkout']"))); checkoutButton.click();
WebElement paymentInfo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("credit-card-number"))); paymentInfo.sendKeys("4111111111111111");
WebElement submitOrder = driver.findElement(By.id("submit-order")); submitOrder.click();
Assert.assertTrue(driver.getPageSource().contains("Order Confirmation"), "Checkout failed"); } }
Facing issues in this portal could not able to redirect in the automation server - displayed error message when its start automating - in manual UI also not working properly - if we click cart button its redirect to this blank page





