Search in sources :

Example 11 with AutotestError

use of ru.sbtqa.tag.qautils.errors.AutotestError in project page-factory-2 by sbtqa.

the class HtmlFindUtils method findComplexElement.

private <T extends WebElement> ComplexElement findComplexElement(T context, String name, boolean wait) {
    ComplexElement<T> element = new ComplexElement<>(context, name, wait);
    try {
        for (; element.getCurrentPosition() < element.getElementPath().size(); element.setCurrentPosition(element.getCurrentPosition() + 1)) {
            Field field = getField(element.getElement(), element.getCurrentName());
            if (field == null) {
                throw new ElementSearchError("No element declared on page " + formErrorMessage(element));
            }
            findElement(field, element);
        }
        if (element.getCurrentPosition() > 0) {
            element.setCurrentPosition(element.getCurrentPosition() - 1);
        }
    } catch (AutotestError | IllegalArgumentException | ElementDescriptionException ex) {
        throw new ElementSearchError("Element " + formErrorMessage(element), ex);
    }
    return element;
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) Field(java.lang.reflect.Field) ElementSearchError(ru.sbtqa.tag.pagefactory.exception.ElementSearchError) ElementDescriptionException(ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException)

Example 12 with AutotestError

use of ru.sbtqa.tag.qautils.errors.AutotestError in project page-factory-2 by sbtqa.

the class HtmlFindUtils method getElementTypesMap.

/**
 * Specifies a type map: type attribute, type. Data is taken from the json file specified in the 'ui.types' parameter
 *
 * @return Returns a type map
 */
public Map<String, Class> getElementTypesMap() {
    Map<String, Class> elements = new HashMap<>();
    try {
        String types = PROPERTIES.geUiTypes();
        String dir = types.substring(0, types.lastIndexOf("/"));
        String name = types.substring(types.lastIndexOf("/")).replace(".json", "");
        TestDataProvider dataObject = new JsonDataProvider(dir, name);
        for (String typeAttribute : dataObject.getKeySet()) {
            elements.put(typeAttribute, Class.forName(dataObject.get(typeAttribute).getValue()));
        }
    } catch (DataException | ClassNotFoundException ex) {
        throw new AutotestError("Error while generating element search types.", ex);
    }
    return elements;
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) DataException(ru.sbtqa.tag.datajack.exceptions.DataException) TestDataProvider(ru.sbtqa.tag.datajack.TestDataProvider) HashMap(java.util.HashMap) JsonDataProvider(ru.sbtqa.tag.datajack.providers.json.JsonDataProvider)

Example 13 with AutotestError

use of ru.sbtqa.tag.qautils.errors.AutotestError in project page-factory-2 by sbtqa.

the class PlaceholderUtils method replaceJsonTemplatePlaceholders.

/**
 * Replace Json template placeholders in string on parameters
 *
 * @param jsonString replace placeholders in this json string
 * @param parameters replace these parameters
 * @return json string with replaced placeholders
 */
public static String replaceJsonTemplatePlaceholders(EndpointEntry entry, String jsonString, Map<String, Object> parameters) {
    Set<Map.Entry<String, Object>> mandatoryValues = parameters.entrySet().stream().filter(stringObjectEntry -> stringObjectEntry.getValue() != null).collect(Collectors.toSet());
    for (Map.Entry<String, Object> parameter : mandatoryValues) {
        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"));
            jsonString = replacePlaceholder(jsonString, declaredField, parameterName, parameterValue);
        } else {
            jsonString = replacePlaceholder(jsonString, null, parameterName, parameterValue);
        }
    }
    return jsonString;
}
Also used : Arrays(java.util.Arrays) ToNumberPolicy(com.google.gson.ToNumberPolicy) Iterator(java.util.Iterator) Set(java.util.Set) TypeToken(com.google.common.reflect.TypeToken) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) GsonBuilder(com.google.gson.GsonBuilder) String.format(java.lang.String.format) ArrayList(java.util.ArrayList) AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Map(java.util.Map) EndpointEntry(ru.sbtqa.tag.api.EndpointEntry) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) Pattern(java.util.regex.Pattern) AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) Field(java.lang.reflect.Field) EndpointEntry(ru.sbtqa.tag.api.EndpointEntry) Map(java.util.Map)

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