Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selenium: Create selenium tests to cover the authorization on GitHub #8395

Merged
merged 4 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.eclipse.che.commons.lang.IoUtil;
import org.eclipse.che.commons.lang.Pair;
import org.eclipse.che.selenium.core.client.KeycloakToken.TokenDetails;
import org.eclipse.che.selenium.core.provider.TestApiEndpointUrlProvider;
import org.eclipse.che.selenium.core.provider.TestOfflineToAccessTokenExchangeApiEndpointUrlProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -58,7 +57,6 @@ public class KeycloakTestAuthServiceClient implements TestAuthServiceClient {

private static final long MIN_TOKEN_LIFETIME_SEC = 30;

private final String apiEndpoint;
private final DefaultHttpJsonRequestFactory requestFactory;
private final TestOfflineToAccessTokenExchangeApiEndpointUrlProvider
testOfflineToAccessTokenExchangeApiEndpointUrlProvider;
Expand All @@ -70,15 +68,14 @@ public class KeycloakTestAuthServiceClient implements TestAuthServiceClient {

@Inject
public KeycloakTestAuthServiceClient(
TestApiEndpointUrlProvider cheApiEndpointProvider,
DefaultHttpJsonRequestFactory requestFactory,
TestOfflineToAccessTokenExchangeApiEndpointUrlProvider
testOfflineToAccessTokenExchangeApiEndpointUrlProvider) {
this.apiEndpoint = cheApiEndpointProvider.get().toString();
testOfflineToAccessTokenExchangeApiEndpointUrlProvider,
TestKeycloakSettingsServiceClient testKeycloakSettingsServiceClient) {
this.requestFactory = requestFactory;
this.gson = new Gson();
this.tokens = new ConcurrentHashMap<>();
this.keycloakSettings = getKeycloakConfiguration();
this.keycloakSettings = testKeycloakSettingsServiceClient.read();
this.testOfflineToAccessTokenExchangeApiEndpointUrlProvider =
testOfflineToAccessTokenExchangeApiEndpointUrlProvider;
}
Expand Down Expand Up @@ -236,18 +233,4 @@ private KeycloakToken requestToken(String grandType, List<Pair<String, ?>> param
}
return token;
}

private KeycloakSettings getKeycloakConfiguration() {
try {
return gson.fromJson(
requestFactory
.fromUrl(apiEndpoint + "keycloak/settings/")
.useGetMethod()
.request()
.asString(),
KeycloakSettings.class);
} catch (ApiException | IOException | JsonSyntaxException ex) {
throw new RuntimeException("Error during retrieving Che Keycloak configuration: ", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.selenium.core.client;

import static java.lang.String.format;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.inject.Inject;
import java.io.IOException;
import org.eclipse.che.api.core.ApiException;
import org.eclipse.che.api.core.rest.DefaultHttpJsonRequestFactory;
import org.eclipse.che.selenium.core.provider.TestApiEndpointUrlProvider;

/** @author Dmytro Nochevnov */
public class TestKeycloakSettingsServiceClient {

private final String keycloakSettingsServiceUrl;
private final DefaultHttpJsonRequestFactory requestFactory;
private final Gson gson;

@Inject
public TestKeycloakSettingsServiceClient(
TestApiEndpointUrlProvider cheApiEndpointProvider,
DefaultHttpJsonRequestFactory requestFactory,
Gson gson) {
this.keycloakSettingsServiceUrl = format("%skeycloak/settings/", cheApiEndpointProvider.get());
this.requestFactory = requestFactory;
this.gson = gson;
}

public KeycloakSettings read() {
try {
return gson.fromJson(
requestFactory.fromUrl(keycloakSettingsServiceUrl).useGetMethod().request().asString(),
KeycloakSettings.class);
} catch (ApiException | IOException | JsonSyntaxException ex) {
throw new RuntimeException("Error during retrieving Che Keycloak configuration: ", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.selenium.pageobject;

import static org.eclipse.che.selenium.core.constant.TestTimeoutsConstants.LOAD_PAGE_TIMEOUT_SEC;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.che.selenium.core.SeleniumWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

/** @author Igor Ohrimenko */
@Singleton
public class SeleniumWebDriverHelper {
protected final SeleniumWebDriver seleniumWebDriver;
protected final WebDriverWait loadPageWait;

@Inject
protected SeleniumWebDriverHelper(SeleniumWebDriver seleniumWebDriver) {
this.seleniumWebDriver = seleniumWebDriver;
this.loadPageWait = new WebDriverWait(seleniumWebDriver, LOAD_PAGE_TIMEOUT_SEC);
}

public void setFieldValue(By fieldLocator, String value) {
waitAndGetElement(fieldLocator).clear();
waitFieldValue(fieldLocator, "");
waitAndGetElement(fieldLocator).sendKeys(value);
waitFieldValue(fieldLocator, value);
}

public void waitElementIsVisible(By elementLocator) {
loadPageWait.until(visibilityOfElementLocated(elementLocator));
}

public String getFieldValue(By fieldLocator) {
return waitAndGetElement(fieldLocator).getAttribute("value");
}

public WebElement waitAndGetElement(By locator) {
return loadPageWait.until(visibilityOfElementLocated(locator));
}

public void waitFieldValue(By fieldLocator, String expectedValue) {
loadPageWait.until(
(ExpectedCondition<Boolean>) driver -> getFieldValue(fieldLocator).equals(expectedValue));
}

public void waitAndClickOnElement(By elementLocator) {
loadPageWait.until(visibilityOfElementLocated(elementLocator)).click();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import org.eclipse.che.selenium.core.SeleniumWebDriver;
import org.eclipse.che.selenium.core.client.TestKeycloakSettingsServiceClient;
import org.eclipse.che.selenium.core.entrance.Entrance;
import org.eclipse.che.selenium.core.provider.TestDashboardUrlProvider;
import org.eclipse.che.selenium.core.provider.TestIdeUrlProvider;
import org.eclipse.che.selenium.core.user.TestUser;
import org.eclipse.che.selenium.pageobject.TestWebElementRenderChecker;
import org.eclipse.che.selenium.pageobject.site.LoginPage;
Expand All @@ -43,19 +44,21 @@ public class CheMultiuserAdminDashboard extends Dashboard {
public CheMultiuserAdminDashboard(
SeleniumWebDriver seleniumWebDriver,
TestUser defaultUser,
TestIdeUrlProvider testIdeUrlProvider,
TestDashboardUrlProvider testDashboardUrlProvider,
Entrance entrance,
LoginPage loginPage,
TestWebElementRenderChecker testWebElementRenderChecker) {
TestWebElementRenderChecker testWebElementRenderChecker,
TestKeycloakSettingsServiceClient testKeycloakSettingsServiceClient,
@Named("che.multiuser") boolean isMultiuser) {
super(
seleniumWebDriver,
defaultUser,
testIdeUrlProvider,
testDashboardUrlProvider,
entrance,
loginPage,
testWebElementRenderChecker);
testWebElementRenderChecker,
testKeycloakSettingsServiceClient,
isMultiuser);
PageFactory.initElements(seleniumWebDriver, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.Arrays;
import com.google.inject.name.Named;
import java.util.List;
import javax.annotation.PreDestroy;
import org.eclipse.che.selenium.core.SeleniumWebDriver;
import org.eclipse.che.selenium.core.client.TestKeycloakSettingsServiceClient;
import org.eclipse.che.selenium.core.entrance.Entrance;
import org.eclipse.che.selenium.core.provider.TestDashboardUrlProvider;
import org.eclipse.che.selenium.core.provider.TestIdeUrlProvider;
import org.eclipse.che.selenium.core.user.TestUser;
import org.eclipse.che.selenium.core.utils.WaitUtils;
import org.eclipse.che.selenium.pageobject.TestWebElementRenderChecker;
Expand All @@ -46,28 +46,31 @@ public class Dashboard {
protected final SeleniumWebDriver seleniumWebDriver;
protected final TestUser defaultUser;

private final TestIdeUrlProvider testIdeUrlProvider;
private final TestDashboardUrlProvider testDashboardUrlProvider;
private final Entrance entrance;
private final LoginPage loginPage;
private final TestWebElementRenderChecker testWebElementRenderChecker;
private final TestKeycloakSettingsServiceClient testKeycloakSettingsServiceClient;
private final boolean isMultiuser;

@Inject
public Dashboard(
SeleniumWebDriver seleniumWebDriver,
TestUser defaultUser,
TestIdeUrlProvider testIdeUrlProvider,
TestDashboardUrlProvider testDashboardUrlProvider,
Entrance entrance,
LoginPage loginPage,
TestWebElementRenderChecker testWebElementRenderChecker) {
TestWebElementRenderChecker testWebElementRenderChecker,
TestKeycloakSettingsServiceClient testKeycloakSettingsServiceClient,
@Named("che.multiuser") boolean isMultiuser) {
this.seleniumWebDriver = seleniumWebDriver;
this.defaultUser = defaultUser;
this.testIdeUrlProvider = testIdeUrlProvider;
this.testDashboardUrlProvider = testDashboardUrlProvider;
this.entrance = entrance;
this.loginPage = loginPage;
this.testWebElementRenderChecker = testWebElementRenderChecker;
this.testKeycloakSettingsServiceClient = testKeycloakSettingsServiceClient;
this.isMultiuser = isMultiuser;
PageFactory.initElements(seleniumWebDriver, this);
}

Expand All @@ -80,6 +83,7 @@ public enum MenuItem {
ORGANIZATIONS("Organizations"),
SETTINGS("Settings"),
CREATE_TEAM("Create Team");

private final String title;

MenuItem(String title) {
Expand Down Expand Up @@ -244,13 +248,17 @@ public void open(String userName, String userPassword) {
}

public void logout() {
String apiEndpoint = testDashboardUrlProvider.get().toString();
List<String> api = Arrays.asList(apiEndpoint.split(":"));
String logoutApiEndpoint = api.get(0) + ":" + api.get(1);
String logoutURL = logoutApiEndpoint + ":5050/auth/realms/che/protocol/openid-connect/logout";
String redirectURL = logoutApiEndpoint + ":8080/dashboard/#/workspaces";
if (!isMultiuser) {
return;
}

String logoutUrl =
format(
"%s?redirect_uri=%s#/workspaces",
testKeycloakSettingsServiceClient.read().getKeycloakLogoutEndpoint(),
testDashboardUrlProvider.get());

seleniumWebDriver.navigate().to(logoutURL + "?redirect_uri=" + redirectURL);
seleniumWebDriver.navigate().to(logoutUrl);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.che.selenium.core.action.ActionsFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
Expand Down Expand Up @@ -267,14 +266,19 @@ public void clickOnConnectGithubAccountButton() {

public boolean isGithubProjectsListDisplayed() {
try {
return new WebDriverWait(seleniumWebDriver, LOAD_PAGE_TIMEOUT_SEC)
.until(visibilityOfElementLocated(By.xpath(Locators.GITHUB_PROJECTS_LIST)))
.isDisplayed();
waitGithubProjectList();
return true;
} catch (TimeoutException ex) {
return false;
}
}

public void waitGithubProjectList() {
new WebDriverWait(seleniumWebDriver, LOAD_PAGE_TIMEOUT_SEC)
.until(visibilityOfElementLocated(By.xpath(Locators.GITHUB_PROJECTS_LIST)))
.isDisplayed();
}

public void selectProjectFromList(String projectName) {
WebElement project =
seleniumWebDriver.findElement(
Expand All @@ -287,14 +291,9 @@ public void selectProjectFromList(String projectName) {
public void waitAuthorizationPageOpened() {
new WebDriverWait(seleniumWebDriver, ELEMENT_TIMEOUT_SEC)
.until(
new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver elem) {
return loginField.isDisplayed()
&& passField.isDisplayed()
&& signInBtn.isDisplayed();
}
});
(ExpectedCondition<Boolean>)
elem ->
loginField.isDisplayed() && passField.isDisplayed() && signInBtn.isDisplayed());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.che.selenium.pageobject.dashboard.account;

/** @author Igor Ohrimenko */
public class Account {
private String login;
private String email;
Expand Down
Loading