...78import org.apache.logging.log4j.LogManager;9import org.apache.logging.log4j.Logger;10import org.openqa.selenium.ElementNotVisibleException;11import org.openqa.selenium.ImeActivationFailedException;12import org.openqa.selenium.ImeNotAvailableException;13import org.openqa.selenium.InvalidCookieDomainException;14import org.openqa.selenium.InvalidElementStateException;15import org.openqa.selenium.InvalidSelectorException;16import org.openqa.selenium.NoAlertPresentException;17import org.openqa.selenium.NoSuchElementException;18import org.openqa.selenium.NoSuchFrameException;19import org.openqa.selenium.NoSuchWindowException;20import org.openqa.selenium.OutputType;21import org.openqa.selenium.StaleElementReferenceException;22import org.openqa.selenium.TakesScreenshot;23import org.openqa.selenium.TimeoutException;24import org.openqa.selenium.UnableToSetCookieException;25import org.openqa.selenium.WebDriver;26import org.openqa.selenium.WebDriverException;27import org.openqa.selenium.interactions.MoveTargetOutOfBoundsException;28import org.openqa.selenium.support.ui.UnexpectedTagNameException;2930import com.ibm.selmate.command.Command;31import com.ibm.selmate.command.SelmateScript;32import com.ibm.selmate.exception.SelmateExecutionException;33import com.ibm.selmate.exception.SelmateValidationException;34import com.ibm.selmate.report.ReportManager;35import com.ibm.selmate.report.VideoRecordingManager;36import com.ibm.selmate.util.CommandValidationErrorHandler;37import com.ibm.selmate.util.ExceptionTypes;38import com.ibm.selmate.util.MessageUtil;3940public final class SelmateScriptImpl implements SelmateScript {4142 static final Logger logger = LogManager.getLogger(SelmateScriptImpl.class);4344 private String name;4546 private List<Command> commands = new ArrayList<Command>();4748 public String getName() {49 return name;50 }5152 public void setName(String name) {53 this.name = name;54 }5556 public void addCommand(Command command) {57 this.commands.add(command);58 }5960 public Iterator<Command> iterateCommand() {61 return commands.iterator();62 }6364 /**65 * This operation validates all the commands.66 */67 public void validate() throws SelmateValidationException {68 logger.info("START");69 SelmateContextImpl selmateContextImpl = SelmateContextImpl.getCurrentContext();70 selmateContextImpl.resetStep();71 CommandValidationErrorHandler commandValidationErrorHandler = CommandValidationErrorHandler.getInstance();72 for (Command command : commands) {73 logger.info("Current Command being validated is :" + command.getClass().toString());74 selmateContextImpl.incrementCurrentStep();75 command.validate(commandValidationErrorHandler, SelmateContextAdapter.getCurrentContext());76 }77 if (commandValidationErrorHandler.isErrorPresent()) {78 throw new SelmateValidationException(commandValidationErrorHandler.getMessages());79 }80 logger.info("END");81 }8283 /**84 * This operation executes the entire script.85 */86 public Boolean execute(WebDriver driver) throws SelmateExecutionException {8788 logger.info("START");8990 ReportManager logManager = null;91 boolean status = false;92 VideoRecordingManager recordingMgr = null;93 try {9495 /*96 * Initialize the Log manager and Video recorder.97 */98 logManager = ReportManager.getInstance();99 SelmateContext selmateContext = SelmateContextAdapter.getCurrentContext();100 logManager.init(selmateContext);101 recordingMgr = VideoRecordingManager.getInstance();102 try {103 recordingMgr.setup();104 } catch (Exception e) {105 logger.fatal("ERROR while initializing video recording.", e);106 throw new SelmateExecutionException(e);107 }108109 /*110 * Reset the step count. Iterate and execute all the commands present. If any111 * command returns false or throws any Exception then the process will be112 * stopped and rest of the commands should be executed.113 */114 SelmateContextImpl selmateContextImpl = SelmateContextImpl.getCurrentContext();115 selmateContextImpl.resetStep();116 for (Command command : commands) {117 try {118 logger.info("Executing Command : " + command.getName());119 selmateContextImpl.incrementCurrentStep();120 status = command.execute(driver, SelmateContextAdapter.getCurrentContext());121 command.log(status, command.getStepDescription(), selmateContext);122123 // Introduce a delay between two consecutive commands.124 Thread.sleep(1000);125 if (!status) {126 break;127 }128 } catch (Exception ex) {129 while (ex.getCause() != null) {130 ex = (Exception) ex.getCause();131 }132 logger.fatal("Exception occured while executing " + command.getName() + " : " + ex);133 logError(command, driver, ex);134135 throw new SelmateExecutionException(ex);136 }137 status = false;138 }139 logger.info("End of execute method inside SelmateScriptImpl");140 } finally {141 /*142 * Release all resources.143 */144 if (logManager != null) {145 logManager.finish();146 }147 try {148 if (recordingMgr != null) {149 recordingMgr.Close();150 }151 } catch (Exception e) {152 logger.fatal("ERROR while closing video recording.", e);153 }154 }155156 logger.info("END");157158 return status;159160 }161162 /**163 * This operation logs error messages to the output report depending on the type164 * of exception encountered.165 * 166 * @param command167 * @param driver168 * @param ex169 */170 public void logError(Command command, WebDriver driver, Exception ex) {171172 logger.info("START");173174 MessageUtil messageUtil = MessageUtil.getInstance();175 if (ex instanceof ElementNotVisibleException) {176 addErrorLogInReport(command, driver, messageUtil.getMessage(ExceptionTypes.ELEMENT_NOT_VISIBLE_ERROR));177 } else if (ex instanceof ImeActivationFailedException) {178 addErrorLogInReport(command, driver, messageUtil.getMessage(ExceptionTypes.IME_ACTIVATION_FAILED_ERROR));179 } else if (ex instanceof ImeNotAvailableException) {180 addErrorLogInReport(command, driver, messageUtil.getMessage(ExceptionTypes.IME_NOT_SUPPORTED_ERROR));181 } else if (ex instanceof InvalidCookieDomainException) {182 addErrorLogInReport(command, driver, messageUtil.getMessage(ExceptionTypes.INVALID_COOKIE_DOMAIN_ERROR));183 } else if (ex instanceof InvalidElementStateException) {184 addErrorLogInReport(command, driver, ExceptionTypes.INVALID_ELEMENT_STATE_ERROR);185 } else if (ex instanceof InvalidSelectorException) {186 addErrorLogInReport(command, driver, messageUtil.getMessage(ExceptionTypes.INVALID_SELECTOR_ERROR));187 } else if (ex instanceof MoveTargetOutOfBoundsException) {188 addErrorLogInReport(command, driver, messageUtil.getMessage(ExceptionTypes.INVALID_TARGET_ERROR));189 } else if (ex instanceof NoAlertPresentException) {190 addErrorLogInReport(command, driver, messageUtil.getMessage(ExceptionTypes.NO_ALERT_PRESENT_ERROR));191 } else if (ex instanceof NoSuchElementException) {
...