Best Cerberus-source code snippet using org.cerberus.service.sikuli.ISikuliService.doSikuliAction
Source:SikuliService.java
...146 }147 return true;148 }149 @Override150 public AnswerItem<JSONObject> doSikuliAction(Session session, String action, String locator, String text) {151 AnswerItem<JSONObject> answer = new AnswerItem<JSONObject>();152 MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS);153 HttpURLConnection connection = null;154 BufferedReader in = null;155 PrintStream os = null;156 URL url;157 String urlToConnect = "http://" + session.getHost() + ":" + session.getPort() + "/extra/ExecuteSikuliAction";158 try {159 /**160 * Connect to ExecuteSikuliAction Servlet Through SeleniumServer161 */162 url = new URL(urlToConnect);163 connection = (HttpURLConnection) url.openConnection();164 connection.setRequestMethod("POST");165 connection.setRequestProperty("User-Agent", "Mozilla/5.0");166 connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");167 JSONObject postParameters = generatePostParameters(action, locator, text, session.getCerberus_selenium_wait_element());168 connection.setDoOutput(true);169 // Send post request170 os = new PrintStream(connection.getOutputStream());171 os.println(postParameters.toString());172 os.println("|ENDS|");173 if (connection == null || connection.getResponseCode() != 200) {174 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_SERVER_NOT_REACHABLE);175 }176 in = new BufferedReader(177 new InputStreamReader(connection.getInputStream()));178 String inputLine;179 StringBuilder response = new StringBuilder();180 /**181 * Wait here until receiving |ENDR| String182 */183 while (!(inputLine = in.readLine()).equals("|ENDR|")) {184 response.append(inputLine);185 }186 /**187 * Convert received string into JSONObject188 *189 */190 JSONObject objReceived = new JSONObject(response.toString());191 answer.setItem(objReceived);192 if (objReceived.has("status")) {193 if ("Failed".equals(objReceived.getString("status"))) {194 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);195 } else {196 msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS);197 }198 } else {199 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);200 }201 in.close();202 } catch (MalformedURLException ex) {203 LOG.warn(ex);204 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_SERVER_BADURL);205 mes.setDescription(mes.getDescription().replace("%URL%", urlToConnect));206 msg = mes;207 } catch (FileNotFoundException ex) {208 LOG.warn(ex);209 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_FILE_NOT_FOUND);210 mes.setDescription(mes.getDescription().replace("%FILE%", locator));211 msg = mes;212 } catch (IOException ex) {213 LOG.warn(ex);214 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);215 } catch (JSONException ex) {216 LOG.warn(ex);217 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);218 } catch (MimeTypeException ex) {219 LOG.warn(ex);220 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);221 } catch (Exception ex) {222 LOG.warn(ex);223 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_SERVER_NOT_REACHABLE);224 mes.setDescription(mes.getDescription().replace("%URL%", urlToConnect));225 msg = mes;226 } finally {227 if (os != null) {228 os.close();229 }230 if (connection != null) {231 connection.disconnect();232 }233 }234 answer.setResultMessage(msg);235 return answer;236 }237 @Override238 public MessageEvent doSikuliActionOpenApp(Session session, String appName) {239 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_OPENAPP, null, appName);240 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {241 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_OPENAPP);242 message.setDescription(message.getDescription().replace("%APP%", appName));243 return message;244 }245 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {246 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_OPENAPP);247 mes.setDescription(mes.getDescription().replace("%STRING1%", appName) + " - " + actionResult.getMessageDescription());248 return mes;249 }250 return actionResult.getResultMessage();251 }252 @Override253 public MessageEvent doSikuliActionCloseApp(Session session, String appName) {254 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_CLOSEAPP, null, appName);255 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {256 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLOSEAPP);257 message.setDescription(message.getDescription().replace("%APP%", appName));258 return message;259 }260 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {261 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_CLOSEAPP);262 mes.setDescription(mes.getDescription().replace("%STRING1%", appName) + " - " + actionResult.getMessageDescription());263 return mes;264 }265 return actionResult.getResultMessage();266 }267 @Override268 public MessageEvent doSikuliActionClick(Session session, String locator, String text) {269 AnswerItem<JSONObject> actionResult = null;270 if (!locator.equals("")) {271 actionResult = doSikuliAction(session, this.SIKULI_CLICK, locator, "");272 } else {273 actionResult = doSikuliAction(session, this.SIKULI_CLICK, null, text);274 }275 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {276 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLICK);277 message.setDescription(message.getDescription().replace("%ELEMENT%", locator));278 return message;279 }280 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {281 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_CLICK_NO_SUCH_ELEMENT);282 mes.setDescription(mes.getDescription().replace("%ELEMENT%", locator) + " - " + actionResult.getMessageDescription());283 return mes;284 }285 return actionResult.getResultMessage();286 }287 @Override288 public MessageEvent doSikuliActionRightClick(Session session, String locator, String text) {289 AnswerItem<JSONObject> actionResult = null;290 if (!locator.equals("")) {291 actionResult = doSikuliAction(session, this.SIKULI_RIGHTCLICK, locator, "");292 } else {293 actionResult = doSikuliAction(session, this.SIKULI_RIGHTCLICK, null, text);294 }295 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {296 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_RIGHTCLICK);297 message.setDescription(message.getDescription().replace("%ELEMENT%", locator));298 return message;299 }300 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {301 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_RIGHTCLICK_NO_SUCH_ELEMENT);302 mes.setDescription(mes.getDescription().replace("%ELEMENT%", locator) + " - " + actionResult.getMessageDescription());303 return mes;304 }305 return actionResult.getResultMessage();306 }307 @Override308 public MessageEvent doSikuliActionSwitchApp(Session session, String locator) {309 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_SWITCHAPP, locator, "");310 return actionResult.getResultMessage();311 }312 @Override313 public MessageEvent doSikuliActionDoubleClick(Session session, String locator, String text) {314 AnswerItem<JSONObject> actionResult = null;315 if (!locator.equals("")) {316 actionResult = doSikuliAction(session, this.SIKULI_DOUBLECLICK, locator, "");317 } else {318 actionResult = doSikuliAction(session, this.SIKULI_DOUBLECLICK, null, text);319 }320 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {321 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_DOUBLECLICK);322 message.setDescription(message.getDescription().replace("%ELEMENT%", locator));323 return message;324 }325 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {326 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_DOUBLECLICK_NO_SUCH_ELEMENT);327 mes.setDescription(mes.getDescription().replace("%ELEMENT%", locator) + " - " + actionResult.getMessageDescription());328 return mes;329 }330 return actionResult.getResultMessage();331 }332 @Override333 public MessageEvent doSikuliActionType(Session session, String locator, String text) {334 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_TYPE, locator, text);335 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {336 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_TYPE);337 message.setDescription(message.getDescription().replace("%ELEMENT%", locator));338 message.setDescription(message.getDescription().replace("%DATA%", text));339 return message;340 }341 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {342 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_TYPE_NO_SUCH_ELEMENT);343 mes.setDescription(mes.getDescription().replace("%ELEMENT%", locator) + " - " + actionResult.getMessageDescription());344 return mes;345 }346 return actionResult.getResultMessage();347 }348 @Override349 public MessageEvent doSikuliActionMouseOver(Session session, String locator, String text) {350 AnswerItem<JSONObject> actionResult = null;351 if (!locator.equals("")) {352 actionResult = doSikuliAction(session, this.SIKULI_MOUSEOVER, locator, "");353 } else {354 actionResult = doSikuliAction(session, this.SIKULI_MOUSEOVER, null, text);355 }356 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {357 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_MOUSEOVER);358 message.setDescription(message.getDescription().replace("%ELEMENT%", locator));359 return message;360 }361 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {362 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_MOUSEOVER_NO_SUCH_ELEMENT);363 mes.setDescription(mes.getDescription().replace("%ELEMENT%", locator) + " - " + actionResult.getMessageDescription());364 return mes;365 }366 return actionResult.getResultMessage();367 }368 @Override369 public MessageEvent doSikuliActionWait(Session session, String locator, String text) {370 AnswerItem<JSONObject> actionResult = null;371 if (!locator.equals("")) {372 actionResult = doSikuliAction(session, this.SIKULI_WAIT, locator, "");373 } else {374 actionResult = doSikuliAction(session, this.SIKULI_WAIT, null, text);375 }376 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {377 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT);378 message.setDescription(message.getDescription().replace("%ELEMENT%", locator));379 return message;380 }381 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {382 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT);383 mes.setDescription(mes.getDescription().replace("%ELEMENT%", locator) + " - " + actionResult.getMessageDescription());384 return mes;385 }386 return actionResult.getResultMessage();387 }388 @Override389 public MessageEvent doSikuliActionWaitVanish(Session session, String locator, String text) {390 AnswerItem<JSONObject> actionResult = null;391 if (!locator.equals("")) {392 actionResult = doSikuliAction(session, this.SIKULI_WAITVANISH, locator, "");393 } else {394 actionResult = doSikuliAction(session, this.SIKULI_WAITVANISH, null, text);395 }396 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {397 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_WAITVANISH_ELEMENT);398 message.setDescription(message.getDescription().replace("%ELEMENT%", locator));399 return message;400 }401 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {402 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_RIGHTCLICK_NO_SUCH_ELEMENT);403 mes.setDescription(mes.getDescription().replace("%ELEMENT%", locator) + " - " + actionResult.getMessageDescription());404 return mes;405 }406 return actionResult.getResultMessage();407 }408 @Override409 public MessageEvent doSikuliActionKeyPress(Session session, String locator, String text) {410 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_KEYPRESS, locator, text);411 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {412 MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_KEYPRESS);413 message.setDescription(message.getDescription().replace("%ELEMENT%", locator));414 message.setDescription(message.getDescription().replace("%DATA%", text));415 return message;416 }417 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {418 MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_KEYPRESS_OTHER);419 mes.setDescription(mes.getDescription().replace("%KEY%", text).replace("%REASON%", actionResult.getMessageDescription()));420 return mes;421 }422 return actionResult.getResultMessage();423 }424 @Override425 public MessageEvent doSikuliVerifyElementPresent(Session session, String locator) {426 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_VERIFYELEMENTPRESENT, locator, "");427 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {428 MessageEvent message = new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_PRESENT);429 message.setDescription(message.getDescription().replace("%STRING1%", locator));430 return message;431 }432 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {433 MessageEvent mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_PRESENT);434 mes.setDescription(mes.getDescription().replace("%STRING1%", locator) + " - " + actionResult.getMessageDescription());435 return mes;436 }437 return actionResult.getResultMessage();438 }439 @Override440 public MessageEvent doSikuliVerifyElementNotPresent(Session session, String locator) {441 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_VERIFYELEMENTNOTPRESENT, locator, "");442 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {443 MessageEvent message = new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_NOTPRESENT);444 message.setDescription(message.getDescription().replace("%STRING1%", locator));445 return message;446 }447 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {448 MessageEvent mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_NOTPRESENT);449 mes.setDescription(mes.getDescription().replace("%STRING1%", locator) + " - " + actionResult.getMessageDescription());450 return mes;451 }452 return actionResult.getResultMessage();453 }454 @Override455 public MessageEvent doSikuliVerifyTextInPage(Session session, String text) {456 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_VERIFYTEXTINPAGE, null, text);457 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_SUCCESS).getCodeString())) {458 MessageEvent message = new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_TEXTINPAGE);459 message.setDescription(message.getDescription().replace("%STRING1%", text));460 return message;461 }462 if (actionResult.getResultMessage().getCodeString().equals(new MessageEvent(MessageEventEnum.ACTION_FAILED).getCodeString())) {463 MessageEvent mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_TEXTINPAGE);464 mes.setDescription(mes.getDescription().replace("%STRING1%", text) + " - " + actionResult.getMessageDescription());465 return mes;466 }467 return actionResult.getResultMessage();468 }469 @Override470 public File takeScreenShotFile(Session session) {471 File image = null;472 long timeout = System.currentTimeMillis() + (session.getCerberus_selenium_wait_element());473 try {474 AnswerItem<JSONObject> actionResult = doSikuliAction(session, this.SIKULI_CAPTURE, null, "");475 String screenshotInBase64 = ((JSONObject) actionResult.getItem()).getString("screenshot");476 byte[] data = Base64.decodeBase64(screenshotInBase64);477 image = new File("temp.png");478 FileUtils.writeByteArrayToFile(image, data);479 if (image != null) {480 //logs for debug purposes481 LOG.info("screen-shot taken with succes: " + image.getName() + "(size" + image.length() + ")");482 } else {483 LOG.warn("screen-shot returned null: ");484 }485 } catch (JSONException ex) {486 LOG.warn(ex);487 } catch (IOException ex) {488 LOG.warn(ex);...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!