2

I'm trying to set up a maven+robot+selenium3+testNG project using Eclipse and java.

So far, I've created a maven project and added the dependencies and plugins to its pom.xml so I don't have to manually download anything. I added the following dependencies: selenium-java, selenium-server (not sure if I need it), testng, robotframework, robotframework-maven-plugin, robotframework-selenium2library-java, and also the robotframework-maven-plugin.

I also added a tests.robot file under src/test/robotframework/acceptance with this test:

*** Settings ***
Library  Selenium2Library


*** Test Cases ***
Visit google
  Open Browser  https://www.google.com  chrome
  Close Browser

However, I'm not sure how to tell robot where my chromedriver is. Currently, when I run as maven-install, I get "java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;

I read somewhere that I should create my own keyword and then use System.setProperty("webdriver.chrome.driver", "path\\chromedriver.exe"); but I don't really know how to create my own keywords in java, and how to connect them with robot.

1
  • I used Set Environment Variable webdriver.gecko.driver ${CURDIR}${/}drivers${/}geckodriver.exe but the program displays this error WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
    – Amr khamis
    Commented Apr 15, 2020 at 12:01

3 Answers 3

2

I actually found out how to do it:

I created a .java file within src/main/java/mycompany and added a method that sets up the property:

package mycompany;

public class Setup {
    public void browserSetup() {
        System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
    }
}

Then, in my test, I imported this as a library:

*** Settings ***
Library  Selenium2Library
Library  mycompany.Setup

And then invoked the method as a new keyword: Browser Setup.

That was it. I ran it and it worked.

EDIT: Just in case anyone is wondering how to reference chromedriver.exe as a relative path within the project, I placed the exe file in my {root_project_directory}\src\test\resources directory, then used it as src\\test\\resources\\chromedriver.exe (notice the \ escaping needed).

2
  • I am puzzled that how does it even work! 1. Your function name was 'browserSetup' in Java. How did it become the Robot Framework keyword 'Browser Setup'? I observed that Robot Framework is processes camel case java functions and creates them as space separated keywords. How is it possible? 2. I am happy that this is working. Now, I would like to enhance this java function to pass the driver from .robot files. How can I do that. Example: I would like to pass the webdriver from .robot file along with table locator and process the table to validate the rows. How can I do it? Commented Aug 4, 2018 at 15:56
  • Floella, I'm commenting to highlight you on the SE bar because I Santosh's comment was accidentally posted as an answer and I wanted to make sure the highlight went through. =)
    – corsiKa
    Commented Aug 5, 2018 at 7:52
1

'Create Webdriver' keyword allows passing argument to WebDriver's __init__, this means that it can pass argument to the constructor of WebDriver class. And WebDriver class constructor has an argument as 'executable_path' which is the path of the webdriver exe file.

We can use keyword arguments i.e kwargs={} to pass arguments to the the WebDriver Constructor. We have to create keyword dictionary and pass it in as argument kwargs.

This is the detail of keyword Create WebDriver:

Create Webdriver driver_name, alias=None, kwargs={}, **init_kwargs

Code to launch webdriver from custom path and launch URL:

*** Variables ***
${Driver_Path}=     C:${/}webdriver${/}chromedriver.exe
${url}=     google.com

*** Keywords ***
${dict}     Create Dictonary    executable_path=${Driver_Path}
Create Webdriver    driver_name=Chrome  kwargs=${dict}
Maximize Browser Window
Go To   ${url}
1

set up a environment variable like this:

Set Environment Variable    webdriver.chrome.driver   C:\webdriver\chromedriver.exe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.