All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.runner.utils.SelenideUtils Maven / Gradle / Ivy

The newest version!
package com.xlrit.gears.runner.utils;

import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Pattern;

import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.WebDriverRunner;
import com.xlrit.gears.base.util.TemporalUtils;
import org.openqa.selenium.Keys;

import static com.codeborne.selenide.Condition.*;
import static com.codeborne.selenide.Selectors.byAttribute;
import static com.codeborne.selenide.Selectors.byCssSelector;

public class SelenideUtils {
	public static final Pattern DATE_PICKER = Pattern.compile("grs-Input(?:(?:Date)|(?:Time)|(?:DateTime))Field");
	public static final Pattern PERIOD = Pattern.compile("PT(?:(?[0-9]+)H)?(?:(?[0-9]+)M)?(?:(?[0-9]+)S)?", Pattern.CASE_INSENSITIVE);

	public static final String JS_GET_HEIGHT = """
			var pageHeight = 0;
			   
			function findHighestNode(nodesList) {
				for (var i = nodesList.length - 1; i >= 0; i--) {
					if (nodesList[i].scrollHeight && nodesList[i].clientHeight) {
						var elHeight = Math.max(nodesList[i].scrollHeight, nodesList[i].clientHeight);
						pageHeight = Math.max(elHeight, pageHeight);
					}
					if (nodesList[i].childNodes.length) findHighestNode(nodesList[i].childNodes);
				}
			}
		   
			findHighestNode(document.documentElement.childNodes);
			return pageHeight;
			""";

	private static final DateTimeFormatter dateFormatter     = DateTimeFormatter.ofPattern("ddMMyyyy");
	private static final DateTimeFormatter timeFormatter     = DateTimeFormatter.ofPattern("HHmmss");
	private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");

	public static String cssId(String id) {
		return String.format("#%s", escapeCSS(id));
	}

	private static String escapeCSS(String id) {
		return id
				.replace(".", "\\.")
				.replace("[", "\\[")
				.replace("]", "\\]");
	}

	public static String dateTimeToInput(String dateTime, String parentClass) {
		if (parentClass.contains("grs-InputDateField")) {
			return TemporalUtils.parseDate(dateTime).format(dateFormatter);
		} else if (parentClass.contains("grs-InputTimeField")) {
			return TemporalUtils.parseTime(dateTime).format(timeFormatter);
		} else if (parentClass.contains("grs-InputDateTimeField")) {
			return TemporalUtils.parseDateTime(dateTime).format(dateTimeFormatter);
		}
		return null;
	}

	public static void clear(SelenideElement element) {
		element.sendKeys(Keys.END);
		int length = element.getValue() == null ? 0 : element.getValue().length();
		Keys[] backSpaces = new Keys[length];
		Arrays.fill(backSpaces, Keys.BACK_SPACE);
		while (!Objects.requireNonNull(element.getValue()).isEmpty()) {
			element.sendKeys(backSpaces);
		}
	}

	public static boolean alreadyCorrect(String inputValue, SelenideElement element) {
		return inputValue.equals(element.getAttribute("value"));
	}

	public static boolean hasRow(SelenideElement table, int rowNr) {
		if (!table.findAll(String.format("#row-%d-add-button", rowNr)).isEmpty()) return true;
		String id = table.getAttribute("id");
		return !table.findAll(byCssSelector("[id]"))
				.filter(attributeMatching("id", String.format(".*%s\\[%d\\].*", escapeCSS(id), rowNr)))
				.isEmpty();
	}

	public static boolean urlMatch(String pattern) {
		return Pattern.matches(pattern, WebDriverRunner.url());
	}

	public static String getFormId() {
		String[] split = WebDriverRunner.url().split("/");
		return split[split.length - 1];
	}

	public static boolean isMultipleTable(SelenideElement element) {
		return element.has(tagName("table"));
	}

	public static boolean isCombobox(SelenideElement element) {
		return element.has(attribute("role", "combobox"));
	}

	public static boolean isSegmentedButton(SelenideElement element) {
		SelenideElement child = element.find(byAttribute("name", element.getAttribute("id")));
		return child.exists() && child.has(attribute("role", "group"));
	}

	public static boolean isTextInput(SelenideElement element) {
		return (element.has(or("type",
				attribute("type", "text"),
				attribute("type", "number"))) &&
				!isCombobox(element));
	}

	public static boolean isTextArea(SelenideElement element) {
		return element.toWebElement().getTagName().equals("textarea");
	}

	public static boolean isFileInput(SelenideElement element) {
		return element.find(byAttribute("type", "file")).exists();
	}

	public static boolean isDatePicker(SelenideElement element) {
		return DATE_PICKER.matcher(element.parent().parent().getAttribute("class")).find();
	}

	public static boolean isPeriodInput(SelenideElement element, String id) {
		return element.$(cssId(id + "_hours")).exists()
				&& element.$(cssId(id + "_minutes")).exists()
				&& element.$(cssId(id + "_seconds")).exists();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy