3
respostas

AssertionError - Cap 2 - assertTrue(achouNome)

Galera já tentei colocar Thread.sleep(10); depois do submit...

Já tentei colocar WebDriveWait ... WebDriver driver; driver = new FirefoxDriver(); WebDriverWait wait = new WebDriverWait(driver, 3); wait.until(ExpectedConditions.elementToBeClickable(By.name("usuario.nome")));

Já tentei colocar o driver.close(); antes do assertTrue.

Mas não consegui nada, o Selenium continua dando erro no teste de achar o nome e e-mail na tela após o cadastro.

Código atual: public class UsuariosSystemTest {

@Test public void deveAdicionarUmUsuario() throws InterruptedException {

WebDriver driver; System.setProperty("webdriver.gecko.driver","C:\develop\geckodriver.exe"); // driver =new FirefoxDriver();// driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

driver = new FirefoxDriver(); WebDriverWait wait = new WebDriverWait(driver, 3); wait.until(ExpectedConditions.elementToBeClickable(By.name("usuario.nome")));

driver.get("http://localhost:8080/usuarios/new"); //// WebElement nome = driver.findElement(By.name("usuario.nome")); WebElement email = driver.findElement(By.name("usuario.email"));

nome.sendKeys("ph"); email.sendKeys("ph@gmail.com"); nome.submit();

// Thread.sleep(10);

boolean achouNome = driver.getPageSource().contains("pg"); boolean achouEmail = driver.getPageSource().contains("ph@gmail.com");

// driver.close();

assertTrue(achouNome); assertTrue(achouEmail);

driver.close();

}

}

3 respostas

Paulo, na última aula a gente mostra um recurso pode ajudar nisso, que é a classe WebDriverWait.

Acho que bem válido você conseguir ver o restante do curso.

Vou falar com a galera o que a gente pode fazer para melhorar isso, pois nessa nova versão do Selenium ele está com esse problema.

Cara já tentei usando o WebDriverWait, assim oh: Estou usando-o errado ?

public class UsuariosSystemTest {

@Test public void deveAdicionarUmUsuario() throws InterruptedException {

WebDriver driver; System.setProperty("webdriver.gecko.driver","C:\develop\geckodriver.exe");

driver = new FirefoxDriver(); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.elementToBeClickable(By.name("usuario.nome")));

driver.get("http://localhost:8080/usuarios/new");

WebElement nome = driver.findElement(By.name("usuario.nome")); WebElement email = driver.findElement(By.name("usuario.email"));

nome.sendKeys("ph"); email.sendKeys("ph@gmail.com"); nome.submit();

// Thread.sleep(10);

boolean achouNome = driver.getPageSource().contains("pg"); boolean achouEmail = driver.getPageSource().contains("ph@gmail.com");

// driver.close();

assertTrue(achouNome); assertTrue(achouEmail);

driver.close();

}

}

Fiz o seguinte:

boolean achouNome = new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElement(By.name("usuario.nome"), "ph"));

Mas ele apresenta o seguinte erro: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for text ('ph') to be present in element found by By.name: usuario.nome (tried for 10 second(s) with 500 MILLISECONDS interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:232) at br.com.caelum.teste.UsuariosSystemTest.deveAdicionarUmUsuario(UsuariosSystemTest.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.name: usuario.nome For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.4.0', revision: 'unknown', time: 'unknown' System info: host: 'N3CE-MTZ19', ip: '192.168.1.66', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131' Driver info: driver.version: unknown at org.openqa.selenium.support.ui.ExpectedConditions.lambda$findElement$0(ExpectedConditions.java:883) at java.util.Optional.orElseThrow(Unknown Source) at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:882) at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44) at org.openqa.selenium.support.ui.ExpectedConditions$13.apply(ExpectedConditions.java:385) at org.openqa.selenium.support.ui.ExpectedConditions$13.apply(ExpectedConditions.java:381) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209) ... 24 more