Skip to content

Instantly share code, notes, and snippets.

@tarun3kumar
Created January 6, 2012 14:13
Show Gist options
  • Save tarun3kumar/1570788 to your computer and use it in GitHub Desktop.
Save tarun3kumar/1570788 to your computer and use it in GitHub Desktop.
Selenium 1 Sample test
package core;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
/**
* Sets up test bed
*
* @author tarunb
*
*/
public class SelTestCase {
protected SeleniumServer seleniumServer;
protected Selenium selenium;
@BeforeSuite
public void startSeleniumServer() throws Exception {
seleniumServer = new SeleniumServer();
seleniumServer.start();
}
@AfterSuite
public void stopSeleniumServer() {
seleniumServer.stop();
}
@BeforeClass
@Parameters( { "serverHost", "serverPort", "browser", "appURL" })
public void startBrowser(String serverHost, int serverPort, String browser,
String appURL) {
selenium = new DefaultSelenium(serverHost, serverPort, browser, appURL);
selenium.start();
selenium.windowMaximize();
selenium.open(appURL);
}
@AfterClass
public void closeBrowser() {
selenium.close();
}
}
package core;
import com.thoughtworks.selenium.Selenium;
/**
* Wraps selenium operations
*
* @author tarunb
*
*/
public class ActionDriver {
protected Selenium selenium;
public static final String MAX_Wait_Period = "60000";
public ActionDriver(Selenium selenium) {
this.selenium = selenium;
}
public void type(String locator, String testdata) {
selenium.type(locator, testdata);
}
public void waitForPageToLoad() {
selenium.waitForPageToLoad(MAX_Wait_Period);
}
public void click(String locator) {
selenium.click(locator);
}
public void clickAndWaitForPageToLoad(String locator) {
selenium.click(locator);
waitForPageToLoad();
}
public void clickAndWaitForElementPresent(String locator, String waitElement) {
selenium.click(locator);
selenium.waitForCondition("selenium.isElementPresent(\""+waitElement+"\")", MAX_Wait_Period);
}
public boolean isElementPresent(String locator) {
return selenium.isElementPresent(locator);
}
}
package pageelements;
/**
* Element locators for Google home page
* @author tarunb
*
*/
public class GoogleHomePageElements {
private static String searchBox = "q";
private static String searchButton = "btnG";
public static String getSearchBox() {
return searchBox;
}
public static String getSearchButton() {
return searchButton;
}
}
package pageelements;
/**
* Element locators for Google search result page
* @author tarunb
*
*/
public class GoogleSearchResultPageElements {
private static String resultStatus = "resultStats";
public static String getResultStatus() {
return resultStatus;
}
}
package pageobjects;
import pageelements.GoogleHomePageElements;
import pageelements.GoogleSearchResultPageElements;
import com.thoughtworks.selenium.Selenium;
import core.ActionDriver;
/**
* Represents services offered by Google Home page
*
* @author tarunb
*
*/
public class GoogleHomePage extends ActionDriver {
public GoogleHomePage(Selenium selenium) {
super(selenium);
if (!isElementPresent((GoogleHomePageElements.getSearchBox()))) {
throw new IllegalStateException(
"This is not Google Search page. Page is: "
+ selenium.getHtmlSource());
}
}
public GoogleSearchResultPage searchGoogle(String testdata) {
type(GoogleHomePageElements.getSearchBox(), testdata);
clickAndWaitForElementPresent(GoogleHomePageElements.getSearchButton(),
GoogleSearchResultPageElements.getResultStatus());
return new GoogleSearchResultPage(selenium);
}
}
package pageobjects;
import com.thoughtworks.selenium.Selenium;
import core.ActionDriver;
/**
* Represents services offered by Google search result page
*
* @author tarunb
*
*/
public class GoogleSearchResultPage extends ActionDriver{
public GoogleSearchResultPage(Selenium selenium) {
super(selenium);
}
}
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(selenium);
googleHomePage.searchGoogle("Selenium HQ");
assert selenium.isTextPresent("seleniumhq.org/"):"Selenium headquarter search failed";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment