2

I'm trying to work with simple Java function as below and calling this library into Robotframework. It throws me below error. Related issues in the blogs are not specific to RF. Someone please help me how to handle this.

import org.openqa.selenium.WebElement;

public class GoForward {
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
public WebElement moveForward(WebElement element){
    element.click();
    return element;
}
}

Robot Framework Code:

 *** Settings ***
Library  Selenium2Library
Library  keywords.Storetexts
Library  keywords.GoForward
*** Keyword ***
Click Forward Button
  move forward  ${forward_button}
  sleep  2s

Error:

TypeError: moveForward(): 1st arg can't be coerced to org.openqa.selenium.WebElement
Roja
  • 293
  • 1
  • 5
  • 21
  • The error says what's wrong - the method's argument should be of WebElement type, but you're passing something different. What's the value of `${forward_button}`, how do you set it? – Todor Minakov Mar 21 '17 at 05:50
  • {forward_button} - This is nothing but the variable that i have created for the id of the element(id=forward-button). An element locator. – Roja Mar 21 '17 at 06:06
  • 1
    And that's the root of the issue - you're passing a locator string, while the method expects an WebElement object. Before executing the click() in the Java, you have to find and transform it. You know, it will be much easier if you create that keyword in robotframework syntax. – Todor Minakov Mar 21 '17 at 06:16
  • To my understanding i have to pass the element in the java code itself. Am i correct? Else could you provide me an example. This would be great as I'm the beginner of RF. – Roja Mar 21 '17 at 06:20
  • I do have the RF syntax for this particular functionality. Since I'm learning RF integrated with Java Library, I tried a simple code to access an element via Java Library. If this works, I will be able to handle further logics with my products. Kindly help me on this. – Roja Mar 21 '17 at 06:43
  • 1
    By the way it looks to me, I guess in your move forward function you are waiting for a WebElement and here you are sending a string locator.. You can change the argument of your function to 'String', which you can use to locate your WebElement.. – Waman Mar 21 '17 at 06:50
  • 1
    Yes, a locator is a string by definition; robotframework supports both locators (as strings) and WebElement objects when you use the Selenium2Library. If you want to transform a string to the later, use the keyword `Get Webelement`. But then, you are going to run into another trouble - if you want to have your keywords in java, you have to get the instance of the selenium library there, and reuse - so your keywords interact within the same SE session. And that is a different topic - very well explained in the documentation. – Todor Minakov Mar 21 '17 at 09:29
  • 2
    Don't go that way, unless if you have a compelling reason to. Robotframework is highly powerful and versatile framework, 98% of the actions you want to do can be accomplished with the builtin functionality, or the standard libraries. And the browser interaction for sure is much easier if you do it in RF syntax using the standard lib, than going through java; especially in the trivial tasks like this one. – Todor Minakov Mar 21 '17 at 09:31
  • So from what I understand is if i pass" ${forward-button} id=forward-button " under variables section, then this variable is considered as string. Else if I make it ${Move-forward} get webelement ${forward-button} , then ${Move-forward} is considered as WebElement. Am I correct? Thanks Todor and Waman! – Roja Mar 21 '17 at 10:04
  • I agree with your point Todor! Thanks! As you said, Im gonna make the syntax using RF builtin Functions. I have Proof of Concept meeting in few days and i have to proove myself with RF using Java with simple coding. Hence I'm having such situation. – Roja Mar 21 '17 at 10:08
  • I corrected my RF code like this, Will this work? `Click Forward Button ${Move_Forward}= get webelement ${forward_button} move forward ${Move_Forward}` or I should still code it in Java? I have all set ups done in Java project with respect to selenium2library. – Roja Mar 21 '17 at 11:21
  • 2 Things! #1 If you want to showcase usage of Java libraries in RF in your meeting you could send the string argument to java function and then do findElementById/name from Java. #2 Otherwise ideally it is always good to use RF builtIn functions which will do the same thing i.e. Click Element ${forward_button} something like this will do. – Waman Mar 22 '17 at 03:59

0 Answers0