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

nl.praegus.fitnesse.slim.fixtures.sikuli.SikuliTest Maven / Gradle / Ivy

The newest version!
package nl.praegus.fitnesse.slim.fixtures.sikuli;

import nl.hsac.fitnesse.fixture.slim.SlimFixture;
import nl.hsac.fitnesse.fixture.slim.SlimFixtureException;
import nl.hsac.fitnesse.fixture.slim.web.annotation.WaitUntil;
import nl.hsac.fitnesse.fixture.util.selenium.by.XPathBy;
import org.sikuli.basics.Settings;
import org.sikuli.script.*;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class SikuliTest extends SlimFixture {

    private Screen screen;
    private App app;
    private String imagePath;
    private String screenshotsDir = new File(filesDir, "screenshots").getPath() + "/";
    private boolean recordMode;

    public SikuliTest() {
        screen = new Screen();
        defaultImagePath();
    }

    public SikuliTest(String ocrLanguage) {
        Settings.OcrLanguage = ocrLanguage;
        screen = new Screen();
        defaultImagePath();
    }

    public boolean startApp(String commandline) {
        Settings.OcrTextRead = true;
        Settings.OcrTextSearch = true;
        Settings.SwitchToText = true;
        app = new App(commandline);
        return app.open() && app.focus() && app.hasFocus();
    }

    public boolean closeApp() {
        if (null != app) {
            return app.close();
        }
        return false;
    }

    public void setRecordMode(boolean recordMode) {
        this.recordMode = recordMode;
    }

    public void setImagePath(String path) {
        imagePath = getFilePathFromWikiUrl(path);
        ImagePath.add(imagePath);
    }

    protected void defaultImagePath() {
        setImagePath(new File(filesDir, "sikuli").getPath() + File.separatorChar);
    }

    public void secondsBeforeTimeout(int timeout) {
        screen.setAutoWaitTimeout(timeout);
    }

    public int secondsBeforeTimeout() {
        Double secs = screen.getAutoWaitTimeout();
        return secs.intValue();
    }

    @WaitUntil
    public boolean click(String image) {
        if (recordMode) {
            waitSeconds(1);
            recordRegionAs(image);
        }
        try {
            return screen.click(image) == 1;
        } catch (FindFailed e) {
            return false;
        }
    }

    @WaitUntil
    public boolean clickWord(String text) {
        return screen.findWord(text).click() > 0;
    }

    @WaitUntil
    public boolean clickLine(String text) {
        return screen.findLine(text).click() > 0;
    }

    public boolean doubleClickText(String text) {
        return screen.findLine(text).doubleClick() > 0;
    }

    public boolean doubleClick(String image) {
        if (recordMode) {
            waitSeconds(1);
            recordRegionAs(image);
        }
        try {
            return screen.doubleClick(image) == 1;
        } catch (FindFailed e) {
            return false;
        }
    }

    public boolean enterIn(String value, String image) {
        if (recordMode) {
            waitSeconds(1);
            recordRegionAs(image);
        }
        return screen.type(image, value) == 1;
    }

    public boolean press(String key) {
        return screen.type(getKeyCode(key)) > 0;
    }

    public void ctrlPress(String key) {
        screen.type(getKeyCode(key), KeyModifier.CTRL);
    }

    public void mndPress(String key) {
        screen.type(getKeyCode(key), KeyModifier.CMD);
    }

    public void altPress(String key) {
        screen.type(getKeyCode(key), KeyModifier.ALT);
    }

    public boolean type(String keys) {
        String[] keyArr = keys.split("(?!^)");
        StringBuilder keySeq = new StringBuilder();
        for (String key : keyArr) {
            keySeq.append(getKeyCode(key));
        }
        return screen.type(keySeq.toString()) > 0;
    }

    private String getKeyCode(String key) {
        try {
            Field field = Key.class.getField(key);
            return (String) field.get(new Key());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            return key;
        }
    }

    public String takeScreenshot(String fileName) {
        screen.capture();
        if (!fileName.endsWith(".png")) {
            fileName += ".png";
        }
        try {
            String lastImage;
            fileName = fileName.replace("/", File.separator).replace("\\", File.separator);
            if (fileName.lastIndexOf(File.separator) >= 0) {

                String completeFileName = screenshotsDir + File.separator + fileName;
                String path = getFilePathFromWikiUrl(completeFileName);
                ensureDirectoryExists(path);

                lastImage = screen.getLastScreenImageFile(screenshotsDir, fileName);
            } else {
                lastImage = screen.getLastScreenImageFile(screenshotsDir, fileName);
            }

            String imgLink = getWikiUrl(lastImage);
            return String.format("", imgLink);
        } catch (IOException e) {
            throw new SlimFixtureException(false, "Unable to create screenshot", e);
        }
    }

    public String textOnsScreen() {
        return screen.text();
    }


    protected void ensureDirectoryExists(String fileName) {
        File output = new File(fileName);
        File parent = output.getAbsoluteFile().getParentFile();
        if (!parent.exists()) {
            if (!parent.mkdirs()) {
                throw new IllegalArgumentException(
                        "Unable to create directory: "
                                + parent.getAbsolutePath());
            }
        }
    }

    private void recordRegionAs(String filename) {
        if (recordMode) {
            screen.userCapture("Select region for " + filename).getFile(imagePath, filename);
        }
    }

    public boolean waitForVisible(String image) {
        if (recordMode) {
            waitSeconds(1);
            recordRegionAs(image);
        }
        try {
            return null != screen.wait(image);
        } catch (FindFailed e) {
            return false;
        }
    }

    public boolean waitForText(String text) {
        return null != screen.existsText(text);
    }

    public String valueOfInput(String inputImg) {
        if (recordMode) {
            waitSeconds(1);
            recordRegionAs(inputImg);
        }
        App.setClipboard("");
        click(inputImg);
        screen.type("a", KeyModifier.CTRL);
        screen.type("c", KeyModifier.CTRL);
        return App.getClipboard();
    }

    public void clear(String inputImg) {
        if (recordMode) {
            waitSeconds(1);
            recordRegionAs(inputImg);
        }
        click(inputImg);
        screen.type("a", KeyModifier.CTRL);
    }


    public String textFromRegion(List region) {
        String regionInfo = "";
        if (recordMode) {
            waitSeconds(1);
            Region selectedRegion = userCreatedRegion("Select region for text extraction");
            regionInfo = getRecordedRegionInfo(selectedRegion);
        }
        if (region.size() != 4) {
            throw new SlimFixtureException("Define region as [x, y, width, height]");
        } else {
            String regionText = screen.newRegion(region.get(0), region.get(1), region.get(2), region.get(3))
                    .highlight().text().trim();
            if (!regionInfo.isEmpty()) {
                throw new SlimFixtureException(false, String.format("
%s
Recognized text: [%s]
", regionInfo, regionText)); } else { return regionText; } } } private Region userCreatedRegion(String message) { return screen.selectRegion(message); } private String getRecordedRegionInfo(Region region) { List regionCoords = regionDefinitionAsList(region); String regionFileName = String.format("region_%s-%s-%s-%s.png", regionCoords.get(0), regionCoords.get(1), regionCoords.get(2), regionCoords.get(3)); String imgLink = getWikiUrl(screen.capture(region).getFile(imagePath, regionFileName)); return String.format("
Region recorded:
Region: %s
", imgLink, region.toString()); } public String normalizedTextFromRegion(List region) { String value = textFromRegion(region); return XPathBy.getNormalizedText(value); } private List regionDefinitionAsList(Region region) { List result = new ArrayList<>(); result.add(region.x); result.add(region.y); result.add(region.w); result.add(region.h); return result; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy