javaautomationsikuli

Is there a way to define a subfolder in Brobot for each state?


I already know that we can define the image folder in Brobot like this:

ImagePath.setBundlePath("images");

But I would like to define a subfolder for each state. Is this possible, and if so, how?

I already tried to set setBundlePath inside each state, but it didn't work. I also tried to define the relative path of each image in the StateImage builder using addPattern, but that didn't work either:

private StateImage cookies_not_present = new StateImage.Builder().addPattern("cookies/cookiesNotPresent").build();

Solution

  • Both of these methods work. In addition to the explanation here, you can also see and run units tests for both methods in the source code on GitHub.

    Example State Structure

    Let's say you have two states, Foo and Bar. If you would like to include an images folder for each state, you could place them with the state and transitions classes. This is a valid way to organize a state structure in Brobot, especially for large automation projects. Your folder structure would look like this:

    bar and foo folders

    Method #1: Set the Bundle Path

    The image paths are set using functionality from the SikuliX dependency. In order to add an additional image path, you can use the method ImagePath.add(path_from_root). I would add this to the @SpringBootApplication class, together with any setup code for Brobot. Use the path from the project's root directory. If the project file structure from root looks like this,

    file structure from root

    you would need to add two lines to your @SpringBootApplication class:

    ImagePath.add("src/main/java/com/example/scratch/bar/images"); 
    ImagePath.add("src/main/java/com/example/scratch/foo/images"); 
    

    If cookies_not_present is in the folder foo/images or bar/images, you would define your StateImage as follows:

    private StateImage cookies_not_present = new StateImage.Builder()
            .addPattern("cookiesNotPresent")
            .build();
    

    Method #2: Specify the Path in the StateImage or Pattern Builder

    The default image path in Brobot is images, located in the root directory. If you are using this default path, you can define a StateImage or Pattern with the path relative to images. If cookies_not_present is in the folder foo/images, you would define your StateImage as follows:

    private StateImage cookies_not_present = new StateImage.Builder()
            .addPattern("../src/main/java/com/example/scratch/foo/images/cookiesNotPresent")
            .build();
    

    In the pre-release version (1.0.7), when a filename is specified in the builder but a name is not, the name is defined from the filename. The path and file extension are removed, so the name of the Pattern in the above example would be cookiesNotPresent. You can also set the name explicitly, which would look like this:

    Pattern cookiesNotPresent = new Pattern.Builder()
            .setFilename("../src/main/java/com/example/scratch/foo/images/cookiesNotPresent")
            .setName("missing chocolate cookies")
            .build();