Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling to a Sikuli script from Python (Selenium)

While running Selenium tests on a website, I have some Flash elements that I cannot test with Selenium/Python. I wanted to call out for a separate terminal window, run the Sikuli OCR tests, and then back into the Selenium/Python testing. I've not been able to figure this out exactly. I put XXX where I do not know the arguments for a new Terminal to open and run the Sikuli script.

def test_05(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_link_text("Home").click()
        driver.find_element_by_id("open_popup").click()
        driver.find_element_by_id("screen_name").send_keys("user")
        driver.find_element_by_id("password").send_keys("pwd")
        driver.find_element_by_id("login_submit").click()
        driver.find_element_by_id("button").click()
        time.sleep(120)
        os.system('XXX')
        os.system('./Sikuli/sikuli-script -r test.sikuli')

I am sure there are a couple items wrong here. Any help would be greatly appreciated. I've searched and read what I can find on this already, but can't get it all to work together.

like image 993
Dave Avatar asked Aug 12 '13 23:08

Dave


3 Answers

I ran into a similar issue, so I wrote a CPython module for Sikuli. The module is hosted on GitHub and available via pip install sikuli. It's able to access an included Sikuli jar using pyjnius, so you don't have to use Jython or even install Sikuli itself (although I'd recommend it for recording purposes). The module currently covers most of the simpler Sikuli functions, so it should cover a lot of use cases.

After installing, a simple from sikuli import * will get you started, but as a best practice, I'd suggest only importing the functions you want to use. This is particularly important for this module, because sikuli has a type function which overrides Python's own type function.

like image 123
kevlened Avatar answered Sep 17 '22 12:09

kevlened


If your sikuli script is completely independent and you just want to run it for once and then have control back to your python script. Then you can create a batch file, which calls your sikuli script and call this batch file from your python script instead. Once the batch file is done running, it exits and returns the control back to your python script.

Sample Batch file:

@echo off
call C:\Sikuli\runIDE.cmd -r C:\Automation\Test1.sikuli
exit

Code snippet to call Sikuli script from inside python:

import subprocess

def runSikuliScript(path):
    filepath = path
    p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)
    stdout, stderr = p.communicate()
    print "Done Running Sikuli"

p = "C:\\Automation\\Test1\\test1.bat"
runSikuliScript(p)
// You can carry on writing your python code from here on
like image 45
Tanuvir Avatar answered Sep 18 '22 12:09

Tanuvir


For calling Sikuli code from Selenium, my first choice would be TestAutomationEngr's suggestion of using Java, since Selenium and Sikuli both have native Java bindings.

Since you want to use Python, you should try running Selenium under Jython. It's important to remember that Sikuli is Jython, which is probably why you're not able to import it. (The other reason would be that you don't have it in Jython's module path.) I have not tried this myself, but there was a bug fixed last year in Selenium which indicates that it should be fine under Jython.

Note that if you call your Sikuli code directly from Jython, you need to add

from sikuli.Sikuli import *

to the top. This is because the Sikuli IDE implicitly adds that to all Sikuli code.

Finally, your last resort is to call Sikuli from the command line. There's an FAQ for that. You probably want the "without IDE" version, where you're calling Java and passing in the sikuli-script JAR file.

like image 40
Nathaniel Waisbrot Avatar answered Sep 20 '22 12:09

Nathaniel Waisbrot