Search in sources :

Example 61 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class ReplayAction method replay.

/**
 * Replay all actions annotated by ReplayOnError if the class is not a subclass of
 * HtmlElement
 * @param joinPoint
 * @throws Throwable
 */
@Around("!execution(public * com.seleniumtests.uipage.htmlelements.HtmlElement+.* (..))" + "&& execution(@com.seleniumtests.uipage.ReplayOnError public * * (..)) && @annotation(replay)")
public Object replay(ProceedingJoinPoint joinPoint, ReplayOnError replay) throws Throwable {
    int replayDelayMs = replay != null ? replay.replayDelayMs() : 100;
    Instant end = systemClock.instant().plusSeconds(SeleniumTestsContextManager.getThreadContext().getReplayTimeout());
    Object reply = null;
    String targetName = joinPoint.getTarget().toString();
    TestAction currentAction = null;
    if (joinPoint.getTarget() instanceof GenericPictureElement) {
        String methodName = joinPoint.getSignature().getName();
        List<String> pwdToReplace = new ArrayList<>();
        String actionName = String.format("%s on %s %s", methodName, targetName, LogAction.buildArgString(joinPoint, pwdToReplace, new HashMap<>()));
        currentAction = new TestAction(actionName, false, pwdToReplace);
        // order of steps is the right one (first called is first displayed)
        if (TestStepManager.getParentTestStep() != null) {
            TestStepManager.getParentTestStep().addAction(currentAction);
        }
    }
    boolean actionFailed = false;
    Throwable currentException = null;
    try {
        while (end.isAfter(systemClock.instant())) {
            // raised if action cannot be performed
            if (((CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false)).getBrowserInfo().getBrowser() == BrowserType.CHROME || ((CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false)).getBrowserInfo().getBrowser() == BrowserType.EDGE) {
                updateScrollFlagForElement(joinPoint, true, null);
            }
            try {
                reply = joinPoint.proceed(joinPoint.getArgs());
                WaitHelper.waitForMilliSeconds(200);
                break;
            // do not replay if error comes from scenario
            } catch (ScenarioException | ConfigurationException | DatasetException e) {
                throw e;
            } catch (MoveTargetOutOfBoundsException | InvalidElementStateException e) {
                updateScrollFlagForElement(joinPoint, null, e);
            } catch (Throwable e) {
                if (end.minusMillis(200).isAfter(systemClock.instant())) {
                    WaitHelper.waitForMilliSeconds(replayDelayMs);
                    continue;
                } else {
                    throw e;
                }
            }
        }
        return reply;
    } catch (Throwable e) {
        actionFailed = true;
        currentException = e;
        throw e;
    } finally {
        if (currentAction != null && TestStepManager.getParentTestStep() != null) {
            currentAction.setFailed(actionFailed);
            scenarioLogger.logActionError(currentException);
            if (joinPoint.getTarget() instanceof GenericPictureElement) {
                currentAction.setDurationToExclude(((GenericPictureElement) joinPoint.getTarget()).getActionDuration());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Instant(java.time.Instant) ArrayList(java.util.ArrayList) InvalidElementStateException(org.openqa.selenium.InvalidElementStateException) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) MoveTargetOutOfBoundsException(org.openqa.selenium.interactions.MoveTargetOutOfBoundsException) TestAction(com.seleniumtests.reporter.logger.TestAction) GenericPictureElement(com.seleniumtests.uipage.htmlelements.GenericPictureElement) DatasetException(com.seleniumtests.customexception.DatasetException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) ScenarioException(com.seleniumtests.customexception.ScenarioException) Around(org.aspectj.lang.annotation.Around)

Example 62 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class Element method checkForMobile.

/**
 * Check if the current platform is a mobile platform
 * if it's the case, search for the element, else, raise a ScenarioException
 */
protected PerformsTouchActions checkForMobile() {
    CustomEventFiringWebDriver driver = (CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false);
    if (driver == null) {
        throw new ScenarioException("Driver has not already been created");
    }
    if (!SeleniumTestsContextManager.isMobileTest()) {
        throw new ScenarioException("action is available only for mobile platforms");
    }
    if (!(driver.getWebDriver() instanceof AppiumDriver<?>)) {
        throw new ScenarioException("action is available only for mobile platforms");
    }
    findElement(true);
    return (PerformsTouchActions) driver.getWebDriver();
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) AppiumDriver(io.appium.java_client.AppiumDriver) ScenarioException(com.seleniumtests.customexception.ScenarioException) PerformsTouchActions(io.appium.java_client.PerformsTouchActions)

Example 63 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class ExcelHelper method readSheet.

/**
 * Read a sheet by name in the excel file
 * @param fis
 * @param sheetName
 * @return
 * @throws IOException
 */
public List<Map<String, String>> readSheet(InputStream fis, String sheetName, boolean headerPresent) throws IOException {
    try (Workbook workbook = WorkbookFactory.create(fis)) {
        FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
        DataFormatter dataFormatter = new DataFormatter();
        Sheet sheet = workbook.getSheet(sheetName);
        if (sheet == null) {
            throw new ScenarioException(String.format("Sheet %s does not exist", sheetName));
        }
        List<Map<String, String>> sheetContent = readSheet(sheet, headerPresent, formulaEvaluator, dataFormatter);
        if (sheetContent == null) {
            throw new ScenarioException(String.format("No data in sheet %s", sheetName));
        }
        return sheetContent;
    } catch (IOException e) {
        throw new ScenarioException(e.getMessage());
    }
}
Also used : IOException(java.io.IOException) Sheet(org.apache.poi.ss.usermodel.Sheet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Workbook(org.apache.poi.ss.usermodel.Workbook) ScenarioException(com.seleniumtests.customexception.ScenarioException) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Example 64 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class ExcelHelper method read.

/**
 * Read an excel file and returns the content
 * @param fis
 * @param headerPresent
 * @return
 * @throws IOException
 */
public Map<String, List<Map<String, String>>> read(InputStream fis, boolean headerPresent) throws IOException {
    Map<String, List<Map<String, String>>> content = new LinkedHashMap<>();
    try (Workbook workbook = WorkbookFactory.create(fis)) {
        FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
        DataFormatter dataFormatter = new DataFormatter();
        for (int sheetIdx = workbook.getNumberOfSheets() - 1; sheetIdx >= 0; sheetIdx--) {
            Sheet sheet = workbook.getSheetAt(sheetIdx);
            List<Map<String, String>> sheetContent = readSheet(sheet, headerPresent, formulaEvaluator, dataFormatter);
            if (sheetContent == null) {
                continue;
            }
            content.put(sheet.getSheetName(), sheetContent);
        }
        return content;
    } catch (IOException e) {
        throw new ScenarioException(e.getMessage());
    }
}
Also used : IOException(java.io.IOException) Workbook(org.apache.poi.ss.usermodel.Workbook) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) Sheet(org.apache.poi.ss.usermodel.Sheet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ScenarioException(com.seleniumtests.customexception.ScenarioException) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Example 65 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class SelectList method findElement.

@Override
protected void findElement() {
    // set a default type so that use of selectImplementation can be done
    selectImplementation = new StubSelect();
    super.findElement(true);
    // if we have a prefered implementation, use it
    // search the right select list handler
    implementationList = orderImplementations(getPreferedUiLibraries());
    for (Class<? extends ISelectList> selectClass : implementationList) {
        try {
            ISelectList selectInstance = selectClass.getConstructor(WebElement.class, FrameElement.class).newInstance(getRealElementNoSearch(), frameElement);
            if (selectInstance.isApplicable()) {
                selectImplementation = selectInstance;
                selectInstance.setDriver(getDriver());
                break;
            }
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
            logger.error(String.format("Cannot use Select implementation %s: %s", selectClass.getName(), e.getMessage()));
        }
    }
    if (selectImplementation instanceof StubSelect) {
        throw new ScenarioException("Cannot find type of select " + getRealElementNoSearch().getTagName());
    }
    options = selectImplementation.getOptions();
    multiple = selectImplementation.isMultipleWithoutFind();
}
Also used : StubSelect(com.seleniumtests.uipage.htmlelements.select.StubSelect) WebElement(org.openqa.selenium.WebElement) InvocationTargetException(java.lang.reflect.InvocationTargetException) ISelectList(com.seleniumtests.uipage.htmlelements.select.ISelectList) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Aggregations

ScenarioException (com.seleniumtests.customexception.ScenarioException)68 ArrayList (java.util.ArrayList)17 WebElement (org.openqa.selenium.WebElement)14 UnirestException (kong.unirest.UnirestException)13 GenericPictureElement (com.seleniumtests.uipage.htmlelements.GenericPictureElement)12 IOException (java.io.IOException)12 JSONObject (kong.unirest.json.JSONObject)12 CheckBoxElement (com.seleniumtests.uipage.htmlelements.CheckBoxElement)11 Element (com.seleniumtests.uipage.htmlelements.Element)11 HtmlElement (com.seleniumtests.uipage.htmlelements.HtmlElement)11 LinkElement (com.seleniumtests.uipage.htmlelements.LinkElement)11 RadioButtonElement (com.seleniumtests.uipage.htmlelements.RadioButtonElement)11 File (java.io.File)10 List (java.util.List)9 HashMap (java.util.HashMap)8 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)7 AWTException (java.awt.AWTException)7 Robot (java.awt.Robot)6 Map (java.util.Map)5 TestStep (com.seleniumtests.reporter.logger.TestStep)4