简体   繁体   English

从Python调用Sikuli脚本(Selenium)

[英]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. 在网站上运行Selenium测试时,我有一些我无法使用Selenium / Python测试的Flash元素。 I wanted to call out for a separate terminal window, run the Sikuli OCR tests, and then back into the Selenium/Python testing. 我想呼叫一个单独的终端窗口,运行Sikuli OCR测试,然后再回到Selenium / Python测试。 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. 我把XXX放在我不知道新终端打开和运行Sikuli脚本的论据的地方。

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. 我已经搜索并阅读了我已经可以找到的内容,但是无法全部一起工作。

I ran into a similar issue, so I wrote a CPython module for Sikuli. 我遇到了类似的问题,所以我为Sikuli写了一个CPython模块。 The module is hosted on GitHub and available via pip install sikuli . 该模块托管在GitHub上 ,可通过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). 它可以使用pyjnius访问包含的Sikuli jar,因此您不必使用Jython甚至安装Sikuli本身(尽管我建议将其用于录制目的)。 The module currently covers most of the simpler Sikuli functions, so it should cover a lot of use cases. 该模块目前涵盖了大多数简单的Sikuli函数,因此它应该涵盖了很多用例。

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. 安装完成后, from sikuli import *一个简单from sikuli import *将帮助您入门,但作为最佳实践,我建议您仅导入要使用的功能。 This is particularly important for this module, because sikuli has a type function which overrides Python's own type function. 这对于这个模块尤为重要,因为sikuli有一个type函数,它覆盖了Python自己的type函数。

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. 为了从Selenium调用Sikuli代码,我的第一选择是TestAutomationEngr建议使用Java,因为Selenium和Sikuli都有本机Java绑定。

Since you want to use Python, you should try running Selenium under Jython. 既然你想使用Python,你应该尝试在Jython下运行Selenium。 It's important to remember that Sikuli is Jython , which is probably why you're not able to import it. 重要的是要记住Sikuli是Jython ,这可能是你无法导入它的原因。 (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. (另一个原因是你没有在Jython的模块路径中使用它。)我自己没有尝试过,但去年在Selenium中修复了一个错误 ,表明在Jython下它应该没问题。

Note that if you call your Sikuli code directly from Jython, you need to add 请注意,如果直接从Jython调用Sikuli代码,则需要添加

from sikuli.Sikuli import *

to the top. 到顶部。 This is because the Sikuli IDE implicitly adds that to all Sikuli code. 这是因为Sikuli IDE隐式地将其添加到所有Sikuli代码中。

Finally, your last resort is to call Sikuli from the command line. 最后,您的最后一招是从命令行调用Sikuli。 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. 您可能需要“无IDE”版本,您可以在其中调用Java并传入sikuli-script JAR文件。

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. 如果你的sikuli脚本是完全独立的,你只想运行一次然后控制回你的python脚本。 Then you can create a batch file, which calls your sikuli script and call this batch file from your python script instead. 然后,您可以创建一个批处理文件,该文件调用您的sikuli脚本并从您的python脚本调用此批处理文件。 Once the batch file is done running, it exits and returns the control back to your python script. 批处理文件运行完毕后,它将退出并将控制权返回给您的python脚本。

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: 从python中调用Sikuli脚本的代码片段:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM