日期:2014-05-20 浏览次数:20736 次
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SeleniumTest {
protected WebDriver driver;
@Before
public void setUp() {
driver = new SafariDriver();
}
@After
public void cleanUp() {
// Close the browser
driver.quit();
}
@Test
public void search() {
// And now use this to visit Baidu
driver.get("http://www.baidu.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");
// Find the text input element by its id
WebElement element = driver.findElement(By.id("kw"));
// Enter something to search for
element.sendKeys("Cheese!");
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Now submit the form. WebDriver will find the form for us from the element
// driver.findElement(By.id("su")).click();
element.submit();
// Baidu's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 20 seconds
(new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return driver.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "Cheese!_百度搜索"
System.out.println("Page title is: " + driver.getTitle());
}
}