com.codacy.scoobydoo.utils.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scooby-doo-fwk Show documentation
Show all versions of scooby-doo-fwk Show documentation
Automated testing framework
package com.codacy.scoobydoo.utils;
import com.codacy.scoobydoo.LoggingHelper;
import org.openqa.selenium.By;
import org.testng.asserts.SoftAssert;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Base64;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Utils {
public static class ElementProperty {
public static final String CLICKABLE = "clickable";
public static final String VISIBLE = "visible";
}
// suppressed default constructor for noninstantiability.
private Utils() {
throw new AssertionError("Utils class should not be instantiated.");
}
public static void assertStringHasValue(String targetString) {
if (targetString == null || targetString.isBlank() || targetString.isEmpty()) {
throw new IllegalArgumentException("String cannot be null or empty.");
}
}
public static String getAPIRequestPayloadFromJsonFilename(String fileName) {
final String jsonFilePath= "src/test/resources/payloads/" + fileName;
try{
return new String(Files.readAllBytes(Paths.get(jsonFilePath)));
}catch (Exception e){
LoggingHelper.error("Could not get payload from file: [" + jsonFilePath + "].", e);
throw new RuntimeException(e);
}
}
public static String getEncodedCursor(int cursor) {
return Base64.getEncoder().encodeToString(String.valueOf(cursor).getBytes());
}
public static String getSubstring(String inputString, String regexPattern) {
Matcher matcher = Pattern.compile(regexPattern).matcher(inputString);
return matcher.find() ? matcher.group(1) : "";
}
public static String getWaitErrorLogMessage(By locator, String property, Duration timeout) {
return String.format("Element [%s] was not [%s] after a timeout of [%s] seconds.",
locator.toString(), property, timeout.getSeconds());
}
public static String getWaitLogMessage(By locator, String property, Duration timeout, Duration polling) {
return String.format("Wait for element [%s] to be [%s] with a timeout of [%s] seconds and polling [%s] milliseconds.",
locator.toString(), property, timeout.getSeconds(), polling.toMillis());
}
/**
* Runs all the provided teardown methods, ignoring any exceptions that might occur, to ensure all steps are performed.
* In the end, a soft assertion is performed to ensure that all teardown methods were executed successfully.
*/
public static void runAllTeardownMethodsIgnoringExceptions(List teardownMethods) {
SoftAssert softAssertions = new SoftAssert();
for (Runnable method : teardownMethods) {
try {
method.run();
} catch (Exception | AssertionError e) {
final String errorMessage = "Failed while running teardown method.";
softAssertions.fail(errorMessage);
LoggingHelper.error(errorMessage, e);
}
}
softAssertions.assertAll();
}
public static void runMethodWithRetries(Runnable runnable, int maxRetries) {
int attempt = 0;
while(attempt <= maxRetries) { // loop will never reach maxRetries, but this check is here in order to ensure no infinite loops will ever happen.
try {
runnable.run();
break;
} catch (AssertionError | Exception e) {
LoggingHelper.error("Failed while trying to run method.", e);
attempt++;
if (attempt >= maxRetries) {
throw e;
}
}
}
}
}