Professional Documents
Culture Documents
Doc
Doc
This form is easy to pick up and is intuitive initially, but it has its problems
:
@Test
public void testCreateUser() throws Exception {
final String firstName = "firstNameSelenium";
final String lastName = "lastNameSelenium";
final String login = "loginSelenium";
selenium.click( "link=Menu" );
selenium.click( "usersMenuItem" );
waitForPageToLoad();
selenium.click( "registerNewUserLink" );
waitForPageToLoad();
Assert.assertTrue( selenium.isElementPresent( "userEditPage" ) );
// Register a new user
selenium.type( "form:firstName", firstName );
selenium.type( "form:lastName", lastName );
selenium.type( "form:login", login );
selenium.type( "password", "secretPassSelenium" );
selenium.type( "form:personalEmail", "perso@perso.com" );
selenium.select( "form:homeCountry", "value=FR" );
selenium.select( "form:access2Add", "label=Business Unit Manager" );
selenium.click( "form:industries:industryToggleLink" );
selenium.click( "form:saveButton" );
waitForPageToLoad();
Assert.assertTrue( selenium.isElementPresent( "userEditPage" ) );
}
Method Chaining :
* Change the signature of user behavior methods so that
they return the page object itself.
* Allows multiple behavior methods to be called in sequence
* All the Page object methods return a reference to page object
* If the "user action" moves the focus to a different page
the method should return that page object.
* Otherwise return the same page object.
Consider an example..
A user will try to login to our page and then will list all the latest news
from the main page using the Page Objects
}
//Each Data Page will have the corresponding manager class which drives the
actions that are performed on the page through the data objects that are accessi
ble
through getters.
public class GoogleHomePageDataManager
{
private final Selenium selenium;
public GoogleHomePageDataManager(Selenium sel)
{
this.selenium=sel;
}
public GoogleSearchResultsDataManager searchFor(String str)
{
selenium.type(GoogleHomePage.getSearchText(),str);
selenium.click(GoogleHomePage.getSearchButton());
selenium.waitForPageToLoad("5000");
return new GoogleSearchResultsDataManager(selenium,str);
}
}
public class GoogleSearchResultsDataManager
{
private final Selenium selenium;
public GoogleSearchResultsDataManager(Selenium sel,String str)
{
this.selenium=sel;
if(!(string + " - Google Search").equals(selenium.getTitle()))
{
throw new IllegalStateException("This is not the Google Results Page");
}
}
public String getResult(int i)
{
String nameXPath = "xpath=id('res')/div[1]/div[" + (i + 1) + "]/h2/a";
return selenium.getText(nameXPath);
}
}
//once the Data and Managers for each page is ready,we'll have the main fixture
//class which tests the scenario that needs to be tested by creating and invokin
g
//the methods from the DataManager classes.
public class GoogleTest {
private Selenium selenium;
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox",
"http://www.google.com/webhp?hl=en");
selenium.start();
}
@Test
//This test basically checsk to see that the "hello world" string should be
in the
//first page of the results returned.
public void helloWorldShouldBeInFirstPageOfResults()
{
GoogleHomePageDataManager home = new GoogleHomePageDataManager(selen
ium);
GoogleSearchResultsDataManager searchResults = home.searchFor("hello
world");
String firstEntry = searchResults.getResult(0);
assertEquals("Hello world program - Wikipedia, the free encyclopedia
", firstEntry);
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}