如何使用Java从Selenium RC调用JavaScript函数

时间:2012-01-25 04:55:39

标签: java javascript selenium-rc

我有user-extensions.js,其中包含以下代码段:

Selenium.prototype.doTypeRepeated = function(locator, text) {
    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);

    // Create the text to type
    var valueToType = text + text;

        // Replace the element text with the new text
    this.page().replaceText(element, valueToType);
};

我想使用Selenium RC从java代码中为文本字段(“css = input.text”)调用此函数。我怎么称呼这个?

1 个答案:

答案 0 :(得分:1)

从头到尾,你应该能够通过以下方式调用你的代码:

selenium.getEval("Selenium.prototype.doTypeRepeated('input.text', 'some text');");

但是,如果遵循suggested方法,您应该这样做

HttpCommandProcessor proc;
proc = new HttpCommandProcessor("localhost", 4444, "*iexplore", "http://google.com/");
Selenium selenium = new DefaultSelenium(proc);
string[] inputParams = {"css=input.text", "Hello World"};
proc.DoCommand("doTypeRepeated", inputParams);

不要忘记将硒作为

开始
java -jar selenium-server.jar -userExtensions user-extensions.js