package br.com.alura.leilao.login;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import org.junit.Assert; import org.junit.Test; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
private static final String URL_LOGIN = "http://localhost:8080/login";
private WebDriver browser;
@BeforeAll
public static void beforeAll() {
System.setProperty("webdriver.chrome.driver", "C:/Ws_GdriverOficial/chromedriver_v88.exe");
}
@BeforeEach
public void beforeEach() {
this.browser = new ChromeDriver();
this.browser.navigate().to(URL_LOGIN);
}
@AfterEach
public void AfterEach() {
this.browser.quit();
}
@Test
public void validaLogin() {
browser.findElement(By.cssSelector("input[name^='username']")).sendKeys("fulano");
browser.findElement(By.cssSelector("input[name^='password']")).sendKeys("pass");
browser.findElement(By.cssSelector("button[class^='btn btn-primary']")).click();
WebElement expected = browser.findElement(By.xpath("//span[text()='fulano']"));
String expectedResult = "fulano";
String result = expected.getText();
assertFalse(browser.getCurrentUrl().equals(URL_LOGIN));
assertEquals(expectedResult, result);
}
@Test
public void naoDeveriaLogarComDadosInvalidos() {
this.browser.findElement(By.cssSelector("input[name^='username']")).sendKeys("ivalido");
browser.findElement(By.cssSelector("input[name^='password']")).sendKeys("pass");
browser.findElement(By.cssSelector("button[class^='btn btn-primary']")).click();
Assert.assertTrue(browser.getCurrentUrl().equals("http://localhost:8080/login?error"));
Assert.assertTrue(browser.getPageSource().contains("Usuário e senha inválidos."));
Assert.assertThrows(NoSuchElementException.class,
() -> browser.findElement(By.xpath("//span[text()='fulano']")));
}
}