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

com.github.siwenyan.si.SiCommandExecutorIf Maven / Gradle / Ivy

There is a newer version: 1.25.1.0
Show newest version
package com.github.siwenyan.si;

class SiCommandExecutorIf extends SiCommandExecutor {

    @Override
    public void execute(SiCommand command, SiContext siContext) throws SiVerifyException {
        String scriptRaw = command.getSiModelCommand().getTarget();
        String script = siContext.dynamic(scriptRaw, "'", "'");

        Boolean ifTrue = null;

        try {
            Object o = siContext.asJavascriptExecutor().executeScript("return " + script);
            if (null == o) {
                o = siContext.asJavascriptExecutor().executeScript(script);
                if (null == o) {
                    throw new RuntimeException("Browser: script evaluated as null: " + script);
                }
            }
            ifTrue = (Boolean) o;
        } catch (Exception e) {
            siContext.getReporter().reportMessage(e);
            siContext.getReporter().reportMessage("Browser: Fail to evaluate if script: " + script);
            try {
                SiScriptEngine sse = SiScriptEngine.instance();
                Object o = sse.executeScript(script);
                ifTrue = (Boolean) o;
                if (null == o) {
                    o = sse.executeScript("return " + script);
                    if (null == o) {
                        throw new RuntimeException("ScriptEngine: script evaluated as null: " + script);
                    }
                }
            } catch (Exception e1) {
                throw new RuntimeException(
                        "ScriptEngine: Fail to evaluate if script: " + script + " | " +
                                e1.getClass().getName() + " | " +
                                e1.getMessage());
            }
        }

        SiCommandIf realCommand = (SiCommandIf) command;
        realCommand.setIfTrue(ifTrue);

        siContext.getReporter().reportMessage("ifTrue: " + ifTrue);
        if (!ifTrue) {
            SiCommand elseOrEnd = command.getPairInBack();
            command.getSiTest().setCursor(elseOrEnd.getIndexInTest());
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy