【问题标题】:Selenium: how to execute user-extensions.js from java codeSelenium:如何从 java 代码执行 user-extensions.js
【发布时间】:2013-06-25 16:07:57
【问题描述】:
我想使用 Selenium 测试一个智能 GWT 应用程序。
为此,我需要加起来
1. 用户扩展.js
2. 用户扩展-ide.js
在 IDE 中。
这提供了一个额外的 scLocators 用于在页面上定位 GWT 元素
现在如果我想使用Java测试上面的页面,那么我将在代码中的哪里添加这些js文件
【问题讨论】:
标签:
java
javascript
selenium
selenium-webdriver
selenium-ide
【解决方案1】:
我解决这个问题的方法是使用 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");
}
}