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

au.com.agic.apptesting.utils.impl.GetByImpl Maven / Gradle / Ivy

package au.com.agic.apptesting.utils.impl;

import static com.google.common.base.Preconditions.checkState;

import au.com.agic.apptesting.exception.InvalidInputException;
import au.com.agic.apptesting.utils.GetBy;
import au.com.agic.apptesting.utils.ThreadDetails;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;

/**
 * Implementation of the GetBy service
 */
public class GetByImpl implements GetBy {
	@Override
	public By getBy(
			final String selector,
			final boolean valueAlias,
			final String value,
			final ThreadDetails threadDetails) {

		final String fixedValue = valueAlias ? threadDetails.getDataSet().get(value) : value;

		checkState(StringUtils.isNotBlank(fixedValue), "Selector or alias is blank");

		if (ID.equals(selector)) {
			return By.id(fixedValue);
		}

		if (XPATH.equals(selector)) {
			return By.xpath(fixedValue);
		}

		if (CLASS.equals(selector)) {
			return By.cssSelector("." + fixedValue);
		}

		if (NAME.equals(selector)) {
			return By.name(fixedValue);
		}

		if (VALUE.equals(selector)) {
			final String escaped = fixedValue.replaceAll("'", "\'");
			return By.cssSelector("[value='" + escaped + "']");
		}

		if (CSS_SELECTOR.equals(selector)) {
			return By.cssSelector(fixedValue);
		}

		if (TEXT.equals(selector)) {
			final String escaped = fixedValue.replaceAll("'", "''");
			return By.xpath("//*[contains(text(),'" + escaped + "')]");
		}

		throw new InvalidInputException("Unexpected selector");
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy