Olá, pessoal, fiquei um tempo sem mexer no projeto e tinha parado na parte aonde refatoramos o código para o padrão Page Objects. Estranhamente, apesar de que na página existe um usuário com o nome e o e-mail informado e até mesmo do debugger eu validei que a informação vem como true para usuário existe, o JUnit está marcando o teste como Failure na asserção.
package br.com.caelum.test;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CreateUserTests {
private WebDriver driver;
private UsersPage userPage;
@BeforeEach
public void beforeEach() {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\KELVINOLIVEIRADASILV\\Documents\\Selenium\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
userPage = new UsersPage(driver);
}
@AfterEach
public void afterEach(){
driver.close();
}
@Test
public void shouldAddUser(){
userPage.visit()
.newUser()
.register(UserValidInput.NAME, UserValidInput.EMAIL);
assertTrue(userPage.itExistsOnList(UserValidInput.NAME, UserValidInput.EMAIL));
}
@Test
public void shouldShowValidationErrors() {
WebElement textBoxUser, textBoxEmail, btnSave;
User user = new User(UserInvalidInput.BLANK_NAME, UserInvalidInput.BLANK_EMAIL);
textBoxUser = driver.findElement(By.name(UserFields.Names.TEXTBOX_NAME));
textBoxEmail = driver.findElement(By.name(UserFields.Names.TEXTBOX_EMAIL));
btnSave = driver.findElement(By.id(UserFields.Ids.BUTTON_SAVE));
textBoxUser.sendKeys(user.getName());
textBoxEmail.sendKeys(user.getEmail());
btnSave.click();
boolean isNameMandatoryErrorMessageVisible = driver.getPageSource().contains(UserValidationErrorMessages.NAME_IS_MANDATORY);
boolean isEmailMandatoryErrorMessageVisible = driver.getPageSource().contains(UserValidationErrorMessages.EMAIL_IS_MANDATORY);
user.setName(UserValidInput.NAME);
user.setEmail(UserInvalidInput.INVALID_EMAIL);
textBoxUser = driver.findElement(By.name(UserFields.Names.TEXTBOX_NAME));
textBoxEmail = driver.findElement(By.name(UserFields.Names.TEXTBOX_EMAIL));
btnSave = driver.findElement(By.id(UserFields.Ids.BUTTON_SAVE));
textBoxEmail.sendKeys(user.getEmail());
btnSave.click();
boolean isEmailInvalid = driver.getPageSource().contains(UserValidationErrorMessages.EMAIL_INVALID);
assertTrue(isNameMandatoryErrorMessageVisible);
assertTrue(isEmailMandatoryErrorMessageVisible);
assertTrue(isEmailInvalid);
}
}
package br.com.caelum.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class NewUserPage {
private WebDriver driver;
public NewUserPage(WebDriver driver) {
this.driver = driver;
}
public void register(String name, String email) {
User user = new User(name, email);
WebElement textBoxName = driver.findElement(By.name(UserFields.Names.TEXTBOX_NAME));
WebElement textBoxEmail = driver.findElement(By.name(UserFields.Names.TEXTBOX_EMAIL));
textBoxName.sendKeys(user.getName());
textBoxEmail.sendKeys(user.getEmail());
textBoxName.submit();
}
}
package br.com.caelum.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class UsersPage {
private WebDriver driver;
public UsersPage(WebDriver driver) {
this.driver = driver;
}
public UsersPage visit() {
driver.get("http://localhost:8080/usuarios");
return this;
}
public NewUserPage newUser() {
driver.findElement(By.linkText("Novo Usuário")).click();
return new NewUserPage(driver);
}
public boolean itExistsOnList(String name, String email) {
return driver.getPageSource().contains(name) &&
driver.getPageSource().contains(email);
}
}
package br.com.caelum.test;
public final class UserValidInput {
public static final String NAME = "Ronaldo Luiz Albuquerque";
public static final String EMAIL = "ronaldo2009@terra.com.br";
}
https://fatecspgov-my.sharepoint.com/:i:/g/personal/kelvin_silva2_fatec_sp_gov_br/Eb7WUzByel1KruhfDkeUK2MBWiiVmFa6KuVePCLx2tl-Eg?e=mrvV7f Link para imagem de erro de asserção: https://fatecspgov-my.sharepoint.com/:i:/g/personal/kelvin_silva2_fatec_sp_gov_br/ES6dMb-YijFCtfKuJEJfpnMB_naYoJ4DbLbWJCvkbVBGsw?e=MH91CW
Link para imagem do site Leilões com usuários cadastrados: