Jython: Import Modules From Other Sources (DB for instance)? - module

I'm using a Java program to load and run Jython scripts - using the org.python.util.PythonInterpreter.
I'm storing the Jython scripts in a database : currently I'm having to extract the Python scripts to a file system prior to running them - to ensure that any 'import' statements within the scripts work.
Is there a way of avoiding this extraction step: that is - is there a way to hook into the Python interpreter to intercept the imports and call out to a Java Method (which would load the jython source from the DB) ?

You can add importers from either Python or Java (there's a standard one in the Jython code which imports from the classpath: org.python.core.ClasspathPyImporter; there's also some Javadocs in the org.python.core.util.importer interface it implements which may be useful). The code is relatively simple; see PEP 302 as well.

Related

Running remote Pycharm interpreter with tensorflow and cuda (with module load)

I am using a remote computer in order to run my program on its GPU. My program contains some code with tensorflow functions, and for easier debugging with Pycharm I would like to connect via ssh with remote interpreter to the computer with the GPU. This part can be done easily since Pycharm has this option so I can connect there. However, tensorflow is not loaded automatically so I get import error.
Note that in our institution, we run module load cuda/10.0 and module load tensorflow/1.14.0 each time the computer is loaded. Now this part is the tricky one. Opening a remote terminal creates another session which is not related to the remote interpreter session so it's not affecting remote interpreter modules.
I know that module load in general configures env, however I am not sure how can I export the environment variables to Pycharm's environment variables that are configured before a run.
Any help would be appreciated. Thanks in advance.
The workaround after all was relatively simple: first, I have installed the EnvFile plugin, as it was explained here: https://stackoverflow.com/a/42708476/13236698
Them I created an .env file with a quick script on python, extracting all environment variables and their values from os.environ and wrote them to a file in the following format: <env_variable>=<variable_value>, and saved the file with .env extension. Then I loaded it to PyCharm, and voila - all tensorflow modules were loaded fine.

Running a single feature file before and after a test run

Was looking for a solution to run a feature file at the end of the suite
My workflow (In parallel Run)
karate.callSingle('Login.feature') so at the beginning i do one
login and then use the cookies/token for the whole suite
Run tests in Parallel
Runs the Logout.feature file
There is no direct support for this currently. By the way no one has ever requested this. If this is so important, kindly open a feature request.
One workaround is to set a singleton / Java static variable from callSingle and then in your JUnit / Java parallel runner, call the feature to logout using the Java API (search the docs for this) and you can pass arguments / access the static variable.
EDIT: just realized that the #AfterClass JUnit annotation may be more than sufficient for your needs.

Connect Jmeter to Redis with Beanshell

I want to connect Jmeter to Redis DB, I want to do it via java programming.
I added jedis-2.2.1.jar file to lib folder.
and create a test plan with only bean-shell preprocessor.
I can not understand what I can see, since nothing happened, and the response tree is blank,
Can someone please advise how to connect to redis via jmeter (please without the redis plugin)
Provided the Pic of the program, it is a simple program just want to connect.
** I am new in java scripting in Jmeter and the only jar I added is jedis.jar, the program is a script from the net. not created thread group in the test plan
with void main it is not worked also
You need to add a Sampler to your Test Plan. PreProcessors are executed before samplers, single PreProcessor won't do any work as it will simply not be executed. So you either need to add a Sampler to your test plan or convert your PreProcessor to be a Sampler
Since JMeter 3.1 it is recommended to use JSR223 Elements and Groovy language for any form of scripting. The reasons are in:
Groovy performance is much better as it is capable of compiling scripts and caching them
Groovy fully supports Java syntax, valid Java code most likely will be valid Groovy code while with Beanshell you are stuck with Java 5 language level
Groovy provides many enhancements on top of Java SDK
See Apache Groovy - Why and How You Should Use It article for more information, benchmarks, examples of real-life Groovy usage, etc.
The solution is to use bean shell sampler and not pre-processor to see response.
Here is a JMeter file and beanShell Sampler script to fetch a set of keys from redis and put them into variables used by a looping HTTP GET request.
https://bitbucket.org/barryknapp/shared/src/d62f8ebb57ede1d15a3bd7683adfdd02cd039369/jmeter/?at=master

jython and jython-apt

i am trying to get jython running with the apt-module from python.
I have standard python2.7 installed as well as jython2.7. When running import apt in python everything is ok, but when running it in jython the module is not found. When extending the sys.path to the python2.7-installation like /usr/lib/python2.7/dist-packages the apt-package is found but it complains about not finding the apt-pkg-package (which is just some simple .so-file and not a real "package" in python).
What can be done here?
apt_pkg is required by apt, and only implemented as a binary module. Unfortunately, jython cannot load these, so you're out of luck. You can either port apt_pkg to Java (you can simply create a shim with JNI), to pure Python, or use another mechanism (for example calling an apt frontend on the commandline and parsing its outputs).

Selenium: How to read values from any file (text, excel or csv)?

Some of my project data needs to be changed every time i run the script. In order to have automated system i require to read values from any file and send its data as input to selenium script.
If you are using Selenium IDE, export your script to JUNIT ( java), then create a java project using that java file, then download the Selenium server library (jar file) and add to your project. then start the selenium server and run your script it should run successfully.
If it is working then you can simply use a java code to read files and set the values in the relevant places in your code.
ex: if you are using excel you can use jxl.jar library
You may refer my blog for more details. http://hrftools.blogspot.com/
From your description, it seems like you need to run the selenium script for different values and values have to be read from a file. You can use testNG's dataprovider to achieve this very easily.