使用 Jython 在 Robot Framework 中使用自定义 Java keywords/library
Using custom Java keywords/library in Robot Framework using Jython
我正在尝试将我自己的 Java 库导入 RFW 以使用关键字。导入硒工作正常。我已将 java class 导出到 .jar。
我尝试过的事情:
1)
将 .jar 和 .robot 文件放在同一文件夹中:
λ jython -m robot testcase1.robot
将 MyTestLib.jar 粘贴到 C:\Program Files\jython2.7.2\Lib\site-packages 和 运行:
λ jython -m robot testcase1.robot
jython -Dpython.path=C:\Users\User\eclipse-workspace\testrobot\src\test\robotframework\acceptance\ -m robot testcase1.robot
- 将我的 CLASSPATH 环境变量设置为 C:\Users\User\eclipse-workspace\testrobot\src\test\robotframework\acceptance
和 运行
λ jython -m robot testcase1.robot
每次我得到同样的错误:
[ ERROR ] Error in file 'C:\Users\User\eclipse-workspace\testrobot\src\test\robotframework\acceptance\testcase1.robot' on line 5: Importing library 'MyTestLib.jar' failed: NoClassDefFoundError: com/company/testautomation/testrobot/MyTestLib (wrong name: MyTestLib)
我做错了什么?
这是我的 java class:
public class MyTestLib {
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
public void hello(String name){
System.out.println("Hello " + name + ".\n");
}
public void Nothing(){
}
}
这是我的测试套件:
*** Settings ***
Documentation This is a test file to test RFW
...
Library SeleniumLibrary
Library MyTestLib.jar
*** Test Cases ***
Testcase0
Log This is testcase 1.robot Starting logging
Open Browser https://google.com chrome
Set Browser Implicit Wait 1
Click Button //button[.//text() = 'Ik ga akkoord']
Sleep 1
Input Text name=q Company
Press Keys name=q \ue00c
Press Keys name=q \ue007
Sleep 1
Close Browser
Log Test completed
MyLibTest
Log My lib test is starting
Nothing
我认为我正确安装了所有要求:
C:\
λ jython --version
Jython 2.7.2
C:\
λ java --version
java 11.0.10 2021-01-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.10+8-LTS-162)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.10+8-LTS-162, mixed mode)
C:\
λ jython -m robot --version
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.python.core.io.StreamIO (file:/C:/Program%20Files/jython2.7.2/jython.jar) t
o field java.io.FilterOutputStream.out
WARNING: Please consider reporting this to the maintainers of org.python.core.io.StreamIO
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Robot Framework 4.0.3 (Jython 2.7.2 on java11.0.10)
C:\
λ jython -m pip list
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.python.core.io.StreamIO (file:/C:/Program%20Files/jython2.7.2/jython.jar) t
o field java.io.FilterOutputStream.out
WARNING: Please consider reporting this to the maintainers of org.python.core.io.StreamIO
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
DEPRECATION: A future version of pip will drop support for Python 2.7.
Package Version
------------------------------ -------
pip 19.1
robotframework 4.0.3
robotframework-seleniumlibrary 4.2.0
selenium 3.141.0
setuptools 41.0.1
urllib3 1.26.5
尝试在 CLASSPATH
envvar 中添加“MyTestLib.jar”的完整路径,例如:
export CLASSPATH=\path\to\MyTestLib.jar
使用Windows等价物,当然
我使用了 .class 文件而不是 .jar,这似乎解决了问题,但不知道为什么 .jar 不起作用。
我正在尝试将我自己的 Java 库导入 RFW 以使用关键字。导入硒工作正常。我已将 java class 导出到 .jar。 我尝试过的事情:
1) 将 .jar 和 .robot 文件放在同一文件夹中:
λ jython -m robot testcase1.robot
将 MyTestLib.jar 粘贴到 C:\Program Files\jython2.7.2\Lib\site-packages 和 运行:
λ jython -m robot testcase1.robot
jython -Dpython.path=C:\Users\User\eclipse-workspace\testrobot\src\test\robotframework\acceptance\ -m robot testcase1.robot
- 将我的 CLASSPATH 环境变量设置为 C:\Users\User\eclipse-workspace\testrobot\src\test\robotframework\acceptance 和 运行
λ jython -m robot testcase1.robot
每次我得到同样的错误:
[ ERROR ] Error in file 'C:\Users\User\eclipse-workspace\testrobot\src\test\robotframework\acceptance\testcase1.robot' on line 5: Importing library 'MyTestLib.jar' failed: NoClassDefFoundError: com/company/testautomation/testrobot/MyTestLib (wrong name: MyTestLib)
我做错了什么?
这是我的 java class:
public class MyTestLib {
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
public void hello(String name){
System.out.println("Hello " + name + ".\n");
}
public void Nothing(){
}
}
这是我的测试套件:
*** Settings ***
Documentation This is a test file to test RFW
...
Library SeleniumLibrary
Library MyTestLib.jar
*** Test Cases ***
Testcase0
Log This is testcase 1.robot Starting logging
Open Browser https://google.com chrome
Set Browser Implicit Wait 1
Click Button //button[.//text() = 'Ik ga akkoord']
Sleep 1
Input Text name=q Company
Press Keys name=q \ue00c
Press Keys name=q \ue007
Sleep 1
Close Browser
Log Test completed
MyLibTest
Log My lib test is starting
Nothing
我认为我正确安装了所有要求:
C:\
λ jython --version
Jython 2.7.2
C:\
λ java --version
java 11.0.10 2021-01-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.10+8-LTS-162)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.10+8-LTS-162, mixed mode)
C:\
λ jython -m robot --version
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.python.core.io.StreamIO (file:/C:/Program%20Files/jython2.7.2/jython.jar) t
o field java.io.FilterOutputStream.out
WARNING: Please consider reporting this to the maintainers of org.python.core.io.StreamIO
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Robot Framework 4.0.3 (Jython 2.7.2 on java11.0.10)
C:\
λ jython -m pip list
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.python.core.io.StreamIO (file:/C:/Program%20Files/jython2.7.2/jython.jar) t
o field java.io.FilterOutputStream.out
WARNING: Please consider reporting this to the maintainers of org.python.core.io.StreamIO
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
DEPRECATION: A future version of pip will drop support for Python 2.7.
Package Version
------------------------------ -------
pip 19.1
robotframework 4.0.3
robotframework-seleniumlibrary 4.2.0
selenium 3.141.0
setuptools 41.0.1
urllib3 1.26.5
尝试在 CLASSPATH
envvar 中添加“MyTestLib.jar”的完整路径,例如:
export CLASSPATH=\path\to\MyTestLib.jar
使用Windows等价物,当然
我使用了 .class 文件而不是 .jar,这似乎解决了问题,但不知道为什么 .jar 不起作用。