package br.edu.ifrn.tads.teste.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Exemplo {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("teste de sofware");
element.submit();
System.out.println("O título da página é: " + driver.getTitle());
driver.quit();
}
}
<!DOCTYPE html>
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="reset" value="Clear" />
</form>
</body>
</html>
Encontra um elemento pelo atributo ID
Ex: id=loginForm
<!DOCTYPE html>
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="reset" value="Clear" />
</form>
</body>
</html>
Encontra um elemento pelo atributo name
Ex: name=username
<!DOCTYPE html>
<html>
<body>
<p>Deseja continuar?</p>
<a href="continue.html">Continuar</a>
<a href="cancel.html">Cancelar</a>
</body>
</html>
Localiza um elemento através do atributo href
Ex: link=continue.html
link=cancel.html
<!DOCTYPE html>
<html>
<body>
<form id="loginForm">
<input class="required" name="username" type="text" />
<input class="required passField" name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="reset" value="Clear" />
</form>
</body>
</html>
Localiza um elemento através do seletor CSS
Ex: css=input.required[type=text]
css=input[type=password]
<!DOCTYPE html>
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="reset" value="Clear" />
</form>
</body>
</html>
Localiza um elemento através na navegação nos elementos
Ex: document.forms[0].password
<!DOCTYPE html>
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="reset" value="Clear" />
</form>
</body>
</html>
Localiza um elemento através na navegação nos elementos
Ex: /input[3]
/input[@name="continue" and @type="reset"]
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.google.com.br/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testGoogleInstant() throws Exception {
driver.get(baseUrl + "/?gws_rd=ssl");
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys("teste de software");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try {
if (isElementPresent(By.linkText("Teste de software – Wikipédia, a enciclopédia livre"))) {
break;
}
} catch (Exception e) {}
Thread.sleep(1000);
}
}