Unable to load the Selenium jar file in the common file

Asked by Maria Haris on 2018-03-14

I am working on automating one of the web module of windows based Application.
Windows client has automaleted scripts in the sikuli ide. There is a common file which has all the functions which are called quite often by various modules(Web and win).
I have been able to import the selenium jar file and automate a few web modules. In each folder of the webmodule test case I have to add the selenium .jar file , which isn't very efficient way of doing things.And also there is a lot of redundant code in the files e.g the following lines have to be added in all the web module test cases :
import org.openqa.selenium.chrome.ChromeDriver as CD
import org.openqa.selenium.chrome.ChromeOptions as CO
import org.openqa.selenium.chrome.ChromeDriverService as CS
import org.openqa.selenium.remote.DesiredCapabilities as DC
import org.openqa.selenium.By as By
import org.openqa.selenium.interactions
import org.openqa.selenium.support.ui.Select as Select
import org.openqa.selenium.support
import org.openqa.selenium.WebDriver as WD
import org.openqa.selenium.WebElement
import java.util.concurrent.TimeUnit as TimeUnit
import java.util.Arrays as Arrays
import java.lang
import java.lang.System as System

options = CO()
System.setProperty('webdriver.chrome.driver', 'Chromedriver.exe')
capability.setCapability('chrome.switches', Arrays.asList('–disable-extensions'))
capability.setCapability('chrome.binary', 'C:/Users/user_name/AppData/Local/Google/Chrome/Application/chrome.exe')
options.addArguments('user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/Default')
driver = CD(capability)
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)

I wanted to move the above code into the common file.
Unfortunately I cant do that without moving the selenium jar file in the common folder as well.Not only that I have to move the Selenium jar file in all the modules' folders which are using the common file regardless of them being web based or windows based.and we have 100's of folders with the sikuli script. It wouldn't be wise to move the selenium. jar to all of those folders.

I wonder why does it have to be this way? Is this a bug related to sikuli ? Or was it by design :D

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Maria Haris
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

ok, understood.

This is how modularized Python works.
Since every module has its own namespace, you have to make all external names known in this module. In your case the feature is import.

But you are on a not necessary complex way: Since you are using the Jython interpreter, you can use jars on the Java classpath directly, no need to use load(), to get the jar on the Python path.
The load() feature is there for jars with mixed Java and Python content.

This leaves you with the imports only.

The next step would be to hide the detailed Selenium stuff needed in every module, by concentrating it to one module (what you apparently already decided to do), that has the imports and def's to be used in the other modules for steps, that are repeated more than once.

BTW: In this scenario I do not understand the decision for Python scripting. A clean Java solution would be much more straight forward.

Maria Haris (mariaharis) said : #2

So you mean maybe adding the folowing lines to the runsikulix.cmd should work for loading jar file
echo +++ trying to run seleniumserver
echo +++ using: %PARMS% -jar selenium-server-standalone-3.0.1.jar %SIKULI_COMMAND%

RaiMan (raimund-hocke) said : #3

I do not really understand what and how you are doing your stuff.

You are talking about Eclipse (so I think it should be PyDev), but in comment #2 i see runsikulix.cmd.

Why don't you run the stuff from inside Eclipse for development and testing?

the Selenium jar must be on the Java class path, which is the parameter -cp for the java command.

All this is basic knowledge and has nothing to do with SikuliX.

Maria Haris (mariaharis) said : #4

The Eclipse question was a separate task for my personal experimentation. Where as this question was refered to how things are going at my work place. We are not using any other ide other than sikuli for automation of the the windows application.

RaiMan (raimund-hocke) said : #5

ok, then you have to modify the java command in the runsikulix.cmd

"%JAVA_HOME%\bin\java.exe" %PARMS% -jar "%SIKULIX_HOME%%SJAR%.jar" %SIKULI_COMMAND%

"%JAVA_HOME%\bin\java.exe" %PARMS% -cp <your-selenium-jar> -jar "%SIKULIX_HOME%%SJAR%.jar" %SIKULI_COMMAND%

Maria Haris (mariaharis) said : #6

It is still not working.
I remove the load selemium code from the test case it fails to import the openqa.

[error] script [ SDK_Service ] stopped with error in line 2
[error] ImportError ( No module named openqa )

RaiMan (raimund-hocke) said : #7

ok, I have to admit: does not work this way - sorry for misleading.

Finally did my own experiments now and this is the solution:
(from: http://sikulix-2014.readthedocs.io/en/latest/scenarios.html#access-python-packages-from-sikulix-scripts-run-by-sikulix-gui-or-commandline)

create a sites.txt file that contains the absolute path to your selenium...jar file and place it according to the above doc/approach 2

Then just run your scripts just with the (unmodified) runsikulix.cmd

Since the selenium....jar now automatically gets on to sys.path, the imports will work without any other additional handling.

Maria Haris (mariaharis) said : #8

worked like a charm! thankyou RaiMan