Selenium:如何从java代码执行user-extensions.js

时间:2013-06-25 16:07:58

标签: java javascript selenium selenium-webdriver selenium-ide

我想使用Selenium测试智能GWT应用。 为此,我需要加起来 1. user-extensions.js 2. user-extensions-ide.js 在IDE中。 这为在页面上定位GWT元素提供了额外的scLocators

现在,如果我想使用Java测试上面的页面,那么我将在代码中添加这些js文件

1 个答案:

答案 0 :(得分:0)

我接近这个的方式是使用SeleniumServer方法,它允许我从java代码中指定我的扩展文件。

一个有效的SmartGWT示例是:

public class Example  {

SeleniumServer server = null;
Selenium selenium = null;
HttpCommandProcessor proc = null;

@Before
public void setUp() throws Exception {
    RemoteControlConfiguration config = new RemoteControlConfiguration();
    config.setUserExtensions(new File("bin/selenium/user-extensions.js"));
    server = new SeleniumServer(config);

    server.boot();

    proc = new HttpCommandProcessor("localhost",
            4444,
            "*firefox",
            "http://test");

    selenium = new DefaultSelenium(
             proc);
    selenium.start();
    selenium.open("http://test");

}
@Test      
public String justATest() {
    execCommand("waitForElementClickable", "scLocator=//Window[ID=\"errorWindow\"]/item[0][Class=\"Label\"]/");
    String elementTest = selenium.getText("scLocator=//Window[ID=\"errorWindow\"]/item[0][Class=\"Label\"]/");
    assertEquals(elementTest, "lorem");
}
protected String execCommand(String command, String locator) {
    String[] locatorArg = {locator};
    return proc.doCommand(command, locatorArg);
}

@After
public void stopSelenium() {
    System.out.println("in after hook");
    if (selenium != null) {
        selenium.deleteAllVisibleCookies();
        selenium.stop();
    }

    if (server != null) {
        server.stop();
    }
    System.out.println("after after hook");
}
}
相关问题