8000 migration test RequiredActionUpdateProfileTest by gaoyikeshuer · Pull Request #48648 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
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
@@ -0,0 +1,334 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.keycloak.testframework.ui.page;

import java.util.LinkedHashMap;
import java.util.Map;

import org.keycloak.testframework.ui.webdriver.ManagedWebDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class LoginUpdateProfilePage extends AbstractLoginPage {

@FindBy(id = "username")
private WebElement usernameInput;

@FindBy(name = "firstName")
private WebElement firstNameInput;

@FindBy(name = "lastName")
private WebElement lastNameInput;

@FindBy(name = "email")
private WebElement emailInput;

@FindBy(name = "department")
private WebElement departmentInput;

@FindBy(css = "input[type=\"submit\"]")
private WebElement submitButton;

@FindBy(name = "cancel-aia")
private WebElement cancelAIAButton;

@FindBy(css = "div[class^='pf-v5-c-alert'], div[class^='alert-error']")
private WebElement loginAlertErrorMessage;

private final UpdateProfileErrors errorsPage;

public LoginUpdateProfilePage(ManagedWebDriver driver) {
super(driver);
this.errorsPage = new UpdateProfileErrors(driver);
}

public void update(String firstName, String lastName) {
prepareUpdate().firstName(firstName).lastName(lastName).submit();
}

public void update(String firstName, String lastName, String email) {
prepareUpdate().firstName(firstName).lastName(lastName).email(email).submit();
}

public void update(Map<String, String> attributes) {
prepareUpdate().otherProfileAttribute(attributes).submit();
}

public Update prepareUpdate() {
return new Update(this);
}

public void cancel() {
cancelAIAButton.click();
}

public String getAlertError() {
try {
return loginAlertErrorMessage.getText();
} catch (NoSuchElementException e) {
return null;
}
}

public String getUsername() {
return readValue(By.id("username"));
}

public String getFirstName() {
return readValue(By.name("firstName"));
}

public String getLastName() {
return readValue(By.name("lastName"));
}

public String getEmail() {
return readValue(By.name("email"));
}

public String getDepartment() {
return readValue(By.name("department"));
}

private String readValue(By locator) {
return driver.waiting().until(d -> {
try {
return d.findElement(locator).getAttribute("value");
} catch (StaleElementReferenceException e) {
return null;
}
});
}

public boolean isDepartmentEnabled() {
return departmentInput.isEnabled();
}

public UpdateProfileErrors getInputErrors() {
return errorsPage;
}

public String getLabelForField(String fieldId) {
return driver.findElement(By.cssSelector("label[for=" + fieldId + "]")).getText().replaceAll("\\s\\*$", "");
}

public WebElement getElementById(String fieldId) {
try {
return driver.findElement(By.id(fieldId));
} catch (NoSuchElementException ignore) {
return null;
}
}

public boolean isUsernamePresent() {
try {
return usernameInput.isDisplayed();
} catch (NoSuchElementException nse) {
return false;
}
}

public boolean isEmailInputPresent() {
try {
return emailInput.isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}

public boolean isDepartmentPresent() {
try {
return departmentInput.isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}

public boolean isCancelDisplayed() {
try {
return cancelAIAButton.isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}

public void setAttribute(String elementId, String value) {
WebElement element = getElementById(elementId);

if (element != null) {
element.clear();
element.sendKeys(value);
}
}

public void clickAddAttributeValue(String elementId) {
WebElement element = getElementById("kc-add-" + elementId);

if (element != null) {
element.click();
}
}

public void clickRemoveAttributeValue(String elementId) {
WebElement element = getElementById("kc-remove-" + elementId);

if (element != null) {
element.click();
}
}

public String getAttribute(String elementId) {
WebElement element = getElementById(elementId);

if (element != null) {
return element.getAttribute("value");
}

return null;
}

@Override
public String getExpectedPageId() {
return "login-login-update-profile";
}

public static class Update {
private final LoginUpdateProfilePage page;
private String username;
private String firstName;
private String lastName;
private String department;
private String email;
private final Map<String, String> other = new LinkedHashMap<>();

protected Update(LoginUpdateProfilePage page) {
this.page = page;
}

public Update username(String username) {
this.username = username;
return this;
}

public Update firstName(String firstName) {
this.firstName = firstName;
return this;
}

public Update lastName(String lastName) {
this.lastName = lastName;
return this;
}

public Update department(String department) {
this.department = department;
return this;
}

public Update email(String email) {
this.email = email;
return this;
}

public Update otherProfileAttribute(Map<String, String> attributes) {
other.putAll(attributes);
return this;
}

public void submit() {
if (username != null) {
page.usernameInput.clear();
page.usernameInput.sendKeys(username);
}
if (firstName != null) {
page.firstNameInput.clear();
page.firstNameInput.sendKeys(firstName);
}
if (lastName != null) {
page.lastNameInput.clear();
page.lastNameInput.sendKeys(lastName);
DB8E }

if (department != null) {
page.departmentInput.clear();
page.departmentInput.sendKeys(department);
}

if (email != null) {
page.emailInput.clear();
page.emailInput.sendKeys(email);
}

for (Map.Entry<String, String> entry : other.entrySet()) {
WebElement el = page.driver.findElement(By.id(entry.getKey()));
if (el != null) {
el.clear();
el.sendKeys(entry.getValue());
}
}

page.submitButton.submit();
}
}

// For managing input errors
public static class UpdateProfileErrors {

private final ManagedWebDriver driver;

public UpdateProfileErrors(ManagedWebDriver driver) {
this.driver = driver;
}

private String getTextById(String id) {
try {
return driver.findElement(By.id(id)).getText();
} catch (NoSuchElementException e) {
return null;
}
}

public String getFirstNameError() {
String text = getTextById("input-error-firstname");
if (text != null) {
return text;
}
return getTextById("input-error-firstName");
}

public String getLastNameError() {
String text = getTextById("input-error-lastname");
if (text != null) {
return text;
}
return getTextById("input-error-lastName");
}

public String getEmailError() {
return getTextById("input-error-email");
}

public String getUsernameError() {
return getTextById("input-error-username");
}
}
}
Loading
Loading
0