Skip to content

Instantly share code, notes, and snippets.

/BasePage.java

Created Oct 15, 2013
Embed
What would you like to do?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Predicate;
public class BasePage {
private static final Integer EXPLICIT_TIMEOUT = 10;
private WebDriver browser;
private WebDriverWait waiter;
{
browser = BrowserManager.getBrowser();
waiter = new WebDriverWait(browser, EXPLICIT_TIMEOUT);
PageFactory.initElements(browser, this);
}
public BasePage(String url) {
navigateTo(url);
}
public WebDriver getBrowser() {
return browser;
}
public void navigateTo(String url) {
getBrowser().navigate().to(url);
}
public void close() {
if (getBrowser().getWindowHandles().size() == 1) {
BrowserManager.closeBrowser();
return;
}
getBrowser().close();
}
public void waitForTitle(final String title) {
waiter.until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver arg0) {
return title.equals(getTitle());
}
});
}
public String getTitle() {
return getBrowser().getTitle();
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public final class BrowserManager {
private static ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>();
public static void closeBrowser() {
if (driver.get() != null) {
driver.get().quit();
driver.set(null);
}
}
public static WebDriver getBrowser() {
if (driver.get() == null) {
driver.set(new ChromeDriver());
}
return driver.get();
}
}
public class Demo {
public static void main(String[] args) {
new Thread(new Runnable(){
@Override
public void run() {
GoogleHomePage google = new GoogleHomePage();
google.search("hello!");
google.waitForTitle("hello! - Поиск в Google");
System.out.println(google.getTitle());
google.close();
}}).start();
new Thread(new Runnable(){
@Override
public void run() {
GoogleHomePage google = new GoogleHomePage();
google.search("goodbye!");
google.waitForTitle("goodbye! - Поиск в Google");
System.out.println(google.getTitle());
google.close();
}}).start();
}
}
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class GoogleHomePage extends BasePage {
@FindBy(xpath = "//input[not(@disabled or @type='hidden')]")
private WebElement searchTextField;
public GoogleHomePage() {
super("http://google.ru");
}
public void search(String searchText) {
searchTextField.sendKeys(searchText);
searchTextField.submit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.