Skip to content

Commit

Permalink
Take a desktop Sikuli screenshot in case of error even if a Cerberus …
Browse files Browse the repository at this point in the history
…exception is raised.
  • Loading branch information
vertigo17 committed Dec 13, 2024
1 parent 9e4367e commit a59c2ca
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ public List<TestCaseExecutionFile> recordScreenshot(TestCaseExecution execution,
newImage = this.sikuliService.takeScreenShotFile(execution.getSession());
}

long maxSizeParam = parameterService.getParameterIntegerByKey("cerberus_screenshot_max_size", "", 1048576);
if (newImage != null) {
try {
long maxSizeParam = parameterService.getParameterIntegerByKey("cerberus_screenshot_max_size", "", 1048576);

Recorder recorder = this.initFilenames(runId, test, testCase, step, index, sequence, controlString, null, 0, fileName, "png", false);
LOG.debug("{}FullPath {}", logPrefix, recorder.getFullPath());
Expand Down Expand Up @@ -439,36 +439,45 @@ public List<TestCaseExecutionFile> recordScreenshot(TestCaseExecution execution,
LOG.debug("{}Temp file deleted with success {}", logPrefix, newImage.getName());
LOG.debug("{}Screenshot done in : {}", logPrefix, recorder.getRelativeFilenameURL());

if (newImageDesktop != null) {
Recorder recorderDestop = this.initFilenames(runId, test, testCase, step, index, sequence, controlString, null, 0, fileName + "-desktop", "png", false);
LOG.debug("{}FullPath {}", logPrefix, recorderDestop.getFullPath());

// Getting the max size of the screenshot.
fileDesc = "Desktop " + fileDescription;
if (maxSizeParam < newImageDesktop.length()) {
LOG.warn("{}Screenshot size exceeds the maximum defined in configurations ({}>={}) {} destination: {}", logPrefix, newImageDesktop.length(), maxSizeParam, newImageDesktop.getName(), recorderDestop.getRelativeFilenameURL());
fileDesc = "Desktop Screenshot Too Big !!";
} else {
// Copies the temp file to the execution file
FileUtils.moveFile(newImageDesktop, new File(recorderDestop.getFullFilename()));
LOG.debug("{}Moving file finished with success - source: {} destination: {}", logPrefix, newImageDesktop.getName(), recorderDestop.getRelativeFilenameURL());
LOG.info("File saved : {}", recorderDestop.getFullFilename());
}
} catch (IOException | CerberusException ex) {
LOG.error("{}{}", logPrefix, ex.toString(), ex);
}
} else {
LOG.warn("{}Screenshot returned null.", logPrefix);
}

// Index file created to database.
object = testCaseExecutionFileFactory.create(0, execution.getId(), recorderDestop.getLevel(), fileDesc, recorderDestop.getRelativeFilenameURL(), "PNG", "", null, "", null);
objectList.add(object);
testCaseExecutionFileService.save(object);
if (newImageDesktop != null) {

try {

Recorder recorderDestop = this.initFilenames(runId, test, testCase, step, index, sequence, controlString, null, 0, fileName + "-desktop", "png", false);
LOG.debug("{}FullPath {}", logPrefix, recorderDestop.getFullPath());

//deletes the temporary file
LOG.debug("{}Desktop Screenshot done in : {}", logPrefix, recorderDestop.getRelativeFilenameURL());
// Getting the max size of the screenshot.
String fileDesc = "Desktop " + fileDescription;
if (maxSizeParam < newImageDesktop.length()) {
LOG.warn("{}Screenshot size exceeds the maximum defined in configurations ({}>={}) {} destination: {}", logPrefix, newImageDesktop.length(), maxSizeParam, newImageDesktop.getName(), recorderDestop.getRelativeFilenameURL());
fileDesc = "Desktop Screenshot Too Big !!";
} else {
// Copies the temp file to the execution file
FileUtils.moveFile(newImageDesktop, new File(recorderDestop.getFullFilename()));
LOG.debug("{}Moving file finished with success - source: {} destination: {}", logPrefix, newImageDesktop.getName(), recorderDestop.getRelativeFilenameURL());
LOG.info("File saved : {}", recorderDestop.getFullFilename());
}

// Index file created to database.
object = testCaseExecutionFileFactory.create(0, execution.getId(), recorderDestop.getLevel(), fileDesc, recorderDestop.getRelativeFilenameURL(), "PNG", "", null, "", null);
objectList.add(object);
testCaseExecutionFileService.save(object);

//deletes the temporary file
LOG.debug("{}Desktop Screenshot done in : {}", logPrefix, recorderDestop.getRelativeFilenameURL());
} catch (IOException | CerberusException ex) {
LOG.error("{}{}", logPrefix, ex.toString(), ex);
}
} else {
LOG.warn("{}Screenshot returned null.", logPrefix);

}

return objectList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1130,36 +1130,36 @@ private MessageEvent doActionMouseMove(TestCaseExecution tCExecution, String val
return message;
}

private MessageEvent doActionSwitchToWindow(TestCaseExecution tCExecution, String object, String property) {
private MessageEvent doActionSwitchToWindow(TestCaseExecution execution, String object, String property) {
String element;
try {
/**
* Get element to use String object if not empty, String property if
* object empty, throws Exception if both empty)
*/
element = getElementToUse(object, property, "switchToWindow", tCExecution);
element = getElementToUse(object, property, "switchToWindow", execution);
/**
* Get Identifier (identifier, locator)
*/
Identifier identifier = identifierService.convertStringToIdentifier(element);
//identifierService.checkWebElementIdentifier(identifier.getIdentifier());

if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_GUI)) {
return webdriverService.doSeleniumActionSwitchToWindow(tCExecution.getSession(), identifier);
if (execution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_GUI)) {
return webdriverService.doSeleniumActionSwitchToWindow(execution.getSession(), identifier);

} else if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_APK)) {
return androidAppiumService.switchToContext(tCExecution.getSession(), identifier);
} else if (execution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_APK)) {
return androidAppiumService.switchToContext(execution.getSession(), identifier);

} else if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_IPA)) {
return iosAppiumService.switchToContext(tCExecution.getSession(), identifier);
} else if (execution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_IPA)) {
return iosAppiumService.switchToContext(execution.getSession(), identifier);

} else if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_FAT)) {
return sikuliService.doSikuliActionSwitchApp(tCExecution.getSession(), identifier.getLocator());
} else if (execution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_FAT)) {
return sikuliService.doSikuliActionSwitchApp(execution.getSession(), identifier.getLocator());

} else {
return new MessageEvent(MessageEventEnum.ACTION_NOTEXECUTED_NOTSUPPORTED_FOR_APPLICATION)
.resolveDescription("ACTION", "SwitchToWindow")
.resolveDescription("APPLICATIONTYPE", tCExecution.getApplicationObj().getType());
.resolveDescription("APPLICATIONTYPE", execution.getApplicationObj().getType());
}
} catch (CerberusEventException ex) {
LOG.fatal("Error doing Action SwitchToWindow :" + ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ public enum MessageEventEnum {
ACTION_FAILED_WITHDETAIL(250, "FA", "Action Error : %DETAIL%", true, true, false, MessageGeneralEnum.EXECUTION_FA),
ACTION_FAILED_CLICK(251, "FA", "Failed to click on '%ELEMENT%'. %MESS%", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_SELENIUM_CONNECTIVITY(252, "CA", "The test case is cancelled due to lost connection to Selenium Server! Detailed error : %ERROR%", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_SELENIUM_EXCEPTION(252, "FA", "Action failed due to Selenium Exception! Detailed error : '%ERROR%'", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_NO_SUCH_ELEMENT(253, "FA", "Identifier '%IDENTIFIER%=' isn't recognized! Use: id=, name=, class=, css= , xpath= , link= , picture= or data-cerberus=.", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_UNKOWN_IDENTIFIER_SIKULI(253, "FA", "Identifier '%IDENTIFIER%=' isn't recognized! Use: picture= or text=.", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_UNKOWN_IDENTIFIER_SELENIUM(253, "FA", "Identifier '%IDENTIFIER%=' isn't recognized! Use: id=, name=, class=, css= , xpath= , link= , coord=, picture= or data-cerberus=.", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private By getBy(Identifier identifier) {
return by;
}

public AnswerItem<WebElement> getWebElement(Session session, Identifier identifier, boolean random, int rank){
public AnswerItem<WebElement> getWebElement(Session session, Identifier identifier, boolean random, int rank) {

AnswerItem<WebElement> answer = new AnswerItem<>();
MessageEvent msg;
Expand All @@ -147,32 +147,32 @@ public AnswerItem<WebElement> getWebElement(Session session, Identifier identifi
Instant now = Instant.now();
Instant stop = now.plus(session.getCerberus_selenium_wait_element(), ChronoUnit.MILLIS);

while(stop.isAfter(Instant.now()) && element==null) {
while (stop.isAfter(Instant.now()) && element == null) {

try {
//Find all elements retrieved
List<WebElement> elements = session.getDriver().findElements(this.getBy(identifier));
//Find all elements retrieved
List<WebElement> elements = session.getDriver().findElements(this.getBy(identifier));

// If random, overide rank with one random int from 1 to the size of the result
if (random) {
Random r = new Random();
rank = r.nextInt(elements.size());
}
// -1 because default value is 1
element = (WebElement) elements.get(rank);
// If random, overide rank with one random int from 1 to the size of the result
if (random) {
Random r = new Random();
rank = r.nextInt(elements.size());
}
// -1 because default value is 1
element = (WebElement) elements.get(rank);

} catch (Exception ex) {
LOG.debug("ELEMENT NOT FOUND : ", identifier.getIdentifier() + "=" + identifier.getLocator() + " : TRYING AGAIN");
}
}

if (element == null){
if (element == null) {
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT);
msg.resolveDescription("ELEMENT", identifier.getIdentifier() + "=" + identifier.getLocator());
} else {
answer.setItem(element);
msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT);
msg.resolveDescription("ELEMENT", identifier.getIdentifier() + "=" + identifier.getLocator() );
msg.resolveDescription("ELEMENT", identifier.getIdentifier() + "=" + identifier.getLocator());
}

answer.setResultMessage(msg);
Expand Down Expand Up @@ -432,7 +432,7 @@ private AnswerItem<WebElement> getSeleniumElement(Session session, Identifier id
Integer numberOfElement = 0;
try {
numberOfElement = this.getNumberOfElements(session, identifier);
} catch(Exception ex){
} catch (Exception ex) {
//No element found
}
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT);
Expand Down Expand Up @@ -549,7 +549,7 @@ public String getElements(Session session, Identifier identifier) {
List<String> result = new ArrayList();

List<WebElement> elements = driver.findElements(this.getBy(identifier));
for(WebElement element : elements){
for (WebElement element : elements) {
result.add(element.getAttribute("innerHTML").toString());
}

Expand All @@ -562,7 +562,7 @@ public String getElementsValues(Session session, Identifier identifier) {
List<String> result = new ArrayList();

List<WebElement> elements = driver.findElements(this.getBy(identifier));
for(WebElement webElement : elements){
for (WebElement webElement : elements) {
if (webElement != null) {
if (webElement.getTagName().equalsIgnoreCase("select")) {
Select select = (Select) webElement;
Expand Down Expand Up @@ -642,7 +642,7 @@ public String getValueFromJS(Session session, String script) {
}

@Override
public String getAttributeFromHtml(Session session, Identifier identifier, String attribute, boolean random, Integer rank) {
public String getAttributeFromHtml(Session session, Identifier identifier, String attribute, boolean random, Integer rank) {
String result = null;
try {
AnswerItem answer = this.getWebElement(session, identifier, random, rank);
Expand Down Expand Up @@ -1044,7 +1044,7 @@ public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier i
// Add try catch to handle not exist anymore window (like when popup is closed).
try {
currentWindowId = session.getDriver().getWindowHandle();
initialContext = "URL:"+session.getDriver().getCurrentUrl()+" | Title:"+session.getDriver().getTitle();
initialContext = "URL:" + session.getDriver().getCurrentUrl() + " | Title:" + session.getDriver().getTitle();
} catch (NoSuchWindowException exception) {
currentWindowId = null;
initialContext = "Page has been closed.";
Expand All @@ -1059,18 +1059,18 @@ public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier i
String targetHandle = null;
for (String windowHandle : handles) {
//if (!windowHandle.equals(currentWindowId)) {
session.getDriver().switchTo().window(windowHandle);
allContexts.add("URL:"+session.getDriver().getCurrentUrl()+" | Title:"+session.getDriver().getTitle());
session.getDriver().switchTo().window(windowHandle);
allContexts.add("URL:" + session.getDriver().getCurrentUrl() + " | Title:" + session.getDriver().getTitle());

if (checkIfExpectedWindow(session, identifier.getIdentifier(), identifier.getLocator())) {
targetHandle = windowHandle;
targetContext = "URL:"+session.getDriver().getCurrentUrl()+" | Title:"+session.getDriver().getTitle();
}
if (checkIfExpectedWindow(session, identifier.getIdentifier(), identifier.getLocator())) {
targetHandle = windowHandle;
targetContext = "URL:" + session.getDriver().getCurrentUrl() + " | Title:" + session.getDriver().getTitle();
}
//}
LOG.debug("windowHandle=" + windowHandle);
}

if (!StringUtil.isEmptyOrNull(targetHandle)){
if (!StringUtil.isEmptyOrNull(targetHandle)) {
session.getDriver().switchTo().window(targetHandle);
message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SWITCHTOWINDOW);
message.setDescription(message.getDescription()
Expand Down Expand Up @@ -1226,7 +1226,7 @@ private boolean checkIfExpectedWindow(Session session, String identifier, String
}

@Override
public MessageEvent doSeleniumActionDoubleClick(Session session, Identifier identifier, Integer hOffset, Integer vOffset, boolean waitForVisibility, boolean waitForClickability) {
public MessageEvent doSeleniumActionDoubleClick(Session session, Identifier identifier, Integer hOffset, Integer vOffset, boolean waitForVisibility, boolean waitForClickability) {
MessageEvent message;
try {
AnswerItem answer = this.getSeleniumElement(session, identifier, waitForVisibility, waitForClickability);
Expand Down Expand Up @@ -1312,7 +1312,7 @@ public MessageEvent doSeleniumActionMouseOver(Session session, Identifier identi
actions.build().perform();
message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_MOUSEOVER);
message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));
message.setDescription(message.getDescription().replace("%OFFSET%", "("+hOffset+","+vOffset+")"));
message.setDescription(message.getDescription().replace("%OFFSET%", "(" + hOffset + "," + vOffset + ")"));
return message;
}
}
Expand All @@ -1321,7 +1321,7 @@ public MessageEvent doSeleniumActionMouseOver(Session session, Identifier identi
} catch (NoSuchElementException exception) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_MOUSEOVER_NO_SUCH_ELEMENT);
message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));
message.setDescription(message.getDescription().replace("%OFFSET%", "("+hOffset+","+vOffset+")"));
message.setDescription(message.getDescription().replace("%OFFSET%", "(" + hOffset + "," + vOffset + ")"));
LOG.debug(exception.toString());
return message;
} catch (TimeoutException exception) {
Expand Down Expand Up @@ -2046,7 +2046,7 @@ public MessageEvent doSeleniumActionRightClick(Session session, Identifier ident
private MessageEvent parseWebDriverException(WebDriverException exception) {
MessageEvent mes;
LOG.fatal(exception.toString());
mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SELENIUM_CONNECTIVITY);
mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SELENIUM_EXCEPTION);
mes.setDescription(mes.getDescription().replace("%ERROR%", exception.getMessage().split("\n")[0]));
return mes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ identifier can be **__title__**, **__url__**, *_regexUrl_* or *_regexTitle_*
title=titleOfThisNewWindow
====

.By Regex Tittle
====
regexTitle=.\*ThisNewWindow.*
====

.By Url
====
url=http://url_of_this_new_window
Expand Down
2 changes: 1 addition & 1 deletion source/src/main/webapp/js/pages/TestCaseExecutionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function aoColumnsFunc() {
"sName": "exe.start",
"like": true,
"title": doc.getDocOnline("page_executiondetail", "start"),
"sWidth": "70px",
"sWidth": "110px",
"sDefaultContent": "",
"mRender": function (data, type, obj) {
return new Date(obj.start).toLocaleString();
Expand Down

0 comments on commit a59c2ca

Please sign in to comment.