Created
December 17, 2024 14:32
-
-
Save borodicht/b1e6eec747a16a146881f49176b357f3 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.*; | |
import java.time.Duration; | |
public class BaseTest { | |
WebDriver driver; | |
LoginPage loginPage; | |
ProductsPage productsPage; | |
CheckoutInfo checkoutInfo; | |
CompletePage completePage; | |
OverviewPage overviewPage; | |
CartPage cartPage; | |
@BeforeMethod | |
public void setup() { | |
ChromeOptions options = new ChromeOptions(); | |
// options.addArguments("--headless"); | |
options.addArguments("--start-maximized"); | |
driver = new ChromeDriver(options); | |
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); | |
loginPage = new LoginPage(driver); | |
productsPage = new ProductsPage(driver); | |
checkoutInfo = new CheckoutInfo(driver); | |
completePage = new CompletePage(driver); | |
overviewPage = new OverviewPage(driver); | |
cartPage = new CartPage(driver); | |
} | |
@AfterMethod | |
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 CartPage { | |
WebDriver driver; | |
By checkoutButton = By.id("checkout"); | |
public CartPage(WebDriver driver) { | |
this.driver = driver; | |
} | |
public CheckoutInfo clickCheckoutButton() { | |
driver.findElement(checkoutButton).click(); | |
return new CheckoutInfo(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 dto.Customer; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
public class CheckoutInfo { | |
WebDriver driver; | |
By firstNameField = By.id("first-name"); | |
By lastNameField = By.id("last-name"); | |
By zipCodeField = By.id("postal-code"); | |
By continueButton = By.id("continue"); | |
public CheckoutInfo(WebDriver driver) { | |
this.driver = driver; | |
} | |
public CheckoutInfo writeInfo(Customer customer) { | |
driver.findElement(firstNameField).sendKeys(customer.getFirstName()); | |
driver.findElement(lastNameField).sendKeys(customer.getLastName()); | |
driver.findElement(zipCodeField).sendKeys(customer.getZipCode()); | |
return this; | |
} | |
public OverviewPage clickContinue() { | |
driver.findElement(continueButton).click(); | |
return new OverviewPage(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 tests; | |
import dto.Customer; | |
import org.testng.Assert; | |
import org.testng.annotations.Test; | |
import static org.testng.Assert.assertEquals; | |
public class CheckOutTest extends BaseTest { | |
@Test | |
public void checkoutTest() { | |
Customer customer = new Customer("Ivan", "Petrov", "123456"); | |
loginPage.open() | |
.login("standard_user", "secret_sauce") | |
.addToCart("Sauce Labs Bolt T-Shirt") | |
.addToCart("Sauce Labs Onesie") | |
.clickToCart() | |
.clickCheckoutButton() | |
.writeInfo(customer) | |
.clickContinue() | |
.clickFinish(); | |
assertEquals(completePage.getFinishMessage(), "Thank you for your order!"); | |
} | |
} |
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 CompletePage { | |
WebDriver driver; | |
public CompletePage(WebDriver driver) { | |
this.driver = driver; | |
} | |
public String getFinishMessage() { | |
return driver.findElement(By.xpath("//*[@class = 'complete-header']")).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 dto; | |
public class Customer { | |
String firstName; | |
String lastName; | |
String zipCode; | |
public Customer(String firstName, String lastName, String zipCode) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.zipCode = zipCode; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String getZipCode() { | |
return zipCode; | |
} | |
public void setZipCode(String zipCode) { | |
this.zipCode = zipCode; | |
} | |
} |
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 loginField = By.name("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 LoginPage open() { | |
driver.get("https://www.saucedemo.com/"); | |
return this; | |
} | |
public void loginNegative(String user, String password) { | |
driver.findElement(loginField).sendKeys(user); | |
driver.findElement(passwordField).sendKeys(password); | |
driver.findElement(loginButton).click(); | |
} | |
public ProductsPage login(String user, String password) { | |
driver.findElement(loginField).sendKeys(user); | |
driver.findElement(passwordField).sendKeys(password); | |
driver.findElement(loginButton).click(); | |
return new ProductsPage(driver); | |
} | |
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.testng.Assert; | |
import org.testng.annotations.Test; | |
public class LoginTest extends BaseTest { | |
@Test | |
public void checkNegativeLoginWithEmptyUsername() { | |
loginPage.open(); | |
loginPage.loginNegative("", "secret_sauce"); | |
Assert.assertEquals(loginPage.getErrorMessage(), "Epic sadface: Username is required"); | |
} | |
@Test | |
public void checkNegativeLoginWithEmptyPassword() { | |
loginPage.open(); | |
loginPage.loginNegative("standard_user", ""); | |
Assert.assertEquals(loginPage.getErrorMessage(), "Epic sadface: Password is required"); | |
} | |
@Test | |
public void checkNegativeLogin() { | |
loginPage.open(); | |
loginPage.loginNegative("standard_user", "12345678"); | |
Assert.assertEquals(loginPage.getErrorMessage(), "Epic sadface: Username and password do not match any user in this service"); | |
} | |
@Test | |
public void checkPositiveLogin() { | |
loginPage.open(); | |
loginPage.login("standard_user", "secret_sauce"); | |
Assert.assertTrue(productsPage.isPageOpened()); | |
} | |
} |
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 OverviewPage { | |
WebDriver driver; | |
By finishButton = By.id("finish"); | |
public OverviewPage(WebDriver driver) { | |
this.driver = driver; | |
} | |
public CompletePage clickFinish() { | |
driver.findElement(finishButton).click(); | |
return new CompletePage(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 ProductsPage { | |
WebDriver driver; | |
By title = By.cssSelector("[data-test=title]"); | |
By cartLink = By.id("shopping_cart_container"); | |
String addToCart = "//div[text()='%s']/ancestor::div[@class='inventory_item']//button"; | |
public ProductsPage(WebDriver driver) { | |
this.driver = driver; | |
} | |
public boolean isPageOpened() { | |
return driver.findElement(title).isDisplayed(); | |
} | |
public CartPage clickToCart() { | |
driver.findElement(cartLink).click(); | |
return new CartPage(driver); | |
} | |
public ProductsPage addToCart(String product) { | |
driver.findElement(By.xpath(String.format(addToCart, product))).click(); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment