← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #147000]: How do you organize image reference inside your code?

 

Question #147000 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/147000

    Status: Open => Answered

RaiMan proposed the following answer:
this is based on the features of RC2
(http://sikuli.org/docx/globals.html#importing-other-sikuli-scripts-
reuse-code-and-images)

--1. use "speaking" image filenames  
you could have a naming convention, that is implemented with the filenames of the .png files.

e.g. "Level1-Level2-name.png" (2 Levels should normally be enough)

so you could put all images in one directory and use addImagePath() to
find the images.

usage: find("Level1-Level2-name.png")

--2. use Python variables as translators
for each bunch of images make a .sikuli, that contains the images and a .py containing

level1_level2_name = "some-image.png"

for each .sikuli you need an
from some_images.sikuli import *
to get the variable names into your namespace (Sikuli adds the path automatically to the image path so the images are found)

again you need a similar naming convention as with --1.

usage: find(level1_level2_name)

If you might want to store more information with an image (e.g. pixel
solution, system type, ...), you could use a dictionary to store the
image filenames and the additional information (the above variable name
string would become the key).

advantage against --1. is, that the image file names are left in the
container: you may always decide to change them, without touching the
main script.

This basic concept can be extended in any direction (using classes with
image access methods or some json based wrapper)

--3. To be open in any direction, I would implement an image accessor
function in your runtests.sikuli from beginning e.g.

def getImg(prefix, name):
    # some code that builds the appropriate image filename
    # prefix is a list of strings according to the naming convention
    # so it can be used with any number of levels

    # for case --1.
    imageFilename =  '-'.join(prefix.append(n))+'.png'

    # for case --2.
    imageFilename = eval( '_'.join(prefix.append(n)))

    return imgFilename

usage: find(getImg(["MainContext", "SubContext"], "some-button"))

if ["MainContext", "SubContext"] is the same for some part of code or a
whole testcase, you might say once

pre = ["MainContext", "SubContext"]

and fourther only 
find getImg(pre, "some-button"))

Such a function makes it possible, to hide the concrete access concept.

And for the beginning it could even be adapted to your directory
structure solution.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.