Atletas ELSA
Tiago, o dançarino:
Gabriel, o "Runner" (a partir de 13:40):
Testes com Selenium WebDriver
public class LoginTest { @Test public void loginTest() { WebDriver driver = new FirefoxDriver(); driver.get("localhost:8080/elsa/login"); } }
public class LoginTest { @Test public void loginTest() { WebDriver driver = new FirefoxDriver(); driver.get("localhost:8080/elsa/login"); driver.findElement(By.id("username")).sendKeys("usuario"); driver.findElement(By.id("password")).sendKeys("senha"); driver.findElement(By.id("loginButton")).click(); } }
public class LoginPage { public void login(WebDriver driver){ driver.findElement(By.id("username")).sendKeys("usuario"); driver.findElement(By.id("password")).sendKeys("senha"); driver.findElement(By.id("loginButton")).click(); } }
public class LoginTest { public LoginPage loginPage; public WebDriver driver; @Before public void setup() { loginPage = new LoginPage(); driver = new FirefoxDriver(); driver.get("localhost:8080/elsa/login"); } @Test public void loginTest() { loginPage.login(driver); String welcomeMessage = driver.findElement( By.xpath("/html/body/div/div[4]/div/ul/li/span")).getText(); assertEquals("Bem vindo ao Prontuário Eletrônico Online do ELSA.", welcomeMessage); } }
Aqui já passamos a instanciar LoginPage e o WebDriver no método setup que está anotado com @Before e, desta forma, é executado sempre antes de cada método anotado com @Test. Além disso, buscamos o valor da mensagem de boas vindas através do método findElement e utilizamos como parâmetro a busca de um elemento presente na página pelo xpath correspondente. Finalmente, fizemos uma asserção para garantir que a mensagem que aparece é a esperada.
Agora que nosso teste está pronto executamos esperando a barra verde que instantaneamente fica vermelha, informando que o nosso teste falhou. Aqui vale uma breve explicação. Como estamos trabalhando com ambiente web, devemos saber que nem sempre a resposta é imediata para as requisições feitas para os servidores onde estão as aplicações. Então o melhor a ser feito para contornarmos o problema é implementarmos uma condição esperada de parada com um timeout, desta maneira o teste pode ser executado com mais confiabilidade.
@Test public void loginTest() { loginPage.login(driver); ExpectedCondition<Boolean> expectedCondition = new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver webDriver) { webDriver.findElement(By.xpath("/html/body/div/div[4]/div/ul/li/span")); return true; } }; Wait<WebDriver> wait = new WebDriverWait(driver, 15); wait.until(expectedCondition); String welcomeMessage = driver.findElement( By.xpath("/html/body/div/div[4]/div/ul/li/span")).getText(); assertEquals("Bem vindo ao Prontuário Eletrônico Online do ELSA.", welcomeMessage); }
PrimeFaces autoComplete weird behavior
A few weeks ago, while changing the behavior of an auto-complete field we have in our system, I came across a very weird bug. The field was supposed to suggest a list of Technicians, based on the name the user typed.
I was asked to have it accept not only the name of the Technician but also their ids.
To make the long story short: we are using a PrimeFaces AutoComplete component with the forceSelection flag set to true. The problem was that even tough the user did not selected any of the suggestions on the list, the value he typed was still being submitted to the form.
At first this was not a problem, because our Converter is using the Technician id, and when searching for a name, there was no match. But when the search includes the id, undesired thing happens, i.e. the field which as supposed to be empty, was filled with the Technician's information whose id the user typed.
With the help of Firebug, I found out that the hidden input Primefaces uses to implement the auto-completion, was not being cleaned when none of the suggestions was picked, thus, submitting a value the user didn't know was there.
To solve the problem, I hacked into Primefaces's jar, and, after formatting the autocomplete.js with the help of my friend Eclipse, I found this line:
jQuery(this).val("");
On a lucky/educated guess, I added the following line below that one:
jQuery(a.jqh).val("");
After repacking the jar, the problem was gone. I am not sure if this is a bug or the correct behavior, but it happened in Primefaces version 2.2.1.