Merge pull request #4502 from mhajas/KEYCLOAK-5555

KEYCLOAK-5555 Use driver from function argument for URL Asserts
This commit is contained in:
Pavel Drozd 2017-10-04 08:00:45 +02:00 committed by GitHub
commit 98c63bc195
8 changed files with 97 additions and 34 deletions

View file

@ -20,11 +20,33 @@ package org.keycloak.testsuite.util;
import org.jboss.arquillian.graphene.context.GrapheneContext;
import org.openqa.selenium.WebDriver;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* @author Vaclav Muzikar <vmuzikar@redhat.com>
*/
public final class DroneUtils {
private static Queue<WebDriver> driverQueue = new LinkedList<>();
public static WebDriver getCurrentDriver() {
return GrapheneContext.lastContext().getWebDriver();
if (driverQueue.isEmpty()) {
return GrapheneContext.lastContext().getWebDriver();
}
return driverQueue.peek();
}
public static void addWebDriver(WebDriver driver) {
driverQueue.add(driver);
}
public static void removeWebDriver() {
driverQueue.poll();
}
public static void resetQueue() {
driverQueue = new LinkedList<>();
}
}

View file

@ -64,6 +64,7 @@ import org.keycloak.testsuite.auth.page.login.OIDCLogin;
import org.keycloak.testsuite.auth.page.login.UpdatePassword;
import org.keycloak.testsuite.client.KeycloakTestingClient;
import org.keycloak.testsuite.util.AdminClientUtil;
import org.keycloak.testsuite.util.DroneUtils;
import org.keycloak.testsuite.util.OAuthClient;
import org.keycloak.testsuite.util.TestCleanup;
import org.keycloak.testsuite.util.TestEventsLogger;
@ -213,6 +214,9 @@ public abstract class AbstractKeycloakTest {
}
testContext.getCleanups().clear();
}
// Remove all browsers from queue
DroneUtils.resetQueue();
}
protected TestCleanup getCleanup(String realmName) {

View file

@ -78,7 +78,7 @@ public abstract class AbstractFuseAdminAdapterTest extends AbstractExampleAdapte
assertCurrentUrlDoesntStartWith(hawtioPage);
testRealmLoginPage.form().login("root", "password");
assertCurrentUrlStartsWith(hawtioPage.getDriver(), hawtioPage.toString() + "/welcome");
assertCurrentUrlStartsWith(hawtioPage.toString() + "/welcome", hawtioPage.getDriver());
hawtioPage.logout();
assertCurrentUrlStartsWith(testRealmLoginPage);

View file

@ -41,7 +41,7 @@ public abstract class AbstractHawtioAdapterTest extends AbstractExampleAdapterTe
testRealmLoginPage.form().login("root", "password");
waitUntilElement(By.xpath("//body")).is().present();
assertCurrentUrlStartsWith(hawtioPage.getDriver(), hawtioPage.toString() + "/welcome");
assertCurrentUrlStartsWith(hawtioPage.toString() + "/welcome", hawtioPage.getDriver());
hawtioPage.logout();
pause(1000);

View file

@ -194,7 +194,7 @@ public abstract class AbstractDemoServletsAdapterTest extends AbstractServletsAd
assertCurrentUrlStartsWithLoginUrlOf(testRealmPage);
testRealmLoginPage.form().login("bburke@redhat.com", "password");
assertCurrentUrlEquals(driver, inputPortal + "/secured/post");
assertCurrentUrlEquals(inputPortal + "/secured/post");
waitForPageToLoad();
String pageSource = driver.getPageSource();
assertThat(pageSource, containsString("parameter=hello"));

View file

@ -127,7 +127,7 @@ public abstract class AbstractOIDCPublicKeyRotationAdapterTest extends AbstractS
testRealmLoginPage.form().waitForUsernameInputPresent();
assertCurrentUrlStartsWithLoginUrlOf(testRealmPage);
testRealmLoginPage.form().login("bburke@redhat.com", "password");
URLAssert.assertCurrentUrlStartsWith(driver, tokenMinTTLPage.getInjectedUrl().toString());
URLAssert.assertCurrentUrlStartsWith(tokenMinTTLPage.getInjectedUrl().toString());
Assert.assertNull(tokenMinTTLPage.getAccessToken());
driver.navigate().to(logoutUri);

View file

@ -37,9 +37,8 @@ import org.keycloak.testsuite.util.SecondBrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import static org.keycloak.testsuite.auth.page.AuthRealm.DEMO;
import static org.keycloak.testsuite.util.URLAssert.assertCurrentUrlEquals;
import static org.keycloak.testsuite.util.URLAssert.assertCurrentUrlStartsWithLoginUrlOf;
@ -84,13 +83,13 @@ public abstract class AbstractSessionServletAdapterTest extends AbstractServlets
// cannot pass to loginAndCheckSession becayse loginPage is not working together with driver2, therefore copypasta
driver2.navigate().to(sessionPortalPage.toString());
assertCurrentUrlStartsWithLoginUrlOf(driver2, testRealmPage);
assertCurrentUrlStartsWithLoginUrlOf(testRealmPage, driver2);
driver2.findElement(By.id("username")).sendKeys("bburke@redhat.com");
driver2.findElement(By.id("password")).sendKeys("password");
driver2.findElement(By.id("password")).submit();
assertCurrentUrlEquals(driver2, sessionPortalPage);
assertCurrentUrlEquals(sessionPortalPage, driver2);
String pageSource = driver2.getPageSource();
assertTrue(pageSource.contains("Counter=1"));
assertThat(pageSource, containsString("Counter=1"));
// Counter increased now
driver2.navigate().to(sessionPortalPage.toString());
pageSource = driver2.getPageSource();
@ -108,12 +107,12 @@ public abstract class AbstractSessionServletAdapterTest extends AbstractServlets
// Assert that I am still logged in browser2 and same session is still preserved
driver2.navigate().to(sessionPortalPage.toString());
assertCurrentUrlEquals(driver2, sessionPortalPage);
assertCurrentUrlEquals(sessionPortalPage, driver2);
pageSource = driver2.getPageSource();
assertTrue(pageSource.contains("Counter=3"));
assertThat(pageSource, containsString("Counter=3"));
driver2.navigate().to(logoutUri);
assertCurrentUrlStartsWithLoginUrlOf(driver2, testRealmPage);
assertCurrentUrlStartsWithLoginUrlOf(testRealmPage, driver2);
}

View file

@ -46,47 +46,85 @@ import static org.keycloak.testsuite.util.URLUtils.currentUrlStartWith;
*/
public class URLAssert {
public static void assertCurrentUrlEquals(AbstractPage page) {
assertCurrentUrlEquals(page.getDriver(), page);
public static void assertCurrentUrlEquals(final AbstractPage page, WebDriver driver) {
assertCurrentUrlEquals(page.toString(), driver);
}
public static void assertCurrentUrlEquals(WebDriver driver, final AbstractPage page) {
String expected = page.toString();
assertTrue("Expected URL: " + expected + "; actual: " + driver.getCurrentUrl(),
currentUrlEqual(page.toString()));
public static void assertCurrentUrlEquals(final String url, WebDriver driver) {
DroneUtils.addWebDriver(driver);
assertCurrentUrlEquals(url);
DroneUtils.removeWebDriver();
}
public static void assertCurrentUrlEquals(WebDriver driver, final String url) {
assertTrue("Expected URL: " + url + "; actual: " + driver.getCurrentUrl(),
public static void assertCurrentUrlEquals(final AbstractPage page) {
assertCurrentUrlEquals(page.toString());
}
public static void assertCurrentUrlEquals(final String url) {
assertTrue("Expected URL: " + url + "; actual: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlEqual(url));
}
public static void assertCurrentUrlStartsWith(AbstractPage page) {
assertCurrentUrlStartsWith(page.getDriver(), page.toString());
public static void assertCurrentUrlStartsWith(final AbstractPage page, WebDriver driver) {
assertCurrentUrlStartsWith(page.toString(), driver);
}
public static void assertCurrentUrlStartsWith(WebDriver driver, final String url) {
assertTrue("URL expected to begin with:" + url + "; actual URL: " + driver.getCurrentUrl(),
currentUrlStartWith(url));
public static void assertCurrentUrlStartsWith(final String url, WebDriver driver) {
DroneUtils.addWebDriver(driver);
assertCurrentUrlStartsWith(url);
DroneUtils.removeWebDriver();
}
public static void assertCurrentUrlStartsWith(final AbstractPage page) {
assertCurrentUrlStartsWith(page.toString());
}
public static void assertCurrentUrlStartsWith(final String url){
assertTrue("URL expected to begin with:" + url + "; actual URL: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlStartWith(url));
}
public static void assertCurrentUrlDoesntStartWith(final AbstractPage page, WebDriver driver) {
assertCurrentUrlDoesntStartWith(page.toString(), driver);
}
public static void assertCurrentUrlDoesntStartWith(final String url, WebDriver driver) {
DroneUtils.addWebDriver(driver);
assertCurrentUrlDoesntStartWith(url);
DroneUtils.removeWebDriver();
}
public static void assertCurrentUrlDoesntStartWith(AbstractPage page) {
assertCurrentUrlDoesntStartWith(page.getDriver(), page.toString());
assertCurrentUrlDoesntStartWith(page.toString());
}
public static void assertCurrentUrlDoesntStartWith(WebDriver driver, final String url) {
assertTrue("URL expected NOT to begin with:" + url + "; actual URL: " + driver.getCurrentUrl(),
public static void assertCurrentUrlDoesntStartWith(final String url) {
assertTrue("URL expected NOT to begin with:" + url + "; actual URL: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlDoesntStartWith(url));
}
public static void assertCurrentUrlStartsWithLoginUrlOf(PageWithLoginUrl page) {
assertCurrentUrlStartsWithLoginUrlOf(page.getDriver(), page);
public static void assertCurrentUrlStartsWithLoginUrlOf(final PageWithLoginUrl page, WebDriver driver) {
assertCurrentUrlStartsWithLoginUrlOf(page.getOIDCLoginUrl().toString(), driver);
}
public static void assertCurrentUrlStartsWithLoginUrlOf(WebDriver driver, PageWithLoginUrl page) {
assertCurrentUrlStartsWith(driver, page.getOIDCLoginUrl().toString());
public static void assertCurrentUrlStartsWithLoginUrlOf(final String url, WebDriver driver) {
DroneUtils.addWebDriver(driver);
assertCurrentUrlStartsWithLoginUrlOf(url);
DroneUtils.removeWebDriver();
}
public static void assertCurrentUrlStartsWithLoginUrlOf(final PageWithLoginUrl page) {
assertCurrentUrlStartsWithLoginUrlOf(page.getOIDCLoginUrl().toString());
}
public static void assertCurrentUrlStartsWithLoginUrlOf(final String url) {
assertCurrentUrlStartsWith(url);
}
public static void assertGetURL(URI url, String accessToken, AssertResponseHandler handler) {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {