Best Cerberus-source code snippet using org.cerberus.engine.execution.impl.RecorderService.recordPageSource
Source:RecorderService.java
...138 * Only if the return code is not equal to Cancel, meaning lost139 * connectivity with selenium.140 */141 if (!returnCode.equals("CA")) {142 objectFile = this.recordPageSource(myExecution, testCaseStepActionExecution, controlNumber);143 if (objectFile != null) {144 objectFileList.add(objectFile);145 }146 } else {147 LOG.debug(logPrefix + "Not Doing screenshot because connectivity with selenium server lost.");148 }149 }150 } else {151 LOG.debug(logPrefix + "Not getting page source because of the pageSource parameter or flag on the last Action result.");152 }153 /**154 * Last call XML SOURCE management. Get Source of the XML if requested155 * by the last Action or control MessageEvent.156 *157 */158 if (applicationType.equals(Application.TYPE_SRV)159 && ((myExecution.getPageSource() == 2) || ((myExecution.getPageSource() == 1) && (getPageSource))160 || (myExecution.getScreenshot() == 2) || ((myExecution.getScreenshot() == 1) && (doScreenshot)))) {161 //Record the Request and Response.162 AppService se = (AppService) testCaseStepActionExecution.getTestCaseStepExecution().gettCExecution().getLastServiceCalled();163 if (se != null) { // No Calls were performed previously164 List<TestCaseExecutionFile> objectFileSOAPList = new ArrayList<TestCaseExecutionFile>();165 objectFileSOAPList = this.recordServiceCall(myExecution, testCaseStepActionExecution, controlNumber, null, se);166 if (objectFileSOAPList.isEmpty() != true) {167 for (TestCaseExecutionFile testCaseExecutionFile : objectFileSOAPList) {168 objectFileList.add(testCaseExecutionFile);169 }170 }171 }172 }173 return objectFileList;174 }175 @Override176 public AnswerItem recordManuallyFile(TestCaseStepActionExecution testCaseStepActionExecution, TestCaseStepActionControlExecution testCaseStepActionControlExecution, String extension, String desc, FileItem file, Integer id, String fileName, Integer fileID) {177 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",178 "Can't upload file");179 AnswerItem a = new AnswerItem();180 TestCaseExecutionFile object = null;181 String returnCode;182 Integer controlNumber = 0;183 String test = "";184 String testCase = "";185 String step = "";186 String index = "";187 String sequence = "";188 String controlString = "";189 Integer myExecution = id;190 if (testCaseStepActionControlExecution == null) {191 test = testCaseStepActionExecution.getTest();192 testCase = testCaseStepActionExecution.getTestCase();193 step = String.valueOf(testCaseStepActionExecution.getStep());194 index = String.valueOf(testCaseStepActionExecution.getIndex());195 sequence = String.valueOf(testCaseStepActionExecution.getSequence());196 controlString = controlNumber.equals(0) ? null : String.valueOf(controlNumber);197 returnCode = testCaseStepActionExecution.getReturnCode();198 } else {199 returnCode = testCaseStepActionControlExecution.getReturnCode();200 controlNumber = testCaseStepActionControlExecution.getControlSequence();201 test = testCaseStepActionControlExecution.getTest();202 testCase = testCaseStepActionControlExecution.getTestCase();203 step = String.valueOf(testCaseStepActionControlExecution.getStep());204 index = String.valueOf(testCaseStepActionControlExecution.getIndex());205 sequence = String.valueOf(testCaseStepActionControlExecution.getSequence());206 controlString = controlNumber.equals(0) ? null : String.valueOf(controlNumber);207 }208 // Used for logging purposes209 String logPrefix = Infos.getInstance().getProjectNameAndVersion() + " - [" + test + " - " + testCase + " - step: " + step + " action: " + sequence + "] ";210 try {211 Recorder recorder = new Recorder();212 String name = "";213 File dir = null;214 if (file != null) {215 name = file.getName();216 extension = name.substring(name.lastIndexOf('.') + 1, name.length());217 extension = extension.toUpperCase();218 extension = testCaseExecutionFileService.checkExtension(name, extension);219 recorder = this.initFilenames(myExecution, test, testCase, step, index, sequence, controlString, null, 0, name.substring(0, name.lastIndexOf('.')), extension, true);220 dir = new File(recorder.getFullPath());221 } else {222 name = fileName;223 extension = testCaseExecutionFileService.checkExtension(name, extension);224 if (name.contains(".")) {225 recorder = this.initFilenames(myExecution, test, testCase, step, index, sequence, controlString, null, 0, name.substring(0, name.lastIndexOf('.')), extension, true);226 dir = new File(recorder.getFullPath());227 } else {228 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);229 msg.setDescription(msg.getDescription().replace("%ITEM%", "manual testcase execution file")230 .replace("%OPERATION%", "Create")231 .replace("%REASON%", "file is missing!"));232 a.setResultMessage(msg);233 return a;234 }235 }236 if (!dir.exists()) {237 try {238 boolean isCreated = dir.mkdirs();239 if (!isCreated) {240 throw new SecurityException();241 }242 } catch (SecurityException se) {243 LOG.warn("Unable to create manual execution file dir: " + se.getMessage());244 msg = new MessageEvent(MessageEventEnum.FILE_ERROR).resolveDescription("DESCRIPTION",245 se.toString()).resolveDescription("MORE", "Please check the parameter cerberus_exemanualmedia_path");246 a.setResultMessage(msg);247 return a;248 }249 }250 if (file != null) {251 AnswerItem<TestCaseExecutionFile> current = testCaseExecutionFileService.readByKey(myExecution, recorder.getLevel(), desc);252 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);253 if (current.getItem() != null) {254 try {255 File temp = new File(recorder.getRootFolder() + current.getItem().getFileName());256 temp.delete();257 } catch (SecurityException se) {258 LOG.warn("Unable to create manual execution file dir: " + se.getMessage());259 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",260 se.toString());261 }262 }263 try {264 file.write(new File(recorder.getFullFilename()));265 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("DESCRIPTION",266 "Manual Execution File uploaded");267 msg.setDescription(msg.getDescription().replace("%ITEM%", "Manual Execution File").replace("%OPERATION%", "Upload"));268 LOG.debug(logPrefix + "Copy file finished with success - source: " + file.getName() + " destination: " + recorder.getRelativeFilenameURL());269 object = testCaseExecutionFileFactory.create(fileID, myExecution, recorder.getLevel(), desc, recorder.getRelativeFilenameURL(), extension, "", null, "", null);270 } catch (Exception e) {271 LOG.warn("Unable to upload Manual Execution File: " + e.getMessage());272 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",273 e.toString());274 }275 } else {276 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("DESCRIPTION",277 "Manual Execution File updated");278 msg.setDescription(msg.getDescription().replace("%ITEM%", "Manual Execution File").replace("%OPERATION%", "updated"));279 LOG.debug(logPrefix + "Updated test case manual file finished with success");280 object = testCaseExecutionFileFactory.create(fileID, myExecution, recorder.getLevel(), desc, name, extension, "", null, "", null);281 }282 testCaseExecutionFileService.saveManual(object);283 } catch (CerberusException e) {284 LOG.error(logPrefix + e.toString());285 }286 a.setResultMessage(msg);287 a.setItem(object);288 return a;289 }290 @Override291 public TestCaseExecutionFile recordScreenshot(TestCaseExecution testCaseExecution, TestCaseStepActionExecution testCaseStepActionExecution, Integer control) {292 TestCaseExecutionFile object = null;293 String test = testCaseStepActionExecution.getTest();294 String testCase = testCaseStepActionExecution.getTestCase();295 String step = String.valueOf(testCaseStepActionExecution.getStep());296 String index = String.valueOf(testCaseStepActionExecution.getIndex());297 String sequence = String.valueOf(testCaseStepActionExecution.getSequence());298 String controlString = control.equals(0) ? null : String.valueOf(control);299 long runId = testCaseExecution.getId();300 String applicationType = testCaseExecution.getApplicationObj().getType();301 // Used for logging purposes302 String logPrefix = Infos.getInstance().getProjectNameAndVersion() + " - [" + test + " - " + testCase + " - step: " + step + " action: " + sequence + "] ";303 LOG.debug(logPrefix + "Doing screenshot.");304 /**305 * Take Screenshot and write it306 */307 File newImage = null;308 if (applicationType.equals(Application.TYPE_GUI)309 || applicationType.equals(Application.TYPE_APK)310 || applicationType.equals(Application.TYPE_IPA)) {311 newImage = this.webdriverService.takeScreenShotFile(testCaseExecution.getSession());312 } else if (applicationType.equals(Application.TYPE_FAT)) {313 newImage = this.sikuliService.takeScreenShotFile(testCaseExecution.getSession());314 }315 if (newImage != null) {316 try {317 Recorder recorder = this.initFilenames(runId, test, testCase, step, index, sequence, controlString, null, 0, "screenshot", "png", false);318 LOG.debug(logPrefix + "FullPath " + recorder.getFullPath());319 File dir = new File(recorder.getFullPath());320 if (!dir.exists()) {321 LOG.debug(logPrefix + "Create directory for execution " + recorder.getFullPath());322 dir.mkdirs();323 }324 // Getting the max size of the screenshot.325 long maxSizeParam = parameterService.getParameterIntegerByKey("cerberus_screenshot_max_size", "", 1048576);326 if (maxSizeParam < newImage.length()) {327 LOG.warn(logPrefix + "Screen-shot size exceeds the maximum defined in configurations " + newImage.getName() + " destination: " + recorder.getRelativeFilenameURL());328 }329 //copies the temp file to the execution file330 FileUtils.copyFile(newImage, new File(recorder.getFullFilename()));331 LOG.debug(logPrefix + "Copy file finished with success - source: " + newImage.getName() + " destination: " + recorder.getRelativeFilenameURL());332 // Index file created to database.333 object = testCaseExecutionFileFactory.create(0, testCaseExecution.getId(), recorder.getLevel(), "Screenshot", recorder.getRelativeFilenameURL(), "PNG", "", null, "", null);334 testCaseExecutionFileService.save(object);335 //deletes the temporary file336 FileUtils.forceDelete(newImage);337 LOG.debug(logPrefix + "Temp file deleted with success " + newImage.getName());338 LOG.debug(logPrefix + "Screenshot done in : " + recorder.getRelativeFilenameURL());339 } catch (IOException ex) {340 LOG.error(logPrefix + ex.toString());341 } catch (CerberusException ex) {342 LOG.error(logPrefix + ex.toString());343 }344 } else {345 LOG.warn(logPrefix + "Screenshot returned null.");346 }347 return object;348 }349 @Override350 public TestCaseExecutionFile recordPageSource(TestCaseExecution testCaseExecution, TestCaseStepActionExecution testCaseStepActionExecution, Integer control) {351 // Used for logging purposes352 String logPrefix = Infos.getInstance().getProjectNameAndVersion() + " - ";353 LOG.debug(logPrefix + "Starting to save Page Source File.");354 TestCaseExecutionFile object = null;355 String test = testCaseExecution.getTest();356 String testCase = testCaseExecution.getTestCase();357 String step = String.valueOf(testCaseStepActionExecution.getStep());358 String index = String.valueOf(testCaseStepActionExecution.getIndex());359 String sequence = String.valueOf(testCaseStepActionExecution.getSequence());360 String controlString = control.equals(0) ? null : String.valueOf(control);361 try {362 Recorder recorder = this.initFilenames(testCaseStepActionExecution.getTestCaseStepExecution().gettCExecution().getId(), test, testCase, step, index, sequence, controlString, null, 0, "pagesource", "html", false);363 File dir = new File(recorder.getFullPath());364 dir.mkdirs();...
recordPageSource
Using AI Code Generation
1import org.cerberus.engine.execution.impl.RecorderService;2import org.cerberus.util.StringUtil;3import org.cerberus.util.answer.AnswerItem;4import org.cerberus.util.answer.AnswerUtil;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.interactions.Actions;8import org.openqa.selenium.support.ui.Select;9import java.util.List;10import java.util.logging.Level;11import java.util.logging.Logger;12public class RecorderServiceCustom extends RecorderService {13 public RecorderServiceCustom(WebDriver driver) {14 super(driver);15 }16 public AnswerItem<String> recordPageSource() {17 AnswerItem<String> answer = new AnswerItem<>();18 String pageSource = driver.getPageSource();19 if (StringUtil.isNullOrEmpty(pageSource)) {20 answer.setResultMessage("Page source is null or empty");21 answer.setResultMessage("KO");22 } else {23 answer.setItem(pageSource);24 answer.setResultMessage("OK");25 }26 return answer;27 }28}29import org.cerberus.engine.execution.impl.RecorderServiceCustom;30import org.cerberus.engine.execution.impl.SeleniumService;31import org.cerberus.engine.execution.impl.SeleniumServiceCustom;32import org.cerberus.engine.execution.impl.WebDriverService;33import org.cerberus.engine.execution.impl.WebDriverServiceCustom;34import org.cerberus.engine.execution.impl.WebElementService;35import org.cerberus.engine.execution.impl.WebElementServiceCustom;36import org.cerberus.engine.execution.impl.action.ActionService;37import org.cerberus.engine.execution.impl.action.ActionServiceCustom;38import org.cerberus.engine.execution.impl.action.AssertService;39import org.cerberus.engine.execution.impl.action.AssertServiceCustom;40import org.cerberus.engine.execution.impl.action.ClickService;41import org.cerberus.engine.execution.impl.action.ClickServiceCustom;42import org.cerberus.engine.execution.impl.action.ControlService;43import org.cerberus.engine.execution.impl.action.ControlServiceCustom;44import org.cerberus.engine.execution.impl.action.DoService;45import org.cerberus.engine.execution.impl.action.DoServiceCustom;46import org.cerberus.engine.execution.impl.action.DragAndDropService;47import org.cerberus.engine.execution.impl.action.DragAndDropServiceCustom;48import org.cerberus.engine.execution.impl.action.ExecuteService;49import
recordPageSource
Using AI Code Generation
1import org.cerberus.engine.execution.impl.RecorderService2import org.cerberus.engine.execution.impl.TestService3import org.cerberus.engine.execution.impl.TestCaseService4import org.cerberus.engine.execution.impl.TestCaseExecutionService5import org.cerberus.engine.execution.impl.TestStepService6def testCaseExecutionService = new TestCaseExecutionService()7def testCaseService = new TestCaseService()8def testService = new TestService()9def testStepService = new TestStepService()10def recorderService = new RecorderService()11def testCaseExecution = testCaseExecutionService.findTestCaseExecutionByKey(executionId)12def testCase = testCaseService.findTestCaseByKey(testCaseExecution.test, testCaseExecution.testcase)13def test = testService.findTestByKey(testCaseExecution.test)14def testStep = testStepService.findTestStep(test, testCase, testStepId)15recorderService.recordPageSource(testCaseExecution, testStep, testCase)16import org.cerberus.engine.execution.impl.RecorderService17import org.cerberus.engine.execution.impl.TestService18import org.cerberus.engine.execution.impl.TestCaseService19import org.cerberus.engine.execution.impl.TestCaseExecutionService20import org.cerberus.engine.execution.impl.TestStepService21def testCaseExecutionService = new TestCaseExecutionService()22def testCaseService = new TestCaseService()23def testService = new TestService()24def testStepService = new TestStepService()25def recorderService = new RecorderService()26def testCaseExecution = testCaseExecutionService.findTestCaseExecutionByKey(executionId)27def testCase = testCaseService.findTestCaseByKey(testCaseExecution.test, testCaseExecution.testcase)28def test = testService.findTestByKey(testCaseExecution.test)29def testStep = testStepService.findTestStep(test, testCase, testStepId)30recorderService.recordPageSource(testCaseExecution, testSte
recordPageSource
Using AI Code Generation
1if (action.getTest().equalsIgnoreCase("RECORDPAGESOURCE")) {2 try {3 recorderService.recordPageSource(action);4 } catch (CerberusEventException ex) {5 Logger.getLogger(RunTestCaseService.class.getName()).log(Level.SEVERE, null, ex);6 }7}
recordPageSource
Using AI Code Generation
1import java.io.File2import java.nio.file.Files3def recorder = cerberusApp.getRecorderService()4def pageSource = recorder.recordPageSource()5File file = new File("pageSource.html")6file.write(pageSource)7System.out.println("Page source saved in file: " + file.getAbsolutePath())8System.out.println("File content: " + content)
recordPageSource
Using AI Code Generation
1package org.cerberus.engine.execution.impl;2import java.io.File;3import java.io.IOException;4import java.nio.file.Files;5import java.nio.file.Paths;6import java.nio.file.StandardOpenOption;7import org.apache.logging.log4j.LogManager;8import org.apache.logging.log4j.Logger;9import org.cerberus.engine.entity.MessageEvent;10import org.cerberus.engine.entity.MessageGeneral;11import org.cerberus.engine.entity.Session;12import org.cerberus.engine.execution.IRecorderService;13import org.cerberus.engine.execution.IRecorderService.RecorderServiceException;14import org.cerberus.engine.execution.IRecorderService.RecorderServiceExceptionCode;15import org.cerberus.engine.execution.impl.handler.SeleniumActionHandler;16import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory;17import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.Action;18import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.RecorderHandler;19import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.Selector;20import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.SelectorType;21import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.SelectorUtil;22import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.Service;23import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.TestCaseStepActionControlExecution;24import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.TestCaseStepActionExecution;25import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.TestCaseStepExecution;26import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.TestCaseStepExecutionControlExecution;27import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.TestCaseStepExecutionControlExecutionControlExecution;28import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.TestCaseStepExecutionControlExecutionControlExecutionControlExecution;29import org.cerberus.engine.execution.impl.handler.SeleniumActionHandlerFactory.TestCaseStepExecutionControlExecutionControlExecutionControlExecutionControlExecution;30import org.cerberus.engine.execution.impl.handler
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!!