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 java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Base64;
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 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 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());
}
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;
}
}
}
}
}