RobotFramework - 将 selenium org.openqa.selenium 添加到我的 JavaLibrary 时出现 ClassNotFound 异常

RobotFramework - ClassNotFound Exception when selenium org.openqa.selenium is added to my JavaLibrary

请帮我解决这个问题。我按照 Java 中的以下步骤将此 jar 文件添加到 Robot 框架项目中。执行测试时,收到ClassNotFoundException。

Java 更正代码:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.WebElement;

public class StoreElements{
    public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
    public String storeTexts(List<WebElement> text){
    for(WebElement theElement : text){
     System.out.println("The Element class is: 
     "+theElement.getAttribute("class")+", text: " + theElement.getText());
 }
return ((WebElement) text).getText();
 }
} 

机器人框架代码:

*** Settings ***
Library  Selenium2Library
Library  keywords.kcc.StoreElements
*** Keyword ***
Get the customer Names
@{customers}    get webelements    xpath=//div[contains(@class,'name-column')]
store Texts  @{customers}

我需要在 Java 项目中包含 Selenium2Library 而不仅仅是 selenium 或者是否可以在 Java 中包含 Selenium2Library?

我收到的异常是,

[ ERROR ] Unexpected error: NoClassDefFoundError: 
org/openqa/selenium/WebElement
java.lang.NoClassDefFoundError: 
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.getMethods(Class.java:1615)
at robot.utils.importer$py.import_(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py:274)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py)
at 
robot.utils.importer$py._import_class_or_module(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py:77)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py)
at robot.utils.importer$py.import_class_or_module(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py:74)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py)
at robot.model.visitor$py.visit_suite(C:\jython2.7.0\Lib\site-
packages\robot\model\visitor.py:88)
at robot.model.visitor$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\visitor.py)
at robot.model.testsuite$py.visit(C:\jython2.7.0\Lib\site-
packages\robot\model\testsuite.py:161)
at robot.model.testsuite$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\testsuite.py)
at robot.model.itemlist$py.visit(C:\jython2.7.0\Lib\site-
packages\robot\model\itemlist.py:75)
at robot.model.itemlist$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\itemlist.py)
at robot.model.visitor$py.visit_suite(C:\jython2.7.0\Lib\site-
packages\robot\model\visitor.py:88)
at robot.model.visitor$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\visitor.py)
at robot.model.testsuite$py.visit(C:\jython2.7.0\Lib\site-
packages\robot\model\testsuite.py:161)
at robot.model.testsuite$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\testsuite.py)
at robot.utils.application$py._execute(C:\jython2.7.0\Lib\site-
packages\robot\utils\application.py:94)
at robot.utils.application$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\application.py)
at robot.utils.application$py.execute_cli(C:\jython2.7.0\Lib\site-
packages\robot\utils\application.py:49)
at robot.utils.application$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\application.py)
at runpy$py._run_code(C:\jython2.7.0\Lib\runpy.py:73)
at runpy$py.call_function(C:\jython2.7.0\Lib\runpy.py)
at runpy$py._run_module_as_main(C:\jython2.7.0\Lib\runpy.py:161)
at runpy$py.call_function(C:\jython2.7.0\Lib\runpy.py)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebElement
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 191 more

按照 Kootstra 的建议,添加了 selenium-server-standalone-3.3.1 jar 文件,目前 RED.xml 如下,这次红线没有显示在关键字 'Store Texts' 下。仅在执行此操作时抛出异常。也不是更长的异常消息。

控制台命令:

Command: C:\jython2.7.0\bin\jython.exe -J-Dpython.path=C:\jython2.7.0\Lib\site-packages -J-cp .;C:\Program Files\Java\jdk1.8.0_121\lib;C:\Program Files\Java\jdk1.8.0_121\jre\lib;;.;C:\Java\Jars_KCC\GetElementText.jar;C:\jython2.7.0\selenium-server-standalone-3.3.1 -m robot.run -P C:\jython2.7.0\Lib\site-packages\Selenium2Library -i SmokeTest --listener C:\Users\CON_RT~1\AppData\Local\Temp\RobotTempDir3926709749247362236\TestRunnerAgent.py:63501:False -s KCC_Automation.TestSuites.TestResource -t KCC_Automation.TestSuites.TestResource.logon to kcc with valid credentials -t KCC_Automation.TestSuites.TestResource.navigate to customer's page -t KCC_Automation.TestSuites.TestResource.display customer names -t KCC_Automation.TestSuites.TestResource.stop testing C:\Java\Workspace\KCC_Automation
Suite Executor: Robot Framework 3.0.2 (Jython 2.7.0 on java1.8.0_121)

我创建了新项目,然后仅将 'c:\jython2.7.0\selenium-server-standalone-3.3.1\' 与 Selenium2library 和自己的 java 库一起添加到类路径中。我还尝试在 jar 文件的末尾添加 .jar。它仍然有例外。但有一件事是现在它停止显示 ;;.;

现在是控制台命令,

Command: C:\jython2.7.0\bin\jython.exe -J-Dpython.path=C:\jython2.7.0\Lib\site-packages -J-cp .;C:\Java\JarsKCC\GetElementList.jar;C:\jython2.7.0\selenium-server-standalone-3.3.1.jar -m robot.run -P C:\jython2.7.0\Lib\site-packages\Selenium2Library -i SmokeTest --listener C:\Users\CON_RT~1\AppData\Local\Temp\RobotTempDir9154227905398549031\TestRunnerAgent.py:54485:False -s KCC_Automation.TestSuites.TestResource -t KCC_Automation.TestSuites.TestResource.logon to kcc with valid credentials -t KCC_Automation.TestSuites.TestResource.navigate to customer's page -t KCC_Automation.TestSuites.TestResource.display customer names -t KCC_Automation.TestSuites.TestResource.stop testing C:\Java\Workspace\KCC_Automation 
Suite Executor: Robot Framework 3.0.2 (Jython 2.7.0 on java1.8.0_121)

这次例外情况不同。 ClassCastException: org.python.core.PyObjectDerived 无法转换为 org.openqa.selenium.WebElement

错误在我看来非常直接:[ ERROR ] Unexpected error: NoClassDefFoundError: org/openqa/selenium/WebElement 它缺少这个 class。这是在常规 selenium-server-standalone-x.x.x.jar 中找到的 class。

在过去的几天里,您在 RED 和 Jython 的上下文中提出了一些问题,我假设这是您的开发环境。话虽这么说,如果碰巧不同,这些步骤仍然适用,但应用方式有所不同。

我已经将你的 java class 转换为 Jar 文件。在 RED 中,我创建了一个机器人项目并将其添加为库文件。在此 中,我详细介绍了该过程。

由于机器人脚本不完整,我对其进行了补充,现在看起来像这样:

*** Settings *** 
Library    StoreElements
Library    Selenium2Library

*** Test Cases ***
Get the customer Names
    @{customers}    Get Webelements    xpath=//div[contains(@class,'name-column')]
    Store Texts  ${customers}

需要添加库以确保关键字被识别,这意味着库被正确加载。

因为新的 Jar 文件引用了 Selenium 服务器 class 它需要能够访问它。这要求此 class 是 class 路径的一部分。在 RED 中,这可以通过将 selenium-server-standalone-x.x.x.jar 添加到 RED.xml 项目文件中的 class 路径来实现。

这应该看起来像这样:

尽管此机器人文件在 运行 时会失败,但如果您想在使用 Jython 和 Robot Framework红色:

C:\jython2.7.0\bin\jython.exe 
 -J-Dpython.path=C:\jython2.7.0\Lib\site-packages 
 -J-cp 
   .;
   C:\Eclipse\Workspace\JavaSelenium\ExampleLibrarySelenium.jar;
   C:\Eclipse\Workspace\ExampleJavaSelenium\selenium-server-standalone-3.3.1.jar 
 -m robot.run 
 -P C:\jython2.7.0\Lib\site-packages\Selenium2Library 
 --listener C:\Users\User\AppData\ ... \TestRunnerAgent.py:54540:False 
 -s JavaSelenium.Example 
 C:\Eclipse\Workspace\JavaSelenium

在上面的格式化命令中,class路径由-J-cp参数定义。添加的自定义 Java 机器人库 ExampleLibrarySelenium.jar 和额外需要的依赖项 selenium-server-standalone-3.3.1.jar 补充了标准 .,它指的是 jython.exe 所在的文件夹。

虽然将 jar 文件添加到现有的 . 文件夹也可以,但最好为每个项目添加所需的文件,以确保添加正确的版本,同时也允许本地实验。