com.greenpepper.extensions.selenium.SeleniumRemoteControlFixture Maven / Gradle / Ivy
The newest version!
package com.greenpepper.extensions.selenium;
import java.util.HashMap;
import java.util.Map;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
/**
* SeleniumRemoteControlFixture class.
*
* @author oaouattara
* @version $Id: $Id
*/
public class SeleniumRemoteControlFixture
{
private static final String DEFAULT_WAIT_TIMEOUT = "30000";
private Selenium selenium;
private Map variables = new HashMap();
/**
* Constructor for SeleniumRemoteControlFixture.
*/
public SeleniumRemoteControlFixture()
{
}
/**
* Constructor for SeleniumRemoteControlFixture.
*
* @param selenium a {@link com.thoughtworks.selenium.Selenium} object.
*/
public SeleniumRemoteControlFixture(Selenium selenium)
{
this.selenium = selenium;
}
/**
* getSystemUnderTest.
*
* @return a {@link com.thoughtworks.selenium.Selenium} object.
*/
@com.greenpepper.reflect.SystemUnderTest
public Selenium getSystemUnderTest()
{
return selenium;
}
/**
* startBrowserWithSeleniumConsoleOnAtPortAndScriptsAt.
*
* @param browserName a {@link java.lang.String} object.
* @param serverHost a {@link java.lang.String} object.
* @param serverPort a int.
* @param browserUrl a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean startBrowserWithSeleniumConsoleOnAtPortAndScriptsAt(String browserName, String serverHost, int serverPort, String browserUrl)
{
selenium = new DefaultSelenium(serverHost, serverPort, "*" + browserName, browserUrl);
selenium.start();
return true;
}
/**
* userOpensUrl.
*
* @param url a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean userOpensUrl(String url)
{
selenium.open(url);
return true;
}
/**
* shutdownBrowser.
*
* @return a boolean.
*/
public boolean shutdownBrowser()
{
selenium.stop();
return true;
}
/**
* text.
*
* @param locator a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String text(String locator)
{
return selenium.getText(locator);
}
/**
* value.
*
* @param locator a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String value(String locator)
{
return selenium.getValue(locator);
}
// =====================================
// Selenium commands
// =====================================
/**
* assertAlert.
*
* @param alert a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean assertAlert(String alert)
{
return selenium.getAlert().equals(alert);
}
/**
* assertAlertPresent.
*
* @return a boolean.
*/
public boolean assertAlertPresent()
{
return selenium.isAlertPresent();
}
/**
* assertAlertNotPresent.
*
* @return a boolean.
*/
public boolean assertAlertNotPresent()
{
return !assertAlertPresent();
}
/**
* assertElementPresent.
*
* @param text a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean assertElementPresent (String text)
{
return selenium.isElementPresent(text);
}
/**
* assertElementNotPresent.
*
* @param text a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean assertElementNotPresent (String text)
{
return !assertElementPresent(text);
}
/**
* assertExpression.
*
* @param variableNameExpression a {@link java.lang.String} object.
* @param expectedValue a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean assertExpression(String variableNameExpression, String expectedValue)
{
Expression expression = new Expression(variableNameExpression);
return expectedValue.equals(this.variables.get(expression.parseVariableName()));
}
/**
* assertFailureOnNext.
*/
public void assertFailureOnNext()
{
}
/**
* assertTextPresent.
*
* @param text a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean assertTextPresent (String text)
{
return selenium.isTextPresent(text);
}
/**
* assertTextNotPresent.
*
* @param text a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean assertTextNotPresent (String text)
{
return !assertTextPresent(text);
}
/**
* assertTitle.
*
* @param title a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean assertTitle (String title)
{
return title.equals(selenium.getTitle());
}
/**
* assertNotTitle.
*
* @param title a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean assertNotTitle(String title)
{
return !assertTitle(title);
}
/**
* clickAndWait.
*
* @param element a {@link java.lang.String} object.
*/
public void clickAndWait (String element)
{
selenium.click(element);
selenium.waitForPageToLoad(DEFAULT_WAIT_TIMEOUT);
}
/**
* storeAlert.
*
* @param variableName a {@link java.lang.String} object.
*/
public void storeAlert(String variableName)
{
this.variables.put(variableName, selenium.getAlert());
}
/**
* verifyAlert.
*
* @param alert a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean verifyAlert(String alert)
{
return assertAlert(alert);
}
/**
* verifyAlertPresent.
*
* @return a boolean.
*/
public boolean verifyAlertPresent()
{
return assertAlertPresent();
}
/**
* verifyAlertNotPresent.
*
* @return a boolean.
*/
public boolean verifyAlertNotPresent()
{
return !verifyAlertPresent();
}
/**
* verifyElementPresent.
*
* @param text a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean verifyElementPresent (String text)
{
return assertElementPresent(text);
}
/**
* verifyElementNotPresent.
*
* @param text a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean verifyElementNotPresent (String text)
{
return !verifyElementPresent(text);
}
/**
* verifyExpression.
*
* @param expression a {@link java.lang.String} object.
* @param expectedValue a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean verifyExpression(String expression, String expectedValue)
{
return assertExpression(expression, expectedValue);
}
/**
* verifyTextPresent.
*
* @param text a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean verifyTextPresent (String text)
{
return assertTextPresent(text);
}
/**
* verifyTextNotPresent.
*
* @param text a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean verifyTextNotPresent (String text)
{
return !verifyTextPresent(text);
}
/**
* verifyTitle.
*
* @param title a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean verifyTitle(String title)
{
return assertTitle(title);
}
/**
* verifyNotTitle.
*
* @param title a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean verifyNotTitle(String title)
{
return !verifyTitle(title);
}
/**
* waitForAlertPresent.
*/
public void waitForAlertPresent()
{
selenium.waitForCondition("selenium.browserbot.hasAlerts()", DEFAULT_WAIT_TIMEOUT);
}
}