
pickleib.web.interactions.WebInteractions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Pickleib Show documentation
Show all versions of Pickleib Show documentation
Pickleib library for supporting web application automation with Selenium
The newest version!
package pickleib.web.interactions;
import collections.Bundle;
import context.ContextStore;
import org.jetbrains.annotations.NotNull;
import org.openqa.selenium.Keys;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.FluentWait;
import pickleib.enums.ElementState;
import pickleib.enums.InteractionType;
import pickleib.enums.Navigation;
import pickleib.exceptions.PickleibException;
import pickleib.exceptions.PickleibVerificationException;
import pickleib.utilities.element.acquisition.ElementAcquisition;
import pickleib.utilities.interfaces.PolymorphicUtilities;
import pickleib.web.driver.PickleibWebDriver;
import pickleib.web.utilities.WebUtilities;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static utils.StringUtilities.Color.BLUE;
import static utils.StringUtilities.Color.GRAY;
import static utils.StringUtilities.contextCheck;
import static utils.StringUtilities.highlighted;
@SuppressWarnings("unused")
public class WebInteractions extends WebUtilities implements PolymorphicUtilities {
public WebInteractions(RemoteWebDriver driver, FluentWait wait) {
super(driver);
this.wait = wait;
}
public WebInteractions() {
super(PickleibWebDriver.get());
}
/**
* Adds given values to the local storage
*
* @param form Map(String, String)
*/
public void addLocalStorageValues(Map form) {
log.info("Adding local storage values ... ");
super.addValuesToLocalStorage(form);
}
/**
* Adds given cookies
*
* @param cookies Map(String, String)
*/
public void addCookies(Map cookies) {
log.info("Adding cookies ... ");
super.addCookies(cookies);
}
/**
* Updates given cookies
*
* @param cookieValue
* @param cookieName
*/
public void updateCookies(String cookieValue, String cookieName) {
log.info("Updating cookies ... ");
super.updateCookies(cookieValue, cookieName);
}
/**
* Deletes all cookies
*/
public void deleteCookies() {
log.info("Deleting cookies ... ");
super.deleteAllCookies();
}
/**
* Clicks a button by its {text} text
*
* @param text target text
*/
public void clickByText(String text) {
log.info("Clicking button by text " + highlighted(BLUE, text));
super.clickButtonWithText(text, false);
}
/**
* Clicks an element acquired by text with optional scrolling.
*
* @param buttonText target element text
* @param scroll scrolls if true
*/
public void clickByText(String buttonText, boolean scroll) {
log.info("Clicking button with text " + highlighted(BLUE, buttonText));
super.clickButtonWithText(buttonText, scroll);
}
/**
* Acquires a specified attribute of a given {element} with the {element name} on the {page name}.
*
* @param element target element
* @param attribute target attribute
* @param elementName target element name
* @param pageName specified page instance name
* @return returns the element attribute
*/
public String getAttribute(WebElement element, String attribute, String elementName, String pageName) {
log.info("Acquiring " +
highlighted(BLUE, attribute) +
highlighted(GRAY, " from ") +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
return super.getAttribute(element, attribute);
}
/**
* Clicks an {element} with the {element name} on the {page name} with optional scrolling
* after waiting for its state to be enabled
*
* @param element target element
* @param scroll scrolls if true
* @param elementName target element name
* @param pageName specified page instance name
*/
public void clickElement(WebElement element, String elementName, String pageName, boolean scroll) {
log.info("Clicking " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
super.clickElement(element, scroll);
}
/**
* Clicks an {element} with the {element name} on the {page name} after waiting for its state to be enabled
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
*/
public void clickElement(WebElement element, String elementName, String pageName) {
log.info("Clicking " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
super.clickElement(element);
}
/**
* Saves element attribute to context
*
* @param element target element
* @param attributeName attribute name
* @param elementName target element name
* @param pageName specified page instance name
*/
public void saveAttributeValue(WebElement element, String attributeName, String elementName, String pageName) {
log.info("Saving attribute " +
highlighted(BLUE, attributeName) +
highlighted(GRAY, " of ") +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName) +
highlighted(GRAY, " to context")
);
super.updateContextByElementAttribute(element, attributeName, elementName, pageName);
log.success("Attribute " + attributeName + " of " + elementName + " from " + pageName + " was saved to context!");
}
/**
* Clicks coordinates specified by the given offsets from the center of a given {element}
* with the {element name} on the {page name}.
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
*/
public void clickTowards(WebElement element, String elementName, String pageName) {
log.info("Clicking towards " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
super.clickTowards(element);
}
/**
* Clicks an {element} with the {element name} on the {page name} after waiting for its state to be enabled.
*
* @param element target element
* @param scroll scrolls if true
* @param elementName target element name
* @param pageName specified page instance name
*/
public void clickIfPresent(WebElement element, Boolean scroll, String elementName, String pageName) {
log.info("Checking if " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " presents on the ") +
highlighted(BLUE, pageName)
);
super.clickIfPresent(element, scroll);
}
/**
* Clears and fills a given input with scroll option.
*
* @param inputElement target input element
* @param elementName target element name
* @param pageName specified page instance name
* @param inputText input text
* @param scroll scrolls if true
* @param verify verifies the input text value equals to an expected text if true
*/
public void clearFillInput(WebElement inputElement, String elementName, String pageName, String inputText, boolean scroll, boolean verify) {
log.info("Clearing input " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
super.clearFillInput(inputElement, inputText, scroll, verify);
}
/**
* Clears and fills a given input without scroll option.
*
* @param inputElement target input element
* @param elementName target element name
* @param pageName specified page instance name
* @param inputText input text
* @param verify verifies the input text value equals to an expected text if true
*/
public void clearFillInput(WebElement inputElement, String elementName, String pageName, String inputText, boolean verify) {
log.info("Clearing input " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
super.clearFillInput(inputElement, inputText, false, verify);
}
/**
* Fills input form
*
* @param bundles list of bundles where input element, input text and boolean value
* (true - if the input text value equals to an expected text) are stored
* @param pageName specified page instance name
*/
public void fillForm(List> bundles, String pageName) {
log.info("Filling form on " + highlighted(BLUE, pageName));
super.fillInputForm(bundles, pageName);
log.success("Form was filled on " + highlighted(BLUE, pageName));
}
/**
* Verifies the text of {element name} on the {page name} to be: {expected text}
*
* @param element target element
* @param expectedText expected text
*/
public void verifyText(WebElement element, String elementName, String pageName, String expectedText) {
log.info("Verifying text " +
highlighted(BLUE, expectedText) +
highlighted(GRAY, " of element ") +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
super.verifyElementText(element, expectedText);
log.success("Text of element " + elementName + " is " + expectedText + " on " + pageName);
}
/**
* Verifies the text of {element name} on the {page name} to contain: {expected text}
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
* @param expectedText expected text
*/
public void verifyContainsText(WebElement element, String elementName, String pageName, String expectedText) {
log.info("Verifying that text of element " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " contains ") +
highlighted(BLUE, expectedText) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
super.verifyElementContainsText(element, expectedText);
log.success("Text of element " + elementName + " contains " + expectedText + " on " + pageName);
}
/**
* Verifies the text of an element from the list on the {page name}.
*
* @param bundles list of bundles where element text, element name and expected text are stored
* @param pageName specified page instance name
*/
public void verifyListedText(List> bundles, String pageName) {
log.info("Verifying the text of elements on " + highlighted(BLUE, pageName));
super.verifyListedElementText(bundles, pageName);
log.success("Text of the elements was verified on " + pageName);
}
/**
* Verifies the presence of an element {element name} on the {page name}
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
*/
public void verifyPresence(WebElement element, String elementName, String pageName) {
log.info("Verifying the presence of " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
super.verifyElementState(element, ElementState.displayed);
log.success("Presence of the element " + elementName + " was verified on " + pageName);
}
/**
* Verifies a given element is in expected state
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
* @param state expected state
* @return returns the element if its in expected state
*/
public WebElement verifyElementState(WebElement element, String elementName, String pageName, ElementState state) {
log.info("Verifying that the state of " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " is ") +
highlighted(BLUE, state.name()) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
return super.verifyElementState(element, state);
}
/**
* Waits until a given element is in expected state
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
* @param state expected state
* @return returns true if an element is in the expected state
*/
public Boolean elementIs(WebElement element, String elementName, String pageName, @NotNull ElementState state) {
log.info("Verifying that the state of " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " is ") +
highlighted(BLUE, state.name()) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
return super.elementIs(element, state);
}
/**
* Acquires listed element by its name
*
* @param items list that includes target element
* @param selectionName element name
* @param pageName specified page instance name
* @return returns the selected element
*/
public WebElement acquireNamedElementAmongst(List items, String selectionName, String pageName) {
log.info("Acquiring element " +
highlighted(BLUE, selectionName) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
return ElementAcquisition.acquireNamedElementAmongst(items, selectionName);
}
/**
* Acquires a listed element by its attribute
*
* @param items list that includes target element
* @param attributeName attribute name
* @param attributeValue attribute value
* @param pageName specified page instance name
* @return returns the selected element
*/
public WebElement acquireElementUsingAttributeAmongst(List items, String attributeName, String attributeValue, String pageName) {
log.info("Acquiring element using attribute " +
highlighted(BLUE, attributeName) +
highlighted(GRAY, " with value ") +
highlighted(BLUE, attributeValue) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
return ElementAcquisition.acquireElementUsingAttributeAmongst(items, attributeName, attributeValue);
}
/**
* Clears an input {element} with the {element name} on the {page name}.
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
*/
public WebElement clearInputField(@NotNull WebElement element, String elementName, String pageName) {
log.info("Clearing " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
return super.clearInputField(element);
}
/**
* Acquires an {element} by its text on the {page name}.
*
* @param elementText target element text
* @param pageName specified page instance name
*/
public WebElement getElementByText(String elementText, String pageName) {
log.info("Acquiring element by text " +
highlighted(BLUE, elementText) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
return super.getElementByText(elementText);
}
/**
* Acquires an {element} that contains a certain text on the {page name}.
*
* @param elementText target element text
* @param pageName specified page instance name
*/
public WebElement getElementContainingText(String elementText, String pageName) {
log.info("Acquiring element by containing text " +
highlighted(BLUE, elementText) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
return super.getElementContainingText(elementText);
}
/**
* Drags and drops a given element on top of another element
*
* @param element element that drags
* @param elementName target element name
* @param destinationElement target element
* @param destinationElementName target destination element name
* @param pageName specified page instance name
*/
public void dragDropToAction(WebElement element, String elementName, WebElement destinationElement, String destinationElementName, String pageName) {
log.info("Drag drop from " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " to ") +
highlighted(BLUE, destinationElementName) +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
super.dragDropToAction(element, destinationElement);
}
/**
* Drags and drops a given element to coordinates specified by offsets from the center of the element
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
* @param xOffset x offset from the center of the element
* @param yOffset y offset from the center of the element
*/
public void dragDropByAction(WebElement element, String elementName, String pageName, int xOffset, int yOffset) {
log.info("Drag drop " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " to point (") +
highlighted(BLUE, String.valueOf(xOffset)) +
highlighted(GRAY, "; ") +
highlighted(BLUE, String.valueOf(yOffset)) +
highlighted(GRAY, ") ") +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
super.dragDropByAction(element, xOffset, yOffset);
}
/**
* Drags and drops a given element to coordinates specified by offsets from the center of the element
* Uses moveToElement()
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
* @param xOffset x offset from the center of the element
* @param yOffset y offset from the center of the element
*/
public void dragDropAction(WebElement element, String elementName, String pageName, int xOffset, int yOffset) {
log.info("Drag drop " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " to point (") +
highlighted(BLUE, String.valueOf(xOffset)) +
highlighted(GRAY, "; ") +
highlighted(BLUE, String.valueOf(yOffset)) +
highlighted(GRAY, ") ") +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
super.dragDropAction(element, xOffset, yOffset);
}
/**
* Clicks coordinates specified by the given offsets from the center of a given element
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
* @param xOffset x offset from the center of the element
* @param yOffset y offset from the center of the element
*/
@SuppressWarnings("SameParameterValue")
public void clickAtAnOffset(WebElement element, String elementName, String pageName, int xOffset, int yOffset) {
log.info("Clicking " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " at point (") +
highlighted(BLUE, String.valueOf(xOffset)) +
highlighted(GRAY, "; ") +
highlighted(BLUE, String.valueOf(yOffset)) +
highlighted(GRAY, ") ") +
highlighted(GRAY, " on ") +
highlighted(BLUE, pageName)
);
super.clickAtAnOffset(element, xOffset, yOffset);
}
/**
* Waits for absence of an element {element name} on the {page name}
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
*/
public void waitUntilAbsence(WebElement element, String elementName, String pageName) {
log.info("Waiting for absence of " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
boolean absent = super.elementIs(element, ElementState.absent);
log.info("Element is absent ? " + highlighted(BLUE, String.valueOf(absent)));
}
/**
* Waits for element {element name} on the {page name} to be visible
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
*/
public void waitUntilVisible(WebElement element, String elementName, String pageName) {
log.info("Waiting visibility of " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
boolean visible = super.elementIs(element, ElementState.displayed);
log.info("Element is visible ? " + highlighted(BLUE, String.valueOf(visible)));
}
/**
* Waits until an element {element name} on the {page name} has {attribute value} value for its {attribute name} attribute
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
* @param attributeValue expected attribute value
* @param attributeName target attribute name
*/
public void waitUntilElementContainsAttribute(WebElement element, String elementName, String pageName, String attributeName, String attributeValue) {
log.info("Waiting until element " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " contains ") +
highlighted(BLUE, attributeValue) +
highlighted(GRAY, " in its ") +
highlighted(BLUE, attributeName) +
highlighted(GRAY, " attribute.")
);
boolean attributeFound = super.elementContainsAttribute(element, attributeName, attributeValue);
log.info("Attribute match ? " + highlighted(BLUE, String.valueOf(attributeFound)));
}
/**
* Verifies that element {element name} on the {page name} has {attribute value} value for its {attribute name} attribute
*
* @param element target element
* @param elementName target element name
* @param pageName specified page instance name
* @param attributeValue expected attribute value
* @param attributeName target attribute name
*/
public void verifyElementContainsAttribute(WebElement element, String elementName, String pageName, String attributeName, String attributeValue) {
log.info("Verifying that " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " contains ") +
highlighted(BLUE, attributeValue) +
highlighted(GRAY, " in its ") +
highlighted(BLUE, attributeName) +
highlighted(GRAY, " attribute.")
);
if (!super.elementContainsAttribute(element, attributeName, attributeValue))
throw new PickleibException(
"The " + attributeName + " attribute of element " + elementName + " could not be verified." +
"\nExpected value: " + attributeValue + "\nActual value: " + element.getAttribute(attributeName)
);
log.success("The " + attributeName + " attribute of element " + elementName + " is verified!");
}
/**
* Verifies that an attribute {attribute name} of element {element name} on the {page name} contains a specific {value}.
*
* @param attributeName the name of the attribute to be verified
* @param elementName the name of the element to be verified
* @param pageName the name of the page containing the element
* @param value the expected part of value of the attribute
*/
public void verifyElementAttributeContainsValue(WebElement element, String attributeName, String elementName, String pageName, String value) {
log.info("Verifying that " +
highlighted(BLUE, elementName) +
highlighted(GRAY, " contains ") +
highlighted(BLUE, value) +
highlighted(GRAY, " in its ") +
highlighted(BLUE, attributeName) +
highlighted(GRAY, " attribute.")
);
if (!super.elementAttributeContainsValue(element, attributeName, value))
throw new PickleibVerificationException(
"The " + attributeName + " attribute of element " + elementName + " could not be verified." +
"\nExpected value: " + value + "\nActual value: " + element.getAttribute(attributeName)
);
log.success("The " + attributeName + " attribute of element " + elementName + " is verified!");
}
/**
* Verifies {attribute name} css attribute of an element {element name} on the {page name} is {attribute value}
*
* @param element target element
* @param attributeName target attribute name
* @param elementName target attribute name
* @param pageName specified page instance name
* @param attributeValue expected attribute value
*/
public void verifyElementColor(WebElement element, String attributeName, String elementName, String pageName, String attributeValue) {
log.info("Verifying " +
highlighted(BLUE, attributeName) +
highlighted(GRAY, " attribute of ") +
highlighted(BLUE, elementName) +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName)
);
if (!attributeValue.equals(element.getCssValue(attributeName)))
throw new PickleibException(
"The " + attributeName + " attribute of element " + elementName + " could not be verified." +
"\nExpected value: " + attributeValue + "\nActual value: " + element.getCssValue(attributeName)
);
log.success("Value of '" + attributeName + "' attribute is verified to be '" + attributeValue + "'!");
}
/**
* Verifies the presence of listed element from a list on the {page name}
*
* @param bundles list that contains element, elementName, elementText
* @param pageName specified page instance name
* @param signForms table that has key as "Input" and value as "Input Element" (dataTable.asMaps())
*/
public void verifyPresenceOfListedElements(List> bundles, WebElement element, List elements, String pageName, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy