Created
January 6, 2012 14:34
-
-
Save tarun3kumar/1570847 to your computer and use it in GitHub Desktop.
Selenium 2 Sample test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package core; | |
import java.util.concurrent.TimeUnit; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.testng.ITestResult; | |
import org.testng.annotations.AfterMethod; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Parameters; | |
/** | |
* Sets up test bed | |
* | |
* @author tarunb | |
* | |
*/ | |
public class SelTestCase { | |
protected WebDriver driver; | |
@BeforeMethod | |
@Parameters("appURL") | |
public void startDriver(String appURL) { | |
driver = new FirefoxDriver(); | |
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); | |
driver.get(appURL); | |
} | |
@AfterMethod | |
public void closeDriver() { | |
driver.quit(); | |
} | |
} | |
package core; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.Keys; | |
import org.openqa.selenium.WebDriver; | |
/** | |
* Wraps selenium operations | |
* | |
* @author tarunb | |
* | |
*/ | |
public class ActionDriver { | |
protected WebDriver driver; | |
public ActionDriver(WebDriver driver) { | |
this.driver = driver; | |
} | |
public void type(By by, String testdata) { | |
driver.findElement(by).clear(); | |
driver.findElement(by).sendKeys(testdata); | |
} | |
public void typeKeys(By locator, Keys key) { | |
driver.findElement(locator).sendKeys(key); | |
} | |
public boolean isElementPresent(By locator) { | |
return driver.findElements(locator).size() > 0; | |
} | |
public void waitForElementPresent(By locator) { | |
for(int i=0; i<60; i++) { | |
if (isElementPresent(locator)) { | |
break; | |
} else { | |
try { | |
driver.wait(1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
public void click(By locator) { | |
driver.findElement(locator).click(); | |
} | |
public void clickAndWaitForElementPresent(By locator, By waitElement) { | |
click(locator); | |
waitForElementPresent(waitElement); | |
} | |
} | |
package pageelements; | |
import org.openqa.selenium.By; | |
/** | |
* Element locators for Google home page | |
* @author tarunb | |
* | |
*/ | |
public class GoogleHomePageElements { | |
private static String searchBox = "q"; | |
private static String searchButton = "btnG"; | |
public static By getSearchBox() { | |
return By.name(searchBox); | |
} | |
public static By getSearchButton() { | |
return By.name(searchButton); | |
} | |
} | |
package pageelements; | |
import org.openqa.selenium.By; | |
/** | |
* Element locators for Google search result page | |
* @author tarunb | |
* | |
*/ | |
public class GoogleSearchResultPageElements { | |
private static String resultStatus = "resultStats"; | |
public static By getResultStatus() { | |
return By.id(resultStatus); | |
} | |
} | |
package pageobjects; | |
import org.openqa.selenium.WebDriver; | |
import pageelements.GoogleHomePageElements; | |
import pageelements.GoogleSearchResultPageElements; | |
import core.ActionDriver; | |
/** | |
* Represents services offered by Google Home page | |
* | |
* @author tarunb | |
* | |
*/ | |
public class GoogleHomePage extends ActionDriver { | |
public GoogleHomePage(WebDriver driver) { | |
super(driver); | |
if (!isElementPresent((GoogleHomePageElements.getSearchBox()))) { | |
throw new IllegalStateException( | |
"This is not Google Search page. Page is: " | |
+ driver.getPageSource()); | |
} | |
} | |
public GoogleSearchResultPage searchGoogle(String testdata) { | |
type(GoogleHomePageElements.getSearchBox(), testdata); | |
clickAndWaitForElementPresent(GoogleHomePageElements.getSearchButton(), | |
GoogleSearchResultPageElements.getResultStatus()); | |
return new GoogleSearchResultPage(driver); | |
} | |
} | |
package pageobjects; | |
import org.openqa.selenium.WebDriver; | |
import core.ActionDriver; | |
/** | |
* Represents services offered by Google search result page | |
* | |
* @author tarunb | |
* | |
*/ | |
public class GoogleSearchResultPage extends ActionDriver{ | |
public GoogleSearchResultPage(WebDriver driver) { | |
super(driver); | |
} | |
} | |
package test; | |
import org.testng.annotations.Test; | |
import pageobjects.GoogleHomePage; | |
import core.SelTestCase; | |
/** | |
* Selenium Google search test | |
* | |
* @author tarunb | |
* | |
*/ | |
public class GoogleSearchTest extends SelTestCase { | |
@Test | |
public void testGoogleSearch() { | |
GoogleHomePage googleHomePage = new GoogleHomePage(driver); | |
googleHomePage.searchGoogle("Selenium HQ"); | |
assert driver.getPageSource().contains("seleniumhq"):"Selenium headquarter search failed"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment