Created
February 3, 2026 17:43
-
-
Save borodicht/84da2277fb1bd605905eeb5b328d8feb 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 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 | |
| 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 close() { | |
| 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 pages; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.WebDriver; | |
| public class LoginPage { | |
| WebDriver driver; | |
| By usernameField = By.id("user-name"); | |
| By passwordField = By.id("password"); | |
| By loginButton = By.id("login-button"); | |
| By errorMessage = By.cssSelector("[data-test='error']"); | |
| public LoginPage(WebDriver driver) { | |
| this.driver = driver; | |
| } | |
| public void open() { | |
| driver.get("https://www.saucedemo.com/"); | |
| } | |
| public void login(String user, String password) { | |
| driver.findElement(usernameField).sendKeys(user); | |
| driver.findElement(passwordField).sendKeys(password); | |
| driver.findElement(loginButton).click(); | |
| } | |
| public String getErrorMessage() { | |
| return driver.findElement(errorMessage).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 tests; | |
| import org.openqa.selenium.By; | |
| import org.testng.Assert; | |
| import org.testng.annotations.Test; | |
| public class LoginTest extends BaseTest{ | |
| /* | |
| 1. Логин с пустым паролем !!! | |
| 2. Логин с пустым юзером !!! | |
| 3. Логин с невалидными значениями !!! | |
| 4. Позитивный логин !!! | |
| */ | |
| @Test | |
| public void checkLogInWithEmptyPassword() { | |
| loginPage.open(); | |
| loginPage.login("standard_user", ""); | |
| Assert.assertEquals(loginPage.getErrorMessage(), "Epic sadface: Password is required"); | |
| } | |
| @Test | |
| public void checkLogInWithEmptyUsername() { | |
| loginPage.open(); | |
| loginPage.login("", "secret_sauce"); | |
| Assert.assertEquals(loginPage.getErrorMessage(), "Epic sadface: Username is required"); | |
| } | |
| @Test | |
| public void checkLogInWithNegativeValue() { | |
| loginPage.open(); | |
| loginPage.login("test","test"); | |
| Assert.assertEquals(loginPage.getErrorMessage(), | |
| "Epic sadface: Username and password do not match any user in this service"); | |
| } | |
| @Test | |
| public void checkLogInWithPositiveValue() { | |
| loginPage.open(); | |
| loginPage.login("standard_user","secret_sauce"); | |
| Assert.assertTrue(productsPage.titleIsDisplayed()); | |
| } | |
| } |
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 { | |
| WebDriver driver; | |
| By title = By.cssSelector("[data-test='title']"); | |
| public ProductsPage(WebDriver driver) { | |
| this.driver = driver; | |
| } | |
| public boolean titleIsDisplayed() { | |
| return driver.findElement(title).isDisplayed(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment