2
votes

I'm a newbie for Robot Framework, working on using Robot Framework with Selenium and java keywords. I know RF is based on python, however, the project demands using java keywords.

My thought is that creating java functions and text file, then use:
java -jar robotframework-2.8.x.jar <..>.txt

For example, java function is using selenium webdriver to open browser, the problem is that I don't know how to combine the java function to text file. I tried but the error is "No Keyword found". If my java function is called openBrower, how should I define keywords in text file?

Can anyone provide me a very simple example of RF with selenium and java keywords? Thanks a lot!

3
You could run Java class from Robot Framework test case using Run And Return Rc function. I will provide example in couple of minutes.Kirill

3 Answers

2
votes

Just try to run your Java class using Run function with Robot Framework like this:

CommonResource.robot:

# This file is located in "robot" folder

*** Settings ***
Documentation   CommonResource file with KWs
Library         OperatingSystem

*** Variables ***
${SRC_PATH}    ../../src/

*** Keywords ***

Compile Class
    [Arguments]  ${class_name}  ${path}=${SRC_PATH}
    Run    javac ${path}${class_name}.java

Run Java Class
    [Arguments]  ${class_name}  ${path}=${SRC_PATH}
    Compile Class  ${path}  ${class_name}
    ${output}=   Run    java -cp ${path} ${class_name}
    Log     ${output}   WARN

Test.java:

/**
 * This file is located in "src" folder
 */
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

TestCase.robot:

# This file is located in "robot/Tests" folder
*** Settings ***
Documentation  Running Java class with RF
Resource       ../CommonResource.robot

*** Testcases ***

TestOne
    Run Java Class  Test
0
votes

I haven't used RF with Selenium, but I am using it to test an HTTP based API so hopefully you can use this example and apply it to your situation.

There are a few parts to this. The first is that you need to build your Java class, and have it included on the classpath, followed by using it in your script. I had a gotcha here because for some reason RF didn't like reading Java code unless it is in a JAR file. I'm not sture why, maybe it's a Jython issue. I used a wrapper Gradle script to handle dependencies/build of my Java library (since I also have a few compile time deps), that generates a very basic shell script to run the tests.

src/main/java/com/mypackage/Foo.java

package com.mypackage;

public class Foo {
  public void methodNameIsKeyword() {
    // ...
  }
}

build.gradle

apply plugin: "java"

repositories {
  mavenCentral()
}

dependencies {
  runtime "org.robotframework:robotframework:2.8.5"
}

task writeScript() {
  def classpath = sourceSets.main.runtimeClasspath + files("build/libs/mylibrary.jar")
  println "java -cp ${classpath.asPath} org.robotframework.RobotFramework \$@"
}

task wrapper(type: Wrapper) {
  gradleVersion = "1.10"
}

The next step is to use your new keyword in your script

*** Settings ***

Library    com.mypackage.Foo # constructor args go here.

*** Testcases ***

It should use the Java class
    Method Name Is Keyword 

Of course you class can take Constructor args, and have parameters/return values from your methods.

0
votes

Even I had the similar situation, where I had to use Java with Robotframework. The linking of keywords is possible by using the Robot Selenium2library for Java.

You can also create your custom keywords and then link them to the Java Classes for their respective implementation.

Try looking at the project below

https://github.com/mskumar1809/StraitTimesAppiumRobot

which provides java implementation for Robot keywords.