Skip to content

Instantly share code, notes, and snippets.

@borodicht
Created December 12, 2024 14:37
Show Gist options
  • Save borodicht/ca1552355cab42fc5d3d4fe73f0d274e to your computer and use it in GitHub Desktop.
Save borodicht/ca1552355cab42fc5d3d4fe73f0d274e to your computer and use it in GitHub Desktop.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.Duration;
public class LoginTest {
WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.saucedemo.com/");
}
@Test
public void checkNegativeLoginWithEmptyUsername() {
driver.findElement(By.name("user-name")).sendKeys("");
driver.findElement(By.id("password")).sendKeys("secret_sauce");
driver.findElement(By.id("login-button")).click();
String errorMessage = driver.findElement(By.cssSelector("[data-test=error]")).getText();
Assert.assertEquals(errorMessage, "Epic sadface: Username is required");
}
@Test
public void checkNegativeLoginWithEmptyPassword() {
driver.findElement(By.id("user-name")).sendKeys("standard_user");
driver.findElement(By.id("password")).sendKeys("");
driver.findElement(By.id("login-button")).click();
String errorMessage = driver.findElement(By.cssSelector("[data-test=error]")).getText();
Assert.assertEquals(errorMessage, "Epic sadface: Password is required");
}
@Test
public void checkNegativeLogin() {
driver.findElement(By.id("user-name")).sendKeys("standard_user");
driver.findElement(By.id("password")).sendKeys("1234556780");
driver.findElement(By.id("login-button")).click();
String errorMessage = driver.findElement(By.cssSelector("[data-test=error]")).getText();
Assert.assertEquals(errorMessage, "Epic sadface: Username and password do not match any user in this service");
}
@Test
public void checkPositiveLogin() {
driver.findElement(By.id("user-name")).sendKeys("standard_user");
driver.findElement(By.id("password")).sendKeys("secret_sauce");
driver.findElement(By.id("login-button")).click();
Boolean isDisplayed = driver.findElement(By.cssSelector("[data-test=title]")).isDisplayed();
Assert.assertTrue(isDisplayed);
}
@AfterMethod
public void close() {
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment