All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codacy.scoobydoo.utils.Utils Maven / Gradle / Ivy

There is a newer version: 3.30.0
Show newest version
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;
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy