robotframework-selenium2library-java icon indicating copy to clipboard operation
robotframework-selenium2library-java copied to clipboard

How to extend selenium2library in Java

Open mammenj opened this issue 9 years ago • 3 comments

I would like to know if there is proper approach to extend this library to include new keyword (supported by selenium driver) without changing the original code? Thanks for the library.

mammenj avatar Dec 18 '15 05:12 mammenj

You can create your own jar with following test library. Note: In your test case, you must import the Selenium2Library first then import ExtendedSelenium2Library.

package sample;

import java.util.List;

import javax.script.ScriptException;

import org.openqa.selenium.WebElement;

import com.github.markusbernhardt.selenium2library.Selenium2Library;
import com.github.markusbernhardt.selenium2library.keywords.BrowserManagement;
import com.github.markusbernhardt.selenium2library.keywords.Element;
import com.github.markusbernhardt.selenium2library.locators.ElementFinder;

public class ExtendedSelenium2Library {
    private Selenium2Library sel2;
    private BrowserManagement browserManagement;
    private Element element;

    public ExtendedSelenium2Library() throws ScriptException {
        sel2 = (Selenium2Library) Selenium2Library.getLibraryInstance();
        browserManagement = sel2.getBrowserManagement();
        element = sel2.getElement();
        // ...
    }

    public void myInputKeyword(String data) {
        // call Selenium2Library api
        System.out.println(browserManagement.getTitle());
        System.out.println(element.getValue("name=btnK"));

        // call selenium api
        List<WebElement> elements = ElementFinder.find(
                browserManagement.getCurrentWebDriver(), "id=lst-ib");
        elements.get(0).sendKeys(data);
    }
}
*** Settings ***
Library           Selenium2Library
Library           sample.ExtendedSelenium2Library

*** Test Cases ***
Test1
    open browser    http://www.google.com    ie
    My Input Keyword    Cheese!
    Submit Form    id=tsf
    close browser

lushiwei avatar Jun 23 '16 02:06 lushiwei

I'm trying to do this on my project but I'm getting a null WebDriver object, and not sure why.

I created this class:

public class Extended {
	private Selenium2Library sel2;
	private BrowserManagement browserManagement;
	private Element element;
	private org.openqa.selenium.WebDriver driver;  //this one is from Selenium, not Selenium2Library
	
	public Extended() throws ScriptException {
		try {
			sel2 = (Selenium2Library) Selenium2Library.getLibraryInstance();
		} catch (javax.script.ScriptException e) {
			e.printStackTrace();
		}
		browserManagement = sel2.getBrowserManagement();
                driver=browserManagement.getCurrentWebDriver();
                element = sel2.getElement();
    }
}

First weird thing is that I get this warning:

Access restriction: The type 'ScriptException' is not API (restriction on required library 'C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar')

Then, if I add a breakpoint right after the driver=browserManagement.getCurrentWebDriver(); line and debug, I can clearly see how this driver object is null.

Not sure if it's relevant, but since I use Maven, I haven't manually installed anything and all my project setup is in pom.xml, where I've included selenium-java from org.seleniumhq.selenium and robotframework-selenium2library-java from com.github.markusbernhardt, as well as the robotframework-maven-plugin from org.robotframework.

anarelle avatar Jul 09 '17 01:07 anarelle

Have you imported the Selenium2Library before your test library in your test case? The import order is very important.

*** Settings ***
Library           Selenium2Library
Library           sample.ExtendedSelenium2Library

lushiwei avatar Aug 02 '17 02:08 lushiwei