请帮助我解决这种情况。我在 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_$32(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$5(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$4(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$2(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$19(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$11(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$2(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$19(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$10(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$5(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$9(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$14(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 如下所示,这次红线未显示在关键字“存储文本”下。只有在执行此操作时才会引发异常。而不是更长的异常消息。
控制台命令:
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