Skip to content

Commit

Permalink
Convert ftests to Graphene/WebDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Smeral committed Oct 15, 2012
1 parent b8ec666 commit add07e8
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 306 deletions.
5 changes: 2 additions & 3 deletions examples/jsf/login/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@
</dependency>

<dependency>
<groupId>org.jboss.arquillian.ajocado</groupId>
<artifactId>arquillian-ajocado-junit</artifactId>
<groupId>org.jboss.arquillian.graphene</groupId>
<artifactId>graphene-webdriver</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,22 @@
package org.jboss.weld.examples.login.ftest;

import java.net.URL;

import org.jboss.arquillian.ajocado.framework.AjaxSelenium;
import org.jboss.arquillian.ajocado.locator.IdLocator;
import org.jboss.arquillian.ajocado.locator.XPathLocator;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.drone.api.annotation.Drone;
import static org.jboss.arquillian.graphene.Graphene.element;
import static org.jboss.arquillian.graphene.Graphene.waitModel;
import static org.jboss.arquillian.graphene.Graphene.guardHttp;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.jboss.arquillian.ajocado.locator.LocatorFactory.id;
import static org.jboss.arquillian.ajocado.locator.LocatorFactory.xp;
import static org.jboss.arquillian.ajocado.Ajocado.elementPresent;
import static org.jboss.arquillian.ajocado.Ajocado.waitForHttp;
import static org.jboss.arquillian.ajocado.Ajocado.waitModel;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
* Tests login examples in Weld
Expand All @@ -54,53 +49,62 @@
@RunWith(Arquillian.class)
@RunAsClient
public class CommonLoginTest {
protected String MAIN_PAGE = "/home.jsf";
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");

protected String MAIN_PAGE = "/home.jsf";
private static final By LOGGED_IN = By.xpath("//li[contains(text(),'Welcome')]");
private static final By LOGGED_OUT = By.xpath("//li[contains(text(),'Goodbye')]");
private static final By USERNAME_FIELD = By.id("loginForm:username");
private static final By PASSWORD_FIELD = By.id("loginForm:password");
private static final By LOGIN_BUTTON = By.id("loginForm:login");
private static final By LOGOUT_BUTTON = By.id("loginForm:logout");

@Drone
AjaxSelenium selenium;
WebDriver driver;

@ArquillianResource
private URL contextPath;

@Deployment(testable = false)
public static WebArchive createTestDeployment1() {
return Deployments.createDeployment();
}

@Before
public void openStartUrl() {
selenium.deleteAllVisibleCookies();
selenium.open(contextPath);
driver.manage().deleteAllCookies();
driver.navigate().to(contextPath);
}

@Test
public void loginTest() {
waitModel.until(elementPresent.locator(USERNAME_FIELD));
assertFalse("User should not be logged in!", selenium.isElementPresent(LOGGED_IN));
selenium.type(USERNAME_FIELD, "demo");
selenium.type(PASSWORD_FIELD, "demo");
waitForHttp(selenium).click(LOGIN_BUTTON);
assertTrue("User should be logged in!", selenium.isElementPresent(LOGGED_IN));
waitModel(driver).until(element(USERNAME_FIELD).isPresent());
assertFalse("User should not be logged in!", element(LOGOUT_BUTTON).isPresent().apply(driver));

driver.findElement(USERNAME_FIELD).clear();
driver.findElement(USERNAME_FIELD).sendKeys("demo");

driver.findElement(PASSWORD_FIELD).clear();
driver.findElement(PASSWORD_FIELD).sendKeys("demo");

guardHttp(driver.findElement(LOGIN_BUTTON)).click();
assertTrue("User should be logged in!", element(LOGGED_IN).isPresent().apply(driver));
}

@Test
public void logoutTest() {
waitModel.until(elementPresent.locator(USERNAME_FIELD));
assertFalse("User should not be logged in!", selenium.isElementPresent(LOGOUT_BUTTON));
selenium.type(USERNAME_FIELD, "demo");
selenium.type(PASSWORD_FIELD, "demo");
waitForHttp(selenium).click(LOGIN_BUTTON);
assertTrue("User should be logged in!", selenium.isElementPresent(LOGGED_IN));
waitForHttp(selenium).click(LOGOUT_BUTTON);
assertTrue("User should not be logged in!", selenium.isElementPresent(LOGGED_OUT));
}
waitModel(driver).until(element(USERNAME_FIELD).isPresent());
assertFalse("User should not be logged in!", element(LOGOUT_BUTTON).isPresent().apply(driver));

driver.findElement(USERNAME_FIELD).clear();
driver.findElement(USERNAME_FIELD).sendKeys("demo");

driver.findElement(PASSWORD_FIELD).clear();
driver.findElement(PASSWORD_FIELD).sendKeys("demo");

guardHttp(driver.findElement(LOGIN_BUTTON)).click();
assertTrue("User should be logged in!", element(LOGGED_IN).isPresent().apply(driver));

guardHttp(driver.findElement(LOGOUT_BUTTON)).click();
assertTrue("User should not be logged in!", element(LOGGED_OUT).isPresent().apply(driver));
}
}
6 changes: 3 additions & 3 deletions examples/jsf/numberguess/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
</dependency>

<dependency>
<groupId>org.jboss.arquillian.ajocado</groupId>
<artifactId>arquillian-ajocado-junit</artifactId>
<groupId>org.jboss.arquillian.graphene</groupId>
<artifactId>graphene-webdriver</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>

</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,23 @@

import java.net.MalformedURLException;
import java.net.URL;

import org.jboss.arquillian.ajocado.framework.AjaxSelenium;
import org.jboss.arquillian.ajocado.locator.IdLocator;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.drone.api.annotation.Drone;
import static org.jboss.arquillian.graphene.Graphene.waitModel;
import static org.jboss.arquillian.graphene.Graphene.element;
import static org.jboss.arquillian.graphene.Graphene.guardHttp;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.jboss.arquillian.ajocado.locator.LocatorFactory.id;
import static org.jboss.arquillian.ajocado.Ajocado.elementPresent;
import static org.jboss.arquillian.ajocado.Ajocado.waitForHttp;
import static org.jboss.arquillian.ajocado.Ajocado.waitModel;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
* Tests numberguess examples in Weld
Expand All @@ -57,37 +54,35 @@
public class CommonNumberGuessTest {

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

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 By GUESS_MESSAGES = By.id("numberGuess:messages");
protected By GUESS_FIELD = By.id("numberGuess:inputGuess");
protected By GUESS_SUBMIT = By.id("numberGuess:guessButton");
protected By GUESS_RESET = By.id("numberGuess:resetButton");
protected By GUESS_SMALLEST = By.id("numberGuess:smallest");
protected By GUESS_BIGGEST = By.id("numberGuess:biggest");
protected String WIN_MSG = "Correct!";
protected String LOSE_MSG = "No guesses left!";

@Drone
AjaxSelenium selenium;
WebDriver driver;

@ArquillianResource
private URL contextPath;

@Deployment(testable = false)
public static WebArchive createTestDeployment1() {
return Deployments.createDeployment();
}

@Before
public void openStartUrl() throws MalformedURLException {
selenium.open(new URL(contextPath.toString() + "home.jsf"));
waitModel.until(elementPresent.locator(GUESS_FIELD));
driver.navigate().to(new URL(contextPath.toString() + "home.jsf"));
waitModel(driver).until(element(GUESS_FIELD).isPresent());
}

@After
public void resetSession() {
selenium.deleteAllVisibleCookies();
driver.manage().deleteAllCookies();
}

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

assertTrue("Expected smallest number on page", selenium.isElementPresent(GUESS_SMALLEST));
assertTrue("Expected biggest number on page", selenium.isElementPresent(GUESS_BIGGEST));
assertTrue("Expected smallest number on page", element(GUESS_SMALLEST).isPresent().apply(driver));
assertTrue("Expected biggest number on page", element(GUESS_BIGGEST).isPresent().apply(driver));

min = Integer.parseInt(driver.findElement(GUESS_SMALLEST).getText());
max = Integer.parseInt(driver.findElement(GUESS_BIGGEST).getText());

min = Integer.parseInt(selenium.getText(GUESS_SMALLEST));
max = Integer.parseInt(selenium.getText(GUESS_BIGGEST));
guess = min + ((max - min) / 2);
enterGuess(guess);
i++;
Expand All @@ -132,22 +128,22 @@ public void linearTest() {
}

protected void enterGuess(int guess) {
selenium.type(GUESS_FIELD, String.valueOf(guess));
waitForHttp(selenium).click(GUESS_SUBMIT);
driver.findElement(GUESS_FIELD).clear();
driver.findElement(GUESS_FIELD).sendKeys(String.valueOf(guess));
guardHttp(driver.findElement(GUESS_SUBMIT)).click();
}

protected boolean isOnGuessPage() {
return !(isOnWinPage() || isOnLosePage());
}

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

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

}
Loading

0 comments on commit add07e8

Please sign in to comment.