use of ru.sbtqa.tag.qautils.errors.AutotestError in project page-factory-2 by sbtqa.
the class WebElementsPage method checkValue.
/**
* Define a type of given WebElement, and check whether it either contains,
* or exactly matches given text in its value. Currently supported elements
* are text input and select box TODO: use HtmlElements here, to define
* which element we are dealing with
*
* @param text string value that will be searched inside of the element
* @param webElement WebElement to check
* @param searchStrategy match strategy. See available strategies in
* {@link MatchStrategy}
*/
protected void checkValue(String text, WebElement webElement, MatchStrategy searchStrategy) {
String value = "";
switch(searchStrategy) {
case EXACT:
try {
switch(webElement.getTagName()) {
case "input":
value = webElement.getAttribute("value");
Assert.assertEquals(text.replaceAll("\\s+", ""), value.replaceAll("\\s+", ""));
break;
case "select":
value = webElement.getAttribute("title");
if (value.isEmpty() || !value.replaceAll("\\s+", "").equals(text.replaceAll("\\s+", ""))) {
value = webElement.getText();
}
Assert.assertEquals(text.replaceAll("\\s+", ""), value.replaceAll("\\s+", ""));
break;
default:
value = webElement.getText();
Assert.assertEquals(text.replaceAll("\\s+", ""), value.replaceAll("\\s+", ""));
break;
}
} catch (Exception | AssertionError exception) {
throw new AutotestError("The actual value '" + value + "' of WebElement '" + webElement + "' are not equal to expected text '" + text + "'", exception);
}
break;
case CONTAINS:
try {
switch(webElement.getTagName()) {
case "input":
value = webElement.getAttribute("value");
Assert.assertTrue(value.replaceAll("\\s+", "").contains(text.replaceAll("\\s+", "")));
break;
case "select":
value = webElement.getAttribute("title");
if (value.isEmpty() || !value.replaceAll("\\s+", "").contains(text.replaceAll("\\s+", ""))) {
value = webElement.getText();
}
Assert.assertTrue(value.replaceAll("\\s+", "").contains(text.replaceAll("\\s+", "")));
break;
default:
value = webElement.getText();
Assert.assertTrue(value.replaceAll("\\s+", "").contains(text.replaceAll("\\s+", "")));
break;
}
} catch (Exception | AssertionError exception) {
throw new AutotestError("The actual value '" + value + "' of WebElement '" + webElement + "' are not equal to expected text '" + text + "'", exception);
}
break;
}
}
use of ru.sbtqa.tag.qautils.errors.AutotestError in project page-factory-2 by sbtqa.
the class WebElementsPage method select.
/**
* Try to extract selectable options form given WebElement, and select
* required one Add corresponding parameter to allure report
*
* @param webElement WebElement for interaction. Element is supposed to be
* selectable, i.e. have select options
* @param option the value to match against
* @param strategy the strategy to match value. See {@link MatchStrategy}
* for available values
*/
@SuppressWarnings("unchecked")
protected void select(WebElement webElement, String option, MatchStrategy strategy) {
String jsString = "" + "var content=[]; " + "var options = arguments[0].getElementsByTagName('option'); " + " for (var i=0; i<options.length;i++){" + " content.push(options[i].text)" + "}" + "return content";
List<String> options = (ArrayList<String>) ((JavascriptExecutor) PageFactory.getDriver()).executeScript(jsString, webElement);
boolean isSelectionMade = false;
for (int index = 0; index < options.size(); index++) {
boolean isCurrentOption = false;
String optionText = options.get(index).replaceAll("\\s+", "");
String needOptionText = option.replaceAll("\\s+", "");
if (strategy.equals(MatchStrategy.CONTAINS)) {
isCurrentOption = optionText.contains(needOptionText);
} else if (strategy.equals(MatchStrategy.EXACT)) {
isCurrentOption = optionText.equals(needOptionText);
}
if (isCurrentOption) {
Select select = new Select(webElement);
select.selectByIndex(index);
isSelectionMade = true;
break;
}
}
if (!isSelectionMade) {
throw new AutotestError("There is no element '" + option + "' in " + ReflectionUtil.getElementTitle(PageContext.getCurrentPage(), webElement));
}
ParamsHelper.addParam(ReflectionUtil.getElementTitle(PageContext.getCurrentPage(), webElement), option);
}
use of ru.sbtqa.tag.qautils.errors.AutotestError in project page-factory-2 by sbtqa.
the class PlaceholderUtils method replaceTemplatePlaceholders.
/**
* Replace placeholders in string on parameters
*
* @param string replace placeholders in this string
* @param parameters replace these parameters
* @return string with replaced placeholders
*/
public static String replaceTemplatePlaceholders(EndpointEntry entry, String string, Map<String, Object> parameters) {
for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
String parameterName = parameter.getKey();
Object parameterValue = parameter.getValue();
if (isFieldExists(entry, parameterName)) {
Field declaredField = FieldUtils.getAllFieldsList(entry.getClass()).stream().filter(field -> field.getName().equals(parameterName)).findFirst().orElseThrow(() -> new AutotestError("This error should never appear"));
string = replacePlaceholder(string, declaredField, parameterName, parameterValue);
} else {
string = replacePlaceholder(string, null, parameterName, parameterValue);
}
}
return string;
}
use of ru.sbtqa.tag.qautils.errors.AutotestError in project page-factory-2 by sbtqa.
the class SelectAbstract method selectByValue.
/**
* Selects an option for the text value in the specified element
*
* @param value the value to be selected
* @param element the element within which the search will occur
*/
public void selectByValue(String value, String element) throws AutotestError {
open();
List<? extends BaseElement> listOp = getOptions();
for (BaseElement op : listOp) {
if (value.equals(op.getElement(element).getText())) {
op.click();
return;
}
}
close();
throw new AutotestError(format(NOT_FOUND_OPTION_IN_ELEMENT_TEMPLATE, value, element));
}
use of ru.sbtqa.tag.qautils.errors.AutotestError in project page-factory-2 by sbtqa.
the class CustomHtmlElementDecorator method decorateTypifiedElementList.
@Override
protected <T extends TypifiedElement> List<T> decorateTypifiedElementList(ClassLoader loader, Field field) {
final Class<T> elementClass = (Class<T>) HtmlElementUtils.getGenericParameterClass(field);
final ElementLocator locator = factory.createLocator(field);
final String name = getElementName(field);
InvocationHandler handler = new TypifiedElementListNamedProxyHandler(elementClass, locator, name) {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
if ("toString".equals(method.getName())) {
return name;
}
List<T> elements = new LinkedList<>();
int elementNumber = 0;
for (WebElement element : locator.findElements()) {
String newName = String.format("%s [%d]", name, elementNumber++);
elements.add(createTypifiedElement(elementClass, element, newName));
}
try {
return method.invoke(elements, objects);
} catch (InvocationTargetException e) {
throw new AutotestError("Error initializing elements.", e);
}
}
};
return createTypifiedElementListProxy(loader, handler);
}
Aggregations