Skip to content

Commit

Permalink
Make test classes use xpath expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
plenyi authored and pmuir committed Aug 24, 2010
1 parent 70eb9d9 commit 206b822
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 59 deletions.
Expand Up @@ -25,11 +25,15 @@
import static org.testng.Assert.assertTrue;

import org.jboss.test.selenium.AbstractTestCase;
import org.jboss.test.selenium.locator.XpathLocator;
import org.jboss.test.selenium.guard.request.RequestTypeGuardFactory;
import org.jboss.test.selenium.locator.IdLocator;
import org.jboss.test.selenium.locator.XpathLocator;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.jboss.test.selenium.locator.LocatorFactory.id;
import static org.jboss.test.selenium.locator.LocatorFactory.xp;

/**
* Tests login examples in Weld
*
Expand All @@ -38,37 +42,43 @@
*/
public class CommonLoginTest extends AbstractTestCase
{

protected String MAIN_PAGE = "/home.jsf";
protected String LOGGED_IN = "xpath=//li[contains(text(),'Welcome')]";
protected String LOGGED_OUT = "xpath=//li[contains(text(),'Goodbye')]";

protected String USERNAME_FIELD = "id=loginForm:username";
protected String PASSWORD_FIELD = "id=loginForm:password";

protected String LOGIN_BUTTON = "id=loginForm:login";
protected String LOGOUT_BUTTON = "id=loginForm:logout";
protected XpathLocator LOGGED_IN = xp("//li[contains(text(),'Welcome')]");
protected XpathLocator LOGGED_OUT = xp("//li[contains(text(),'Goodbye')]");

protected IdLocator USERNAME_FIELD = id("loginForm:username");
protected IdLocator PASSWORD_FIELD = id("loginForm:password");

protected IdLocator LOGIN_BUTTON = id("loginForm:login");
protected IdLocator LOGOUT_BUTTON = id("loginForm:logout");

@BeforeMethod
public void openStartUrl()
{
selenium.open(contextPath);
waitModel.until(elementPresent.locator(USERNAME_FIELD));
}

@Test
public void loginTest()
{
assertFalse(selenium.isElementPresent(new XpathLocator(LOGGED_IN)), "User should not be logged in!");
selenium.type(new XpathLocator(USERNAME_FIELD), "demo");
selenium.type(new XpathLocator(PASSWORD_FIELD), "demo");
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(LOGIN_BUTTON));
assertTrue(selenium.isElementPresent(new XpathLocator(LOGGED_IN)), "User should be logged in!");
assertFalse(selenium.isElementPresent(LOGGED_IN), "User should not be logged in!");
selenium.type(USERNAME_FIELD, "demo");
selenium.type(PASSWORD_FIELD, "demo");
RequestTypeGuardFactory.waitHttp(selenium).click(LOGIN_BUTTON);
assertTrue(selenium.isElementPresent(LOGGED_IN), "User should be logged in!");
}

@Test
public void logoutTest()
{
assertFalse(selenium.isElementPresent(new XpathLocator(LOGGED_IN)), "User should not be logged in!");
selenium.type(new XpathLocator(USERNAME_FIELD), "demo");
selenium.type(new XpathLocator(PASSWORD_FIELD), "demo");
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(LOGIN_BUTTON));
assertTrue(selenium.isElementPresent(new XpathLocator(LOGGED_IN)), "User should be logged in!");
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(LOGOUT_BUTTON));
assertTrue(selenium.isElementPresent(new XpathLocator(LOGGED_OUT)), "User should not be logged in!");
assertFalse(selenium.isElementPresent(LOGGED_IN), "User should not be logged in!");
selenium.type(USERNAME_FIELD, "demo");
selenium.type(PASSWORD_FIELD, "demo");
RequestTypeGuardFactory.waitHttp(selenium).click(LOGIN_BUTTON);
assertTrue(selenium.isElementPresent(LOGGED_IN), "User should be logged in!");
RequestTypeGuardFactory.waitHttp(selenium).click(LOGOUT_BUTTON);
assertTrue(selenium.isElementPresent(LOGGED_OUT), "User should not be logged in!");
}

}
Expand Up @@ -25,11 +25,14 @@
import static org.testng.Assert.fail;

import org.jboss.test.selenium.AbstractTestCase;
import org.jboss.test.selenium.locator.XpathLocator;
import org.jboss.test.selenium.guard.request.RequestTypeGuardFactory;
import org.jboss.test.selenium.locator.IdLocator;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.jboss.test.selenium.locator.LocatorFactory.id;

/**
* Tests numberguess examples in Weld
*
Expand All @@ -41,17 +44,30 @@ public class CommonNumberGuessTest extends AbstractTestCase
{

protected String MAIN_PAGE = "/home.jsf";
protected String GUESS_MESSAGES = "id=numberGuess:messages";
protected IdLocator GUESS_MESSAGES = id("numberGuess:messages");

protected String GUESS_FIELD = "id=numberGuess:inputGuess";
protected String GUESS_SUBMIT = "id=numberGuess:guessButton";
protected String GUESS_RESET = "id=numberGuess:resetButton";
protected String GUESS_SMALLEST = "id=numberGuess:smallest";
protected String GUESS_BIGGEST = "id=numberGuess:biggest";
protected IdLocator GUESS_FIELD = id("numberGuess:inputGuess");
protected IdLocator GUESS_SUBMIT = id("numberGuess:guessButton");
protected IdLocator GUESS_RESET = id("numberGuess:resetButton");
protected IdLocator GUESS_SMALLEST = id("numberGuess:smallest");
protected IdLocator GUESS_BIGGEST = id("numberGuess:biggest");

protected String WIN_MSG = "Correct!";
protected String LOSE_MSG = "No guesses left!";

@BeforeMethod
public void openStartUrl()
{
selenium.open(contextPath);
waitModel.until(elementPresent.locator(GUESS_FIELD));
}

@AfterMethod
public void resetSession()
{
selenium.deleteAllVisibleCookies();
}

@Test
public void smartTest()
{
Expand All @@ -68,11 +84,11 @@ public void smartTest()
fail("Game should not be longer than 10 guesses");
}

assertTrue(selenium.isElementPresent(new XpathLocator(GUESS_SMALLEST)), "Expected smallest number on page");
assertTrue(selenium.isElementPresent(new XpathLocator(GUESS_BIGGEST)), "Expected biggest number on page");
assertTrue(selenium.isElementPresent(GUESS_SMALLEST), "Expected smallest number on page");
assertTrue(selenium.isElementPresent(GUESS_BIGGEST), "Expected biggest number on page");

min = Integer.parseInt(selenium.getText(new XpathLocator(GUESS_SMALLEST)));
max = Integer.parseInt(selenium.getText(new XpathLocator(GUESS_BIGGEST)));
min = Integer.parseInt(selenium.getText(GUESS_SMALLEST));
max = Integer.parseInt(selenium.getText(GUESS_BIGGEST));
guess = min + ((max - min) / 2);
enterGuess(guess);
i++;
Expand Down Expand Up @@ -103,8 +119,8 @@ public void linearTest()

protected void enterGuess(int guess)
{
selenium.type(new XpathLocator(GUESS_FIELD), String.valueOf(guess));
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(GUESS_SUBMIT));
selenium.type(GUESS_FIELD, String.valueOf(guess));
RequestTypeGuardFactory.waitHttp(selenium).click(GUESS_SUBMIT);
}

protected boolean isOnGuessPage()
Expand All @@ -114,13 +130,13 @@ protected boolean isOnGuessPage()

protected boolean isOnWinPage()
{
String text = selenium.getText(new XpathLocator(GUESS_MESSAGES));
String text = selenium.getText(GUESS_MESSAGES);
return WIN_MSG.equals(text);
}

protected boolean isOnLosePage()
{
String text = selenium.getText(new XpathLocator(GUESS_MESSAGES));
String text = selenium.getText(GUESS_MESSAGES);
return LOSE_MSG.equals(text);
}

Expand Down
Expand Up @@ -24,11 +24,15 @@
import static org.testng.Assert.assertTrue;

import org.jboss.test.selenium.AbstractTestCase;
import org.jboss.test.selenium.locator.XpathLocator;
import org.jboss.test.selenium.guard.request.RequestTypeGuardFactory;
import org.jboss.test.selenium.locator.IdLocator;
import org.jboss.test.selenium.locator.XpathLocator;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.jboss.test.selenium.locator.LocatorFactory.id;
import static org.jboss.test.selenium.locator.LocatorFactory.xp;

/**
* Tests permalink example in Weld. The first test just adds comment on the first
* topic and returns back. Second test tests permanent link from jsf2.
Expand All @@ -40,35 +44,42 @@ public class PermalinkTest extends AbstractTestCase
{
protected String MAIN_PAGE = "/home.jsf";
protected String PAGE_TITLE = "Direct links to the news you crave";
protected String VIEW_ENTRY_LINK = "xpath=//a[contains(text(),'View Entry')][1]";
protected XpathLocator VIEW_ENTRY_LINK = xp("//a[contains(text(),'View Entry')][1]");
protected String TOPIC_TITLE = "Mojarra == RI";
protected String PERMALINK_LINK = "xpath=//a[@title='A bookmarkable link for this entry.'][1]";
protected String AUTHOR_INPUT = "id=author";
protected String COMMENT_INPUT = "id=body";
protected String SUBMIT_BUTTON = "id=post";
protected String BACK_BUTTON = "xpath=//input[@type='button'][@value='Back to main page']";
protected XpathLocator PERMALINK_LINK = xp("//a[@title='A bookmarkable link for this entry.'][1]");
protected IdLocator AUTHOR_INPUT = id("author");
protected IdLocator COMMENT_INPUT = id("body");
protected IdLocator SUBMIT_BUTTON = id("post");
protected XpathLocator BACK_BUTTON = xp("//input[@type='button'][@value='Back to main page']");
protected String COMMENT_TEXT = "This is my first comment on Mojarra project";
protected String AUTHOR_NAME = "Martin";
protected String TEXT_ON_HOME_PAGE = "Annotation nation";

@BeforeMethod
public void openStartUrl()
{
selenium.open(contextPath);
waitModel.until(elementPresent.locator(VIEW_ENTRY_LINK));
}

@Test
public void addCommentOnTopicTest()
{
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(VIEW_ENTRY_LINK));
RequestTypeGuardFactory.waitHttp(selenium).click(VIEW_ENTRY_LINK);
assertTrue(selenium.isTextPresent(TOPIC_TITLE), "Topic title expected on the page");
selenium.type(new XpathLocator(AUTHOR_INPUT), AUTHOR_NAME);
selenium.type(new XpathLocator(COMMENT_INPUT), COMMENT_TEXT);
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(SUBMIT_BUTTON));
selenium.type(AUTHOR_INPUT, AUTHOR_NAME);
selenium.type(COMMENT_INPUT, COMMENT_TEXT);
RequestTypeGuardFactory.waitHttp(selenium).click(SUBMIT_BUTTON);
assertTrue(selenium.isTextPresent(AUTHOR_NAME), "A name of comment's author expected");
assertTrue(selenium.isTextPresent(COMMENT_TEXT), "A text of entered comment expected");
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(BACK_BUTTON));
RequestTypeGuardFactory.waitHttp(selenium).click(BACK_BUTTON);
assertTrue(selenium.isTextPresent(TEXT_ON_HOME_PAGE), "Home page expected");
}

@Test(dependsOnMethods="addCommentOnTopicTest")
public void permanentLinkTest()
{
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(PERMALINK_LINK));
RequestTypeGuardFactory.waitHttp(selenium).click(PERMALINK_LINK);
assertTrue(selenium.isTextPresent(AUTHOR_NAME), "A name of comment's author expected");
assertTrue(selenium.isTextPresent(COMMENT_TEXT), "A text of entered comment expected");
}
Expand Down
Expand Up @@ -24,11 +24,13 @@
import static org.testng.Assert.assertTrue;

import org.jboss.test.selenium.AbstractTestCase;
import org.jboss.test.selenium.locator.XpathLocator;
import org.jboss.test.selenium.guard.request.RequestTypeGuardFactory;
import org.jboss.test.selenium.locator.IdLocator;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.jboss.test.selenium.locator.LocatorFactory.id;

/**
* Tests translator example in Weld
*
Expand All @@ -38,21 +40,28 @@
public class TranslatorTest extends AbstractTestCase
{
protected String MAIN_PAGE = "/home.jsf";
protected String INPUT_AREA = "id=TranslatorMain:text";
protected String TRANSLATE_BUTTON = "id=TranslatorMain:button";
protected IdLocator INPUT_AREA = id("TranslatorMain:text");
protected IdLocator TRANSLATE_BUTTON = id("TranslatorMain:button");
protected String ONE_SENTENCE = "This is only one sentence.";
protected String MORE_SENTENCES = "First sentence. Second and last sentence.";
protected String ONE_SENTENCE_TRANSLATED = "Lorem ipsum dolor sit amet.";
protected String MORE_SENTENCES_TRANSLATED = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";

@BeforeMethod
public void openStartUrl()
{
selenium.open(contextPath);
waitModel.until(elementPresent.locator(INPUT_AREA));
}

@Test
public void translateTest()
{
browser.type(new XpathLocator(INPUT_AREA), ONE_SENTENCE);
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(TRANSLATE_BUTTON));
assertTrue(browser.isTextPresent(ONE_SENTENCE_TRANSLATED), "One sentence translated into latin expected.");
browser.type(new XpathLocator(INPUT_AREA), MORE_SENTENCES);
RequestTypeGuardFactory.waitHttp(selenium).click(new XpathLocator(TRANSLATE_BUTTON));
assertTrue(browser.isTextPresent(MORE_SENTENCES_TRANSLATED), "More sentences translated into latin expected.");
selenium.type(INPUT_AREA, ONE_SENTENCE);
RequestTypeGuardFactory.waitHttp(selenium).click(TRANSLATE_BUTTON);
assertTrue(selenium.isTextPresent(ONE_SENTENCE_TRANSLATED), "One sentence translated into latin expected.");
selenium.type(INPUT_AREA, MORE_SENTENCES);
RequestTypeGuardFactory.waitHttp(selenium).click(TRANSLATE_BUTTON);
assertTrue(selenium.isTextPresent(MORE_SENTENCES_TRANSLATED), "More sentences translated into latin expected.");
}
}

0 comments on commit 206b822

Please sign in to comment.