本文介绍了RobotFramework Selenium2Library 和外部库 - 通过 webdriver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用 Selenium Web Driver 实例的 Java 库.我想运行我用这个库以及 Selenium2Library 编写的测试.在某种程度上,Java 库会添加一些我需要的功能(使用 Ajax 元素),但大部分测试可以使用 Selenium2 关键字编写.

I have created a Java library that utilizes an instance of Selenium Web Driver. I would like to run the test I have written with this library, as well as the Selenium2Library. In a way, the Java Library would add some functionality that I need (working with Ajax elements), but much of the test could be written with Selenium2 keywords.

有没有办法将在 Selenium2Library 中实例化的 webdriver 传递给我的外部库,以便它们可以运行相同的测试?

Is there a way to pass the webdriver instantiated in Selenium2Library to my external library so that they can run the same test?

感谢您的意见!

推荐答案

当前浏览器存储在受保护的 WebDriverCache 字段中.您可以扩展 Selenium2Library 并公开 WebDriver,但我认为在这个简单的用例中,您最好改用反射.这样您就可以使用原始的 Selenium2Library.其他人可能会有不同的感觉.我将展示两者.

The current browser is stored in a WebDriverCache field that is protected. You could extend Selenium2Library and expose the WebDriver, but I think in this simple use case, you are better off using reflection instead. This way you can work with the original Selenium2Library. Others may feel differently. I will demonstrate both.

这两种解决方案都提供了一个 Get Current Browser 关键字,您可以从中获取结果并将其传递给库的构造函数等.

Both solutions provide a Get Current Browser keyword which you could take the result from and pass that into your library's constructor, etc.

这是一个带有关键字的库,它将使用反射来访问 WebDriverCache 并公开它:

Here you is a library with a keyword that will use reflection to access WebDriverCache and expose it:

// default package
import java.lang.reflect.Field;

import org.openqa.selenium.WebDriver;
import org.robotframework.selenium2library.keywords.BrowserManagement;
import org.robotframework.selenium2library.utils.WebDriverCache;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Misc {
    public static void goToGoogle() {
        getCurrentBrowser().get("http://www.google.com");
    }

    public static WebDriverCache getWebDriverCache() {
        try
        {
            BrowserManagement bm = (BrowserManagement) getLibraryInstance("Selenium2Library");
            Field cacheField = BrowserManagement.class.getDeclaredField("webDriverCache");
            cacheField.setAccessible(true);
            return (WebDriverCache) cacheField.get(bm);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static WebDriver getCurrentBrowser() {
        return getWebDriverCache().getCurrent();
    }

    private static Object getLibraryInstance(String library) throws ScriptException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
        engine.put("library", library);
        engine.eval("from robot.libraries.BuiltIn import BuiltIn");
        engine.eval("instance = BuiltIn().get_library_instance(library)");
        return engine.get("instance");
    }
}

下面您可以看到它是如何使用的,将 Selenium2Library 关键字与 Misc 中的关键字混合在一起:

Below you can see how it is used, mixing Selenium2Library keywords with keywords from Misc:

*** Settings ***
Test Teardown    Close All Browsers
Library    Selenium2Library
Library    Misc

*** Test Cases ***
Get Current Browser Test
    Open Browser    http://slashdot.org
    Go To Google
    Title Should Be    Google

如果你想使用自定义的 Selenium2Library(继承),这里是一个例子:

If you want to use a custom Selenium2Library instead (inheritence), here is an example:

// default package
import org.openqa.selenium.WebDriver;


public class MySelenium2Library extends Selenium2Library
{
    public WebDriver getCurrentBrowser() {
        return this.webDriverCache.getCurrent();
    }
}

直接从 Robot Framework 与 WebDriver 实例交互以使示例更简单:

Interact with WebDriver instance directly from Robot Framework to make the example simpler:

*** Settings ***
Test Teardown    Close All Browsers
Library    MySelenium2Library

*** Test Cases ***
Get Current Browser Test
    Open Browser    http://slashdot.org
    ${driver}=    Get Current Browser
    Call Method    ${driver}    get    http://www.google.com
    Title Should Be    Google

这篇关于RobotFramework Selenium2Library 和外部库 - 通过 webdriver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 16:38