Calling to a Sikuli script from Python (Selenium) - python

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.

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.

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

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.

Related

Can manim be used in pycharm?

I have been programming with python for about half a year, and I would like to try manim ( the animation programme of 3blue1brown from youtube), but I am not sure where to start. I have not installed it, but I have tried to read up on it. And to be honest I do not understand much of the requirements of the program, and how to run it.
Google has left me without much help, so I decided to check here to see if anyone here is able to help.
From what I understand, you run manim directly in python and the animations are based on a textfile with code i assume is LaTex. I have almost no experience with python itself, but I have learned to use it through Thonny, and later Pycharm.
My main questions are: (Good sources to how to do this without being a wizard would be really helpful if they exist☺️)
Is it possible to install manim in pycharm, and how? Do i need some extra stuff installed to pycharm in order to run it? (I run a windows 64-bit computer)
If i manage to do this in pycharm, Will I then be able to code the animations directly in pycharm (in .py or .txt files), or is it harder to use in pycharm?
All help or insights is very appreciated😅 As I said I am not extremely knowledgeable in computers, but I am enjoying learning how to code and applications of coding😊
I recommend you this playlist
I always uses pycharm for manim.
Firstly i setup python interpreter by just open File->Settings->Projet->Project Interpreter then just press on little gear icon to add python interpreter to Existing environment and locate C:\Python3x\python.exe
Then just open a terminal from left-down corner and run some basic commands to run manim as mentioned in tutorials or manim github page.
Something that works nicely for me is to run manimgl.exe from Python in PyCharm using the subprocess module. It also goes well with using the run shortcut while iterating with small edits.
I like to do this from the script in which my main scene is defined, for example, I have main.py which defines MyScene:
from manimlib import *
class MyScene(Scene):
def construct(self):
...
if __name__ == '__main__':
import subprocess
params = 'manimgl main.py MyScene -c WHITE'.split()
subprocess.run(params,
check=True,
capture_output=True,
text=True)
# Possibly look at captured output here
The code inside if __name__ ... does not execute when the same script is loaded by manim. What is nice is that one can easily add automation steps before or after the actual execution if needed and it keeps everything related in a single script.
Edit: I also end the animations in the construct() method of MyScene with exit() to terminate the preview. I honestly don't know if this is good practice, but it works well for my usage pattern.
Note that this does require that manimgl.exe reside somewhere that is in your path, in my case (Windows) I installed this for my global Python interpreter. I followed the instructions on GitHub and it works for me because the following is in my path:
C:\Users\XXX\AppData\Local\Programs\Python\Python38\Scripts\
It may vary depending on where your Python is installed. For a venv, you could do something like:
params = '.\venv\Scripts\manimgl.exe ...'.split()
Yes, you can
1.Write your code in pycharm
2.save it
3.copy that .py file to where you installed manim. In my case, it is
This pc>> C drive >> manim-master >> manim-master
4.select on the path and type "cmd" to open terminal from there
Type this on the terminal
python -m manim -pql projectname.py
This will do.
To play back the animation or image, open the media folder.

How do I run python libraries specific to command prompt on IDLE?

I wish to run some commands which are specifically made for command line interface in idle environment.
There is a library in python called "Ezflix" which is for streaming torrent videos.
It runs properly on command line interface but does not work when I run it on python idle.
I know that command line commands cant be used in idle but I just wish to find if there is any possibility or any hack to make it run on idle.
According to https://pypi.org/project/ezflix/, ezflix is a "command line utility", one which happens to be written in Python. At this level, it is intended to be run from a command line terminal/console.
Such a program, even if written in python, might not be a Python library module, meaning that it is not intended that you import it and directly access its functions. If this is true, it would not have a supported and documented application program interface (API). If so, one could read the code and import it anyway, but the private internal objects and names might change from version to version. So the best way to access it from a Python program would be to run it separately, for instance, with subprocess, as suggested in the comments.
It turns out the ezflix does have a documented API and so it is also a library module. The is briefly described at the bottom of the pypi page linked above.
from ezflix import Ezflix
ez = Ezflix(<arguments>)
...
I presume that the package itself contains more information on its usage.
None of the above has anything to do with whether you run your program directly with python or with IDLE or with any other IDE. What could matter is whether the ezflix user interface specifically requires that it be run connected to the system terminal/console. Noting I saw on its pypi page suggests this. It might also be that the movie player window somehow interferes with the IDLE GUI window, but I also do not expect this.

How can I run a Jython script with Python

I am programming in Python(2.7), processing a bunch of data.
And I've got a software, what I have to use, and I want to start it automatically, and fill it up with data.
The problem is, that I cant open it with Python, because it has API only for Jython.
My question is, that how could I run a Jython script from a Python code(actually I am working on a standalone software)?
Is it even possible?
If it is, could you please give me a short example?
How to install Jython and how to run a file from python?
You can simply use Jython to run everything.
Having my_script.py and jython_script.py edit my_script.py by adding import jython_script and adding call jython_script.some_function().
# my_script.py
import jython_script
def my_function_using_some_function_from_jython_script():
...
jython_script.some_function()
...
Then simply call:
jython my_script.py
I assume you do not use modules that work only with CPython.

Running command lines within your Python script

So I have a bunch of aliases and Command Line prompt programs, and my main program works by inputting b into the cmd.exe, followed by some filepath names and what not. How would I run those arguments in my python script? So that it mimics the action i am doing in the cmd?
You should use the subprocess module. In particular, subprocess.call will run command line programs for you.
or you can use
import os
os.system('your_command')
for example:
import os
os.system('notepad')
will launch the notepad with the command line behind.
hope this helps
You can do this using subprocess
For example, this call bellow gets the output of the program and stores it as a string, using .call will help with calling it and for more accurate control use .Popen
subprocess.check_output(["ipconfig"])
Check out Sarge - a wrapper for subprocess which aims to make life easier for anyone who needs to interact with external applications from their Python code. and Plumbum - a small yet feature-rich library for shell script-like programs in Python.

Is it possible to use batch scripts in a GUI made with Python?

I was wondering if it was possible to write a GUI in python, and then somewhere in the python script, insert a script switch to temporarily change the language to accomodate for the batch snippet.
I know this can be done in html and vbscript but what about Python?
You can control other processes, written with any language, including bash using the subprocess module.
The subprocess module is the most powerful and complete method for executing other processes. However, there's also a very simple method using the os module: os.system(command) runs command just as if you were to type it into a command line.

Categories