2

Please help me on this situation. I followed the below steps in Java and Added this jar file into Robot framework project. When executing the test, I received ClassNotFoundException.

Java Corrected Code:

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();
 }
} 

Robot Framework Code:

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

IS that Something like i need to include Selenium2Library in Java Project rather than just selenium Or is it possible include Selenium2Library in Java?

The Exception that i received is,

[ 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

As suggested by Kootstra, added selenium-server-standalone-3.3.1 jar file and currently the RED.xml is as below, This time red line is not shown under keyword 'Store Texts'. The exception is thrown only when executing this. And not the longer exception message too. enter image description here

Console Command:

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)

I created new project, then added only the 'c:\jython2.7.0\selenium-server-standalone-3.3.1\' into classpath along with Selenium2library and own java library. I also tried adding .jar at the end of the jar file. It still has exception. But one thing is that now it stopped showing ;;.;

Here is the console command now,

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)

This time the exception is different. ClassCastException: org.python.core.PyObjectDerived cannot be cast to org.openqa.selenium.WebElement

Roja
  • 293
  • 1
  • 5
  • 21

1 Answers1

2

The error is in my view quite straight forward: [ ERROR ] Unexpected error: NoClassDefFoundError: org/openqa/selenium/WebElement it is missing this class. This is a class found in the regular selenium-server-standalone-x.x.x.jar.

In the past couple of days you've asked some questions in context of RED and Jython, and I'm going to assume that this is your development environment. That being said, if it happens to be different the steps would still apply but applied differently.

I've taken your java class and converted it to a Jar file. In RED I've created a robot project and added it as a Library file. In this StackOverflow response I've detailed the process step-by-step.

Because the robot script wasn't complete, I've complemented it so that it now looks like this:

*** Settings *** 
Library    StoreElements
Library    Selenium2Library

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

The addition of the Libraries is needed to ensure the keywords are recognized and that means the libraries are loaded correctly.

Because the new Jar file refers to the Selenium Server class it needs to be able to access it. This requires that this class is part of the class path. In RED this can be achieved by adding the selenium-server-standalone-x.x.x.jar to the class path in the RED.xml project file.

This should then look like this:

enter image description here

enter image description here

Although this robot file will fail when run, the generated command might be of some interest if you want to specify the java path from the command line when working with Jython and Robot Framework in RED:

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

In the above formatted command, the class path is defined by the -J-cp argument. The added custom Java Robot library ExampleLibrarySelenium.jar and the additionally required dependency selenium-server-standalone-3.3.1.jar complement the standard . which refers to the folder where jython.exe is located.

Although adding the jar files to the existing . folder would have worked too, it is better to add the required files per project, in order to ensure that the right version is added but also to allow for local experimentation.

Community
  • 1
  • 1
A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
  • Your assumption is correct. My development environment is RED and Jython. The missing part from my end is not added the jar file selenium-server-standalone-3.3.1.jar It is been added now. But still failed. Also updated the question with current set up in my project. Please check and correct me. – Roja Mar 19 '17 at 12:23
  • Can you share the command RED generates? The jar file needs to be part of the java class path that is defined there. As I mentioned before, if you put the jar files in the same folder as `jython.exe` it should work as well. This is not a recommended setup but a working setup makes it easier to change to a better setup then a non-working one. – A. Kootstra Mar 19 '17 at 12:35
  • Attached the screenshot of the RED.xml with the error shown on the execution. jython.exe is available in the path C:/jython2.7.0/ where i have updated the selenium-server-standalone-3.3.1.jar. – Roja Mar 19 '17 at 13:07
  • Although the screenshot didn't provide me with the console command used to start Robot Framework, the screenshot did show me the path you added to the java class as `c:/jython2.7.0/selenium-server-standalone-3.3.1` which is a directory, I assume. If that is the case it should be replaced by `c:\jython2.7.0\selenium-server-standalone-3.3.1\ ` and your setup should work. – A. Kootstra Mar 19 '17 at 14:27
  • Added the console command into the question. Also i replaced the said path and refreshed and re-executed the same. But still its not obeying me. – Roja Mar 19 '17 at 14:54
  • Had a look and although I suspected that `;;.;` was the culprit, it did no harm in my own example. Which leaves me with little to offer other than to advise you to create a simple new project with just the selenium jar file and your own. Start simple, just your own jar file without the selenium dependency. Then extend with the dependency. From your screenshot I also gather that you have the CLASSPATH environment variable set. Perhaps for testing purpose you may want to disable that. Adding an _ to the name does the trick generally. – A. Kootstra Mar 19 '17 at 15:24
  • It was my initial try having a simple java program from the link [link]http://stackoverflow.com/questions/42384822/it-errors-when-specifying-the-user-defined-java-library-into-red-robot-framework and integrating with RF. Also it worked successfully. When comes to class org.openqa.selenium it doesnt work. As specified I removed classpath file and renamed the jar file folder. And have the same ;;.; in the console command. – Roja Mar 19 '17 at 16:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138473/discussion-between-a-kootstra-and-roja). – A. Kootstra Mar 19 '17 at 16:34
  • I specified the output after the RED update in the chat. Updated the same in question too. – Roja Mar 19 '17 at 17:41