Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to extend selenium2library in Java #79

Open
mammenj opened this issue Dec 18, 2015 · 3 comments
Open

How to extend selenium2library in Java #79

mammenj opened this issue Dec 18, 2015 · 3 comments

Comments

@mammenj
Copy link
Contributor

mammenj commented Dec 18, 2015

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 mammenj changed the title How to extend selenium2library in Jaav How to extend selenium2library in Java Dec 18, 2015
@lushiwei
Copy link

lushiwei commented Jun 23, 2016

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

@anarelle
Copy link

anarelle commented Jul 9, 2017

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.

@lushiwei
Copy link

lushiwei commented Aug 2, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants