Skip to content

Commit

Permalink
fix Selenium HomePage can be one of two urls.
Browse files Browse the repository at this point in the history
- clean up the rest of the pageObjects package

Signed-off-by: Duane May <[email protected]>
  • Loading branch information
duanemay committed Jul 5, 2024
1 parent fddf005 commit 0d3a595
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,13 @@ void simpleSamlPhpLoginDisplaysLastLogin() throws Exception {
createIdentityProvider(SAML_ORIGIN);

Long beforeTest = System.currentTimeMillis();
LoginPage.go(webDriver, baseUrl)
HomePage homePage = LoginPage.go(webDriver, baseUrl)
.clickSamlLink_goesToSamlLoginPage(SAML_ORIGIN)
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.logout_goesToLoginPage()
.clickSamlLink_goesToSamlLoginPage(SAML_ORIGIN)
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.hasLastLoginTime();
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword());
assertThat(homePage.hasLastLoginTime()).isTrue();

Long afterTest = System.currentTimeMillis();
String zoneAdminToken = IntegrationTestUtils.getClientCredentialsToken(serverRunning, "admin", "adminsecret");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.hamcrest.Matcher;
import org.openqa.selenium.WebDriver;

/**
* The CustomErrorPage class represents the custom error page on the UAA server.
*/
public class CustomErrorPage extends Page {

public CustomErrorPage(WebDriver driver, Matcher urlMatcher) {
public CustomErrorPage(WebDriver driver, Matcher<String> urlMatcher) {
super(driver);
validateUrl(driver, urlMatcher);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

import org.openqa.selenium.WebDriver;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

/**
* The FaviconElement class represents the favicon image on the UAA server.
*/
public class FaviconElement extends Page {

// The favicon.ico image is not present on the server because we specify a custom icon URL
// in the headers, but browsers try to hit it and tests need to hit this default URL.
/**
* Expect a 404 error when landing on the favicon URL.
*/
public FaviconElement(WebDriver driver) {
super(driver);
assertThat(driver.getCurrentUrl())
.as("Should be on the favicon image")
.endsWith("/favicon.ico");
assertThat(driver.getPageSource())
.contains("Something went amiss.");
}

/**
* Get the default favicon image.
* The favicon.ico image is not present on the server because we specify a custom icon URL
* in the headers, but browsers try to hit it and tests need to hit this default URL.
*/
static public FaviconElement getDefaultIcon(WebDriver driver, String baseUrl) {
driver.get(baseUrl + "/favicon.ico");
return new FaviconElement(driver);
}

// Expect a 404 error when landing on the favicon URL.
public FaviconElement(WebDriver driver) {
super(driver);
assertThat("Should be on the favicon image", driver.getCurrentUrl(), endsWith("/favicon.ico"));
assertThat(driver.getPageSource(), containsString("Something went amiss."));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
package org.cloudfoundry.identity.uaa.integration.pageObjects;

import org.cloudfoundry.identity.uaa.home.HomeController;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.ui.Model;

import java.security.Principal;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;

// TODO extend LoggedInPage
/**
* The HomePage class represents the home page on the UAA server.
* It can have either url: `/home` or just `/`.
* {@link HomeController#home(Model, Principal)}
*/
public class HomePage extends Page {
static final private String urlPath = "/";
static final private String slashUrlPath = "/";
static final private String homeUrlPath = "/home";

public HomePage(WebDriver driver) {
super(driver);
validateUrl(driver, endsWith(urlPath));
validateUrl(driver, anyOf(endsWith(slashUrlPath), endsWith(homeUrlPath)));
validatePageSource(driver, containsString("Where to?"));
}

static public LoginPage tryToGoHome_redirectsToLoginPage(WebDriver driver, String baseUrl) {
driver.get(baseUrl + urlPath);
driver.get(baseUrl + slashUrlPath);
return new LoginPage(driver);
}

public boolean hasLastLoginTime() {
WebElement lastLoginTime = driver.findElement(By.id("last_login_time"));
String loginTime = lastLoginTime.getText();
return loginTime != null && ! loginTime.isBlank();
return loginTime != null && !loginTime.isBlank();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import static org.hamcrest.Matchers.matchesPattern;

/**
* The LoginPage class represents the login page on the UAA server.
* It has url matching: `/login`.
*/
public class LoginPage extends Page {

static final private String urlPath = "/login";
Expand All @@ -22,22 +26,29 @@ static public LoginPage go(WebDriver driver, String baseUrl) {
return new LoginPage(driver);
}

// When there is a SAML integration, there is a link to go to a SAML login page instead. This assumes there is
// only one SAML link.
/**
* When there is a SAML integration, there is a link to go to a SAML login page.
* Clicking the link will go to the SAML login page.
*/
public SamlLoginPage clickSamlLink_goesToSamlLoginPage(String matchText) {
clickSamlLoginLinkWithText(matchText);
return new SamlLoginPage(driver);
}

// If the SAML IDP has no logout URL in the metadata, logging out of UAA will leave
// the IDP still logged in, and when going back to the SAML login page, it will log
// the app back in automatically and immediately redirect to the post-login page.
/**
* If the SAML IDP has no logout URL in the metadata, logging out of UAA will leave
* the IDP still logged in.
* When going back to the SAML login page, it will log
* the app back in automatically and immediately redirect to the post-login page.
*/
public HomePage clickSamlLink_goesToHomePage(String matchText) {
clickSamlLoginLinkWithText(matchText);
return new HomePage(driver);
}

// Click the first link that contains the given text
/**
* Click the first link that contains the given text
*/
private void clickSamlLoginLinkWithText(String matchText) {
final AtomicReference<WebElement> matchingElement = new AtomicReference<>();
driver.findElements(By.className("saml-login-link")).forEach(webElement -> {
Expand All @@ -50,4 +61,4 @@ private void clickSamlLoginLinkWithText(String matchText) {
}
matchingElement.get().click();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package org.cloudfoundry.identity.uaa.integration.pageObjects;

import java.time.Duration;

import org.assertj.core.api.HamcrestCondition;
import org.hamcrest.Matcher;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import static org.junit.Assert.assertThat;
import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;

/**
* The Page class is the base class, representing a web page.
* It provides methods for validating the URL, page source, and title,
* as well as performing common page actions like logging out and clearing cookies.
*/
public class Page {
protected WebDriver driver;

Expand All @@ -16,24 +22,27 @@ public Page(WebDriver driver) {
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}

protected static void validateUrl(WebDriver driver, Matcher urlMatcher) {
assertThat("URL validation failed", driver.getCurrentUrl(), urlMatcher);
protected static void validateUrl(WebDriver driver, Matcher<String> urlMatcher) {
HamcrestCondition<String> condition = new HamcrestCondition<>(urlMatcher);
assertThat(driver.getCurrentUrl()).as("URL validation failed").is(condition);
}

public void validateUrl(Matcher urlMatcher) {
validateUrl(driver, urlMatcher);
protected static void validatePageSource(WebDriver driver, Matcher<String> matcher) {
HamcrestCondition<String> condition = new HamcrestCondition<>(matcher);
assertThat(driver.getPageSource()).is(condition);
}

protected static void validatePageSource(WebDriver driver, Matcher matcher) {
assertThat(driver.getPageSource(), matcher);
public void validateUrl(Matcher<String> urlMatcher) {
validateUrl(driver, urlMatcher);
}

public void validatePageSource(Matcher matcher) {
public void validatePageSource(Matcher<String> matcher) {
validatePageSource(driver, matcher);
}

public void validateTitle(Matcher matcher) {
assertThat(driver.getTitle(), matcher);
public void validateTitle(Matcher<String> matcher) {
HamcrestCondition<String> condition = new HamcrestCondition<>(matcher);
assertThat(driver.getTitle()).is(condition);
}

public LoginPage logout_goesToLoginPage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.junit.Assert.assertThat;

/**
* The PasscodePage class represents the passcode page on the UAA server.
* Which displays the temporary authentication code.
* It has url matching: `/passcode`.
*/
public class PasscodePage extends Page {
static final private String urlPath = "/passcode";

public PasscodePage(WebDriver driver) {
super(driver);
validateUrl(driver, endsWith(urlPath));
validatePageSource(driver, containsString("Temporary Authentication Code") );
validatePageSource(driver, containsString("Temporary Authentication Code"));
}

static public LoginPage requestPasscode_goesToLoginPage(WebDriver driver, String baseUrl) {
driver.get(baseUrl + urlPath);
return new LoginPage(driver);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import org.openqa.selenium.WebDriver;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;

/**
* The SamlErrorPage class represents the saml error page on the UAA server.
* It has url matching: `/saml_error`.
*/
public class SamlErrorPage extends Page {
static final private String urlPath = "/saml_error";

Expand All @@ -13,4 +16,3 @@ public SamlErrorPage(WebDriver driver) {
validateUrl(driver, endsWith(urlPath));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import static org.hamcrest.Matchers.containsString;

/**
* The SamlLoginPage class represents the login page on the SimpleSAML server.
* This class provides methods to interact with the SAML login page and perform login actions.
* It has url matching: `/module.php/core/loginuserpass`.
*/
public class SamlLoginPage extends Page {
// This is on the saml server, not the UAA server
static final private String urlPath = "/module.php/core/loginuserpass";
Expand All @@ -26,7 +31,7 @@ public PasscodePage login_goesToPasscodePage(String username, String password) {
return new PasscodePage(driver);
}

public CustomErrorPage login_goesToCustomErrorPage(String username, String password, Matcher urlMatcher) {
public CustomErrorPage login_goesToCustomErrorPage(String username, String password, Matcher<String> urlMatcher) {
sendLoginCredentials(username, password);
return new CustomErrorPage(driver, urlMatcher);
}
Expand All @@ -43,4 +48,4 @@ private void sendLoginCredentials(String username, String password) {
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("submit_button")).click();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package org.cloudfoundry.identity.uaa.integration.pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;

/**
* The SamlWelcomePage class represents the welcome page on the SimpleSAML server.
* It has url matching: `/module.php/core/welcome`.
*/
public class SamlWelcomePage extends Page {
static final private String urlPath = "module.php/core/welcome";

public SamlWelcomePage(WebDriver driver) {
super(driver);
validateUrl(driver, endsWith(urlPath));
}

}

0 comments on commit 0d3a595

Please sign in to comment.