Calling to a Sikuli script from Python (Selenium)

六月ゝ 毕业季﹏ 提交于 2019-11-27 20:56:15

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.

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!