Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#username"}

Quando vou executar o Run As JUnit, estou tendo o retorno de erro: **org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {​​​​​​​"method":"css selector","selector":"#username"}​​​​​​​ (Session info: chrome=92.0.4515.159) For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: 'RSI1518', ip: '10.18.0.55', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '16.0.2' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {​​​​​​​acceptInsecureCerts: false, browserName: chrome, browserVersion: 92.0.4515.159, chrome: {​​​​​​​chromedriverVersion: 92.0.4515.107 (87a818b10553..., userDataDir: C:\Users\KEVIN~1.ABR\AppDat...}​​​​​​​, goog:chromeOptions: {​​​​​​​debuggerAddress: localhost:49331}​​​​​​​, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {​​​​​​​implicit: 0, pageLoad: 300000, script: 30000}​​​​​​​, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}​​​​​​​ Session ID: 91fc83d600735416429757f6ed801aa8

  • Element info: {​​​​​​​Using=id, value=username}​​​​​​​ ** Como posso resolver isso?

Código completo de login válido e inválido.

package br.com.alura.leilao.login;

import org.junit.Assert; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; 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", "drivers/chromedriver.exe");
}

@BeforeEach
public void beforeEach(){
    this.browser = new ChromeDriver();
    this.browser.navigate().to(URL_LOGIN); //irá abrir o navegador
}

@AfterEach
public void afterEach(){
    this.browser.quit();
}

@Test
public void deveriaEfetuarLoginComDadosValidos() {

    WebDriver browser = new ChromeDriver();//irá estanciar o driver, o google chrome        

    browser.findElement(By.id("username")).sendKeys("fulano"); //preenchi o campo usuario com fulano
    browser.findElement(By.id("password")).sendKeys("pass"); //preenchi o campo senha com pass
    browser.findElement(By.id("login-form")).submit();

    Assert.assertFalse(browser.getCurrentUrl().equals(URL_LOGIN));
    Assert.assertEquals("fulano", browser.findElement(By.id("usuario-logado")).getText());

}

@Test
public void naoDeveriaLogarComDadosInvalidos() {

    WebDriver browser = new ChromeDriver();//irá estanciar o driver, o google chrome

    browser.findElement(By.id("username")).sendKeys("invalido"); //preenchi o campo usuario com invalido
    browser.findElement(By.id("password")).sendKeys("1234"); //preenchi o campo senha com pass
    browser.findElement(By.id("login-form")).submit();

    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.id("usuario-logado")));

}    

}

1 resposta
solução!

Pessoal, ja descobri o problema.

Foi falta de atenção minha mesmo.

Esqueci de apagar o "WebDriver browser = new ChromeDriver();" , dentro de cada teste. Não já tinha necessidade por conta before que estava utilizando.

@BeforeEach public void beforeEach(){ this.browser = new ChromeDriver(); this.browser.navigate().to(URL_LOGIN);