def restart_flex():
    helper = TestHelper("restart_flex")
    helper.write("restarting flex")
    helper.Type("t", KeyModifier.CTRL | KeyModifier.ALT, time=5)
    helper.Type("sudo /home/vagrant/Integration-Testing-Framework/sikuli/examples/restart_flex.sh && exit" + Key.ENTER)

    # If the 'hello' project shows up as the last opened project,
    # just press enter
    if helper.Exists(Pattern("iii3iuE.png").similar(0.90), "'hello' project not showing as last opened, " +
              "trying to open via 'Open a project'", give_up=False):
        helper.write("Success")
        helper.Type(Key.ENTER, time=20)

    # If not, try to find it in the 'Open project screen'
    else:
        helper.Click("Openaproject.png", "Couldn't find 'Open a project' button")
        hello = helper.Find(Pattern("hello.png").similar(0.90), "'hello' project not found in the 'Open Project'" +
                     "screen, trying to create a new project", give_up=False)
        if hello:
            helper.DoubleClick(hello, "Found 'hello' but didn't manage to click it, wat!?",
                  time=20)
            helper.write("Success")

        # As a last resort, try to create the project (this shouldn't happen
        # and will probably not work: green screen if hello already exists.)
        else:
            helper.write_fail("'hello' project not found, quitting.")
            exit()
     wait(2)
region = first_region.offset(Location(0, 18))
count = 0

# Goal
###############

# keep clicking while the list still has items 
while not region.exists(Pattern("blank_space.png").similar(0.99)):
    
    global changed
    changed = False

    # keep count and make sure we don't go too far
    count += 1
    if count > 9:
        helper.write_fail("Script malfunction: went too far down the left column")
        break;

    # Click in the middle of the region and react to change
    MID_TOOLBAR.onChange(stop_observer)
    click(region.getCenter())
    MID_TOOLBAR.observe(5)
    # Check if the observer stopped as intended
    if not changed:
        helper.write_fail("No change when clicking item " + str(count))

    # Move region down by 18 pixels
    region = region.offset(Location(0, 18))

# Closing
###############
def run_test(language_idx):

    setAutoWaitTimeout(1)
    helper = TestHelper("test_" + languages[language_idx].lower() + "_localization")
    set_flex_helper(helper)
    
    # Opening: change UI to the language we want
    ################

    # Open Options popup
    wait(2)
    type("1", KeyModifier.ALT)
    for i in range(7):
        type(Key.RIGHT)
    for i in range(5):
        type(Key.DOWN)
    helper.Type(Key.ENTER)

    # Get to language drop-down menu
    type(Key.TAB)
    type(Key.TAB)

    # Get to the right language
    for i in range(len(languages)):
        type(Key.UP)
    for i in range(language_idx):
        type(Key.DOWN)

    type(Key.ENTER)
    wait(10)

    # GOAL
    ################

    for k in range(len(areas)):
        area = areas[k]

        # First see if there is any English (note: cognates may be
        # flagged as not translated)
        if language_idx != 2:
            # Check if whole area matches
            if area_regions[k].exists(Pattern(english_reference_images[k]).
                    similar(0.99)):
                helper.write_fail("Nothing translated in " + area)
                # If everything's in English, no need for more tests
                continue
            
            # Look for any English words
            if see_if_things_are_translated:
                words = english_words[k]
                for word in words:
                    if area_regions[k].exists(word):
                        helper.write_fail("'" + word + "' not translated in " + area)
        
        # Look for any placeholder squares
        if area_regions[k].exists(square_types[k]):
            helper.write_fail("Unable to render (placeholder boxes found) in " + area)

    
    # Closing: Go back to English UI
    ##############
    
    # Open Options popup
    type("1", KeyModifier.ALT)
    for i in range(7):
        type(Key.RIGHT)
    for i in range(5):
        type(Key.DOWN)
    helper.Type(Key.ENTER)
    
    # Get to language drop-down menu
    type(Key.TAB)
    type(Key.TAB)
        
    # Get to the top of the list, then go down to English
    for i in range(len(languages)):
        type(Key.UP)
    for i in range(languages.index("English")):
        type(Key.DOWN)
    type(Key.ENTER)
    wait(40)
    
    helper.write_success()
from sikuli import *

sys.path.insert(0, '/home/vagrant/Integration-Testing-Framework/sikuli/examples')
from test_helper import TestHelper

helper = TestHelper("close_flex")

# Close FLEX - This is the last script
switchApp("FieldWorks Language Explorer")
helper.Click("1436902218392.png", "Cannot find orange close button")

if helper.has_fail():
    helper.write_fail("Failed to close")
helper.write_success()