Skip to content

Commit

Permalink
- refactoring: Snackbar element
Browse files Browse the repository at this point in the history
- Update test site: Snackbar element
  • Loading branch information
Aleksandr_Vorobev committed Jun 13, 2024
1 parent 3f4039b commit dfd98aa
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
package io.github.com.pages;

import com.epam.jdi.light.angular.elements.complex.Snackbar;
import com.epam.jdi.light.elements.complex.dropdown.Dropdown;
import com.epam.jdi.light.elements.pageobjects.annotations.locators.JDropdown;
import com.epam.jdi.light.elements.pageobjects.annotations.locators.UI;
import com.epam.jdi.light.ui.html.elements.common.Button;
import com.epam.jdi.light.ui.html.elements.common.TextField;

public class SnackBarPage extends AngularPage {
@UI("#snack-bar-custom-component-input")
public static TextField durationInput;

@UI("#snack-bar-custom-component-button")
public static Button showCustomSnackbarButton;

@UI("#snack-bar-custom-component")
public static Snackbar customSnackbar;

@UI("#snack-bar-message-input")
public TextField messageInput;
public static TextField basicSnackbarMessageInput;

@UI("#snack-bar-action-input")
public TextField actionInput;
public static TextField basicSnackbarActionInput;

@UI("#snack-bar-open-button")
public Button openButton;

public static Button showBasicSnackbarButton;
@UI("//simple-snack-bar")
public Snackbar basicSnackbar;
public static Snackbar snackbar;

@UI("#snack-bar-custom-component-input")
public TextField durationInput;
@JDropdown(root = "mat-form-field.mat-mdc-form-field-type-mat-select:has(#snack-bar-position-horizontal-select)", value = "span.mat-mdc-select-min-line", list = "//ancestor::body//mat-option")
public static Dropdown horizontalPositionDropdown;

@UI("#snack-bar-custom-component-button")
public Button customSnackbarOpenButton;
@JDropdown(root = "mat-form-field.mat-mdc-form-field-type-mat-select:has(#snack-bar-position-vertival-select)", value = "span.mat-mdc-select-min-line", list = "//ancestor::body//mat-option")
public static Dropdown verticalPositionDropdown;

@UI("#snack-bar-custom-component")
public Snackbar customSnackbar;
@UI("#snack-bar-position-open-button")
public static Button showPositionSnackbarButton;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.epam.jdi.light.angular.asserts;

import com.epam.jdi.light.angular.elements.enums.Position;
import com.epam.jdi.light.angular.elements.interfaces.HasPosition;
import com.epam.jdi.light.asserts.generic.UIAssert;
import com.epam.jdi.light.common.JDIAction;
import org.hamcrest.Matchers;

import static com.epam.jdi.light.asserts.core.SoftAssert.jdiAssert;

public abstract class PositionAssert<A extends UIAssert<?, ?>, E extends HasPosition> extends UIAssert<A, E> {

@JDIAction(value = "Assert that '{name}' has position '{0}'", isAssert = true)
public A position(String position) {
jdiAssert(element().position(), Matchers.is(Position.fromFullString(position)));
return (A) this;
}

@JDIAction(value = "Assert that '{name}' has position '{0}'", isAssert = true)
public A position(Position position) {
jdiAssert(element().position(), Matchers.is(position));
return (A) this;
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
package com.epam.jdi.light.angular.asserts;

import com.epam.jdi.light.angular.elements.complex.Snackbar;
import com.epam.jdi.light.asserts.generic.UIAssert;
import com.epam.jdi.light.common.JDIAction;
import com.jdiai.tools.Timer;
import com.jdiai.tools.func.JAction;
import org.hamcrest.Matchers;

import static com.epam.jdi.light.asserts.core.SoftAssert.jdiAssert;

public class SnackbarAssert extends UIAssert<SnackbarAssert, Snackbar> {
public class SnackbarAssert extends PositionAssert<SnackbarAssert, Snackbar> {

@JDIAction(value = "Assert that '{name}' has message '{0}'", isAssert = true)
public SnackbarAssert message(String expected) {
jdiAssert(element().messageText(), Matchers.containsString(expected));
jdiAssert(element().message()
.getText(), Matchers.containsString(expected));
return this;
}

@JDIAction(value = "Assert that '{name}' has action icon", isAssert = true)
@JDIAction(value = "Assert that '{name}' has action '{0}'", isAssert = true)
public SnackbarAssert action(String expected) {
jdiAssert(element().action()
.getText(), Matchers.containsString(expected));
return this;
}

@JDIAction(value = "Assert that '{name}' has action button", isAssert = true)
public SnackbarAssert hasAction() {
jdiAssert(element().actionIcon()
jdiAssert(element().action()
.isExist(), Matchers.is(true), "Action doesn't exist in snackbar");
return this;
}

@JDIAction(value = "Assert that '{name}' has no action icon", isAssert = true)
@JDIAction(value = "Assert that '{name}' has no action button", isAssert = true)
public SnackbarAssert hasNoAction() {
jdiAssert(element().actionIcon()
jdiAssert(element().action()
.isNotExist(), Matchers.is(true), "Action exists in snackbar");
return this;
}

@JDIAction(value = "Assert that '{name}' is visible during '{0}' seconds", isAssert = true)
public SnackbarAssert notHidden(int timeoutSec) {
JAction action = () -> {
element().base()
.timer()
.wait(() -> element().isDisplayed());
};
boolean result = new Timer(timeoutSec * 1000L).wait(action);
jdiAssert(result ? "displayed" : "hidden", Matchers.is("displayed"));
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,80 +12,34 @@
*/

public class Snackbar extends UIBaseElement<SnackbarAssert> implements HasPosition {
@Deprecated
private UIElement message;
@Deprecated
protected UIElement action;
private String messageLocator = ".mat-mdc-snack-bar-label .mdc-snackbar__label";
private String actionLocator = ".//button";
private static final String MESSAGE_LOCATOR = ".mat-mdc-snack-bar-label .mdc-snackbar__label";
private static final String ACTION_LOCATOR = ".//button";
private static final String OVERLAY_LOCATOR = "//ancestor::div[@class='cdk-global-overlay-wrapper']";

@Deprecated
public Snackbar() {
message = new UIElement();
message.core()
.setLocator(messageLocator);

action = new UIElement();
action.core()
.setLocator(actionLocator);
}

@Deprecated
@JDIAction("Get '{name}' message")
public String getMessageText() {
return message.getValue();
}

@Deprecated
@JDIAction("Get '{name}' action")
public String getActionText() {
return action.getValue();
public UIElement action() {
return core().find(ACTION_LOCATOR);
}

@JDIAction("Get '{name}' message")
public String messageText() {
return message.getValue();
}

@JDIAction("Get '{name}' action")
public String actionText() {
return action.getValue();
}

@Deprecated
@JDIAction("Click '{name}' action")
public void clickAction() {
action.click();
}

@JDIAction("Click '{name}' action")
public UIElement actionIcon() {
return this.action;
}

@Deprecated
@JDIAction("Is '{name}' action displayed")
public boolean isActionDisplayed() {
return action.isDisplayed();
}

@Override
public SnackbarAssert is() {
return new SnackbarAssert().set(this);
public UIElement message() {
return core().find(MESSAGE_LOCATOR);
}

@Override
@JDIAction("Get '{name}' position")
public Position position() {
return null;
return getPositionFromAttribute("style");
}

@Override
public Position getPositionFromClass(final UIElement element, final String className) {
return HasPosition.super.getPositionFromClass(element, className);
public Position getPositionFromAttribute(String attributeValue) {
return Position.fromFullString(core().find(OVERLAY_LOCATOR)
.attr(attributeValue));
}

@Override
public Position getPositionFromAttribute(final String attributeValue) {
return HasPosition.super.getPositionFromAttribute(attributeValue);
public SnackbarAssert is() {
return new SnackbarAssert().set(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,30 @@
* Each constant includes information about its string representation.
*/
public enum Position {
TOP("top"),
BOTTOM("bottom"),
LEFT("left"),
RIGHT("right"),
END("end"),
START("start"),
TOP_RIGHT("topRight"),
TOP_LEFT("topLeft"),
TOP_CENTER("topCenter"),
BOTTOM_CENTER("bottomCenter"),
BOTTOM_RIGHT("bottomRight"),
BOTTOM_LEFT("bottomLeft"),
STATIC("Static");
TOP("align-items: flex-start"),
BOTTOM("align-items: flex-end"),

START("justify-content: flex-start"),
CENTER("justify-content: center"),
END("justify-content: flex-end"),
LEFT("justify-content: flex-start"),
RIGHT("justify-content: flex-end"),

START_BOTTOM("justify-content: flex-start; align-items: flex-end;"),
CENTER_BOTTOM("justify-content: center; align-items: flex-end;"),
END_BOTTOM("justify-content: flex-end; align-items: flex-end;"),
LEFT_BOTTOM("justify-content: flex-start; align-items: flex-end;"),
RIGHT_BOTTOM("justify-content: flex-end; align-items: flex-end;"),

START_TOP("justify-content: flex-start; align-items: flex-start;"),

CENTER_TOP("justify-content: center; align-items: flex-start;"),

END_TOP("justify-content: flex-end; align-items: flex-start;"),

LEFT_TOP("justify-content: flex-start; align-items: flex-start;"),

RIGHT_TOP("justify-content: flex-end; align-items: flex-start;");

private final String value;

Expand All @@ -46,28 +57,28 @@ public static Position fromFullString(String text) {
}
return Arrays.stream(Position.values())
.filter(p -> StringUtils.containsAnyIgnoreCase(text, p.toString()))
.max(Comparator.comparing(p -> p.toString().length()))
.max(Comparator.comparing(p -> p.toString()
.length()))
.orElseThrow(() -> runtimeException(String.format("No appropriate %s constant found for value '%s'", Position.class.getName(), text)));
}

public static Position fromClasses(List<String> classes, String stylePrefix, String stylePostfix) {
if (classes == null || classes.isEmpty() || StringUtils.isBlank(stylePrefix)) {
throw runtimeException(String.format("%s: input string can't be empty",
Position.class.getName()));
throw runtimeException(String.format("%s: input string can't be empty", Position.class.getName()));
}
String positionClass = classes.stream()
.filter(c -> StringUtils.containsAnyIgnoreCase(c, stylePrefix))
.map(c -> c.replaceFirst(stylePrefix, "").replace(stylePostfix, ""))
.findFirst().orElse("");
.map(c -> c.replaceFirst(stylePrefix, "")
.replace(stylePostfix, ""))
.findFirst()
.orElse("");
if (StringUtils.isBlank(positionClass)) {
throw runtimeException(String.format("%s: input string can't be empty",
Position.class.getName()));
throw runtimeException(String.format("%s: input string can't be empty", Position.class.getName()));
}
return Arrays.stream(Position.values())
.filter(p -> StringUtils.equalsIgnoreCase(positionClass, p.toString()))
.findAny()
.orElseThrow(() -> runtimeException(String.format("No appropriate %s constant found for value '%s'",
Position.class.getName(), positionClass)));
.orElseThrow(() -> runtimeException(String.format("No appropriate %s constant found for value '%s'", Position.class.getName(), positionClass)));
}

/**
Expand Down

0 comments on commit dfd98aa

Please sign in to comment.