Created
May 7, 2026 14:24
-
-
Save borodicht/e90d3204dd58f6c4323140621631ef19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pages; | |
| import org.openqa.selenium.WebDriver; | |
| public class BasePage { | |
| WebDriver driver; | |
| public final String BASE_URL = "https://www.saucedemo.com/"; | |
| public BasePage(WebDriver driver) { | |
| this.driver = driver; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pages; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.WebDriver; | |
| public class LoginPage extends BasePage{ | |
| private final By USERNAME_FIELD = By.id("user-name"); | |
| private final By PASSWORD_FIELD = By.id("password"); | |
| private final By LOGIN_BUTTON = By.id("login-button"); | |
| private final By ERROR_MESSAGE = By.cssSelector("[data-test=error]"); | |
| public LoginPage(WebDriver driver) { | |
| super(driver); | |
| } | |
| public void open() { | |
| driver.get(BASE_URL); | |
| } | |
| public void login(String user, String password) { | |
| driver.findElement(USERNAME_FIELD).sendKeys(user); | |
| driver.findElement(PASSWORD_FIELD).sendKeys(password); | |
| driver.findElement(LOGIN_BUTTON).click(); | |
| } | |
| public String getErrorMessage() { | |
| return driver.findElement(ERROR_MESSAGE).getText(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pages; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.WebDriver; | |
| public class ProductsPage extends BasePage{ | |
| private final By TITLE = By.cssSelector("[data-test = title]"); | |
| private final By CART = By.cssSelector("[data-test = shopping-cart-link]"); | |
| private final String ADD_TO_CART_PATTERN = | |
| "//*[text()='%s']//ancestor::div[@class='inventory_item']//button[text()='Add to cart']"; | |
| public ProductsPage(WebDriver driver) { | |
| super(driver); | |
| } | |
| public void open() { | |
| driver.get(BASE_URL + "/inventory.html"); | |
| } | |
| public String getTitle() { | |
| return driver.findElement(TITLE).getText(); | |
| } | |
| public void addToCart(String product) { | |
| driver.findElement(By.xpath(String.format(ADD_TO_CART_PATTERN, product))).click(); | |
| } | |
| public void clickCart() { | |
| driver.findElement(CART).click(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tests; | |
| import org.openqa.selenium.WebDriver; | |
| import org.openqa.selenium.chrome.ChromeDriver; | |
| import org.openqa.selenium.chrome.ChromeOptions; | |
| import org.testng.annotations.AfterMethod; | |
| import org.testng.annotations.BeforeMethod; | |
| import pages.LoginPage; | |
| import pages.ProductsPage; | |
| import java.time.Duration; | |
| import java.util.HashMap; | |
| public class BaseTest { | |
| WebDriver driver; | |
| LoginPage loginPage; | |
| ProductsPage productsPage; | |
| @BeforeMethod(alwaysRun = true) | |
| public void setUp() { | |
| ChromeOptions options = new ChromeOptions(); | |
| HashMap<String, Object> chromePrefs = new HashMap<>(); | |
| chromePrefs.put("credentials_enable_service", false); | |
| chromePrefs.put("profile.password_manager_enabled", false); | |
| options.setExperimentalOption("prefs", chromePrefs); | |
| options.addArguments("--incognito"); | |
| options.addArguments("--disable-notifications"); | |
| options.addArguments("--disable-popup-blocking"); | |
| options.addArguments("--disable-infobars"); | |
| driver = new ChromeDriver(options); | |
| driver.manage().window().maximize(); | |
| driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); | |
| loginPage = new LoginPage(driver); | |
| productsPage = new ProductsPage(driver); | |
| } | |
| @AfterMethod(alwaysRun = true) | |
| public void tearDown() { | |
| if (driver != null) { | |
| driver.quit(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tests; | |
| import org.testng.annotations.Test; | |
| public class CartTest extends BaseTest { | |
| String expectedProductName = "Sauce Labs Backpack"; | |
| String expectedPriceOfProduct = "29.99"; | |
| @Test | |
| public void checkCart() { | |
| //логинимся | |
| loginPage.open(); | |
| loginPage.login("standard_user", "secret_sauce"); | |
| //добавляем товары в корзину | |
| productsPage.addToCart("Sauce Labs Backpack"); | |
| productsPage.addToCart("Test.allTheThings() T-Shirt (Red)"); | |
| //нажимаем на кнопку корзина | |
| productsPage.clickCart(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tests; | |
| import org.testng.annotations.Test; | |
| import static org.testng.Assert.assertEquals; | |
| public class LoginTest extends BaseTest { | |
| @Test | |
| public void checkLoginWithPositiveCred() { | |
| loginPage.open(); | |
| loginPage.login("standard_user", "secret_sauce"); | |
| assertEquals(productsPage.getTitle(), | |
| "Products", | |
| "SO BAAAAAAD"); | |
| } | |
| @Test | |
| public void chekLoginWithEmptyUserName() { | |
| loginPage.open(); | |
| loginPage.login("", "secret_sauce"); | |
| assertEquals(loginPage.getErrorMessage(), | |
| "Epic sadface: Username is required", | |
| "SO BAAAAAD"); | |
| } | |
| @Test | |
| public void chekLoginWithEmptyPassword() { | |
| loginPage.open(); | |
| loginPage.login("standard_user", ""); | |
| assertEquals(loginPage.getErrorMessage(), | |
| "Epic sadface: Password is required", | |
| "SO BBAAAAD"); | |
| } | |
| @Test | |
| public void chekLoginWithNegativeCred() { | |
| loginPage.open(); | |
| loginPage.login("test", "test"); | |
| assertEquals(loginPage.getErrorMessage(), | |
| "Epic sadface: Username and password do not match any user in this service", | |
| "SO BAAAAD"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment