Search in sources :

Example 1 with AutotestError

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;
    }
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) PageException(ru.sbtqa.tag.pagefactory.exceptions.PageException) WaitException(ru.sbtqa.tag.pagefactory.exceptions.WaitException) ElementNotFoundException(ru.sbtqa.tag.pagefactory.exceptions.ElementNotFoundException)

Example 2 with AutotestError

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);
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) ArrayList(java.util.ArrayList) Select(org.openqa.selenium.support.ui.Select)

Example 3 with AutotestError

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;
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) Field(java.lang.reflect.Field) Map(java.util.Map)

Example 4 with AutotestError

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));
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) BaseElement(ru.sbtqa.tag.pagefactory.elements.BaseElement)

Example 5 with AutotestError

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);
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) ElementLocator(org.openqa.selenium.support.pagefactory.ElementLocator) Method(java.lang.reflect.Method) WebElement(org.openqa.selenium.WebElement) InvocationHandler(java.lang.reflect.InvocationHandler) LinkedList(java.util.LinkedList) InvocationTargetException(java.lang.reflect.InvocationTargetException) TypifiedElementListNamedProxyHandler(ru.yandex.qatools.htmlelements.loader.decorator.proxyhandlers.TypifiedElementListNamedProxyHandler)

Aggregations

AutotestError (ru.sbtqa.tag.qautils.errors.AutotestError)13 Field (java.lang.reflect.Field)4 WebElement (org.openqa.selenium.WebElement)3 BaseElement (ru.sbtqa.tag.pagefactory.elements.BaseElement)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ElementDescriptionException (ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException)2 ElementNotFoundException (ru.sbtqa.tag.pagefactory.exceptions.ElementNotFoundException)2 TypeToken (com.google.common.reflect.TypeToken)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ToNumberPolicy (com.google.gson.ToNumberPolicy)1 Result (cucumber.api.Result)1 TestCaseFinished (cucumber.api.event.TestCaseFinished)1 TestResult (io.qameta.allure.model.TestResult)1 String.format (java.lang.String.format)1 Annotation (java.lang.annotation.Annotation)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1