Skip to content

Instantly share code, notes, and snippets.

@OdaShinsuke
Created February 19, 2012 03:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OdaShinsuke/1861829 to your computer and use it in GitHub Desktop.
Save OdaShinsuke/1861829 to your computer and use it in GitHub Desktop.
BingSearch_WebDriver_java
package webdrivertest;
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BingSearch {
private final RemoteWebDriver driver;
public static BingSearch create(RemoteWebDriver driver) {
BingSearch instance = new BingSearch(driver);
driver.navigate().to("http://www.bing.com");
return instance;
}
private BingSearch(RemoteWebDriver driver) {
this.driver = driver;
}
private WebElement txt条件() {
return driver.findElementByName("q");
}
private WebElement btn検索() {
return driver.findElementByXPath("//input[@type='submit' and @name='go']");
}
private WebElement lbl件数() {
return driver.findElementById("count");
}
public void input検索条件(String 条件) {
this.txt条件().clear();
this.txt条件().sendKeys(条件);
}
public void click検索() {
this.btn検索().click();
Wait<WebDriver> wait = new WebDriverWait(driver, 10);
wait.until(titleIs("Microsoft - Bing"));
}
public String get検索結果件数() {
return this.lbl件数().getText().replaceAll("(.*of )(.*)( results)", "$2");
}
}
package webdrivertest;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class NoPageObjectTest {
@Test
public void 検索() {
RemoteWebDriver driver = new InternetExplorerDriver();
try {
driver.navigate().to("http://www.bing.com");
WebElement txt条件 = driver.findElementByName("q");
txt条件.clear();
txt条件.sendKeys("Microsoft");
WebElement btn検索 = driver.findElementByXPath("//input[@type='submit' and @name='go']");
btn検索.click();
Wait<WebDriver> wait = new WebDriverWait(driver, 10);
wait.until(titleIs("Microsoft - Bing"));
WebElement lbl件数 = driver.findElementById("count");
assertThat(lbl件数.getText().replaceAll("(.*of )(.*)( results)", "$2"), is("527,000,000"));
} finally {
driver.quit();
}
}
}
package webdrivertest;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class PageObjectTest {
@Test
public void 検索() {
RemoteWebDriver driver = new InternetExplorerDriver();
try {
BingSearch instance = BingSearch.create(driver);
instance.input検索条件("Microsoft");
instance.click検索();
assertThat(instance.get検索結果件数(), is("527,000,000"));
} finally {
driver.quit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment