htmlUnit
Webdriver
セレン テストをヘッドレス ブラウザ ( 、および)で実行しようとしていますphamtomJs
。しかし、私が何を使用しても、javascriptコードの実行によって例外がスローされるか、テストしているhtmlページでjavascriptを使用するイベントを使用しようとするとエラーが発生します。
なぜうまくいかなかったのか理解できません。
ここに私が試しているスクリプトがあります:
public class Test {
static WebDriver driver;
static Wait<WebDriver> wait;
public static void main(String[] args) {
// File file = new File("C:/phantomjs-2.1.1-windows/bin/phantomjs.exe");
// System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
// DesiredCapabilities caps = new DesiredCapabilities();
// caps.setJavascriptEnabled(true);
// driver = new PhantomJSDriver(caps);
driver = new HtmlUnitDriver(true);//true to enable the JS
wait = new WebDriverWait(driver, 3000);
final String url = "https://localhost:8444/myApp/";
JavascriptExecutor js = (JavascriptExecutor) driver;
try {
driver.navigate().to(url);
js.executeScript("document.getElementsByName('formLogin')[0].checked = true;");
By usernameInput = By.xpath("//input");
By passwordInput = By.xpath("//div[2]/div/div/input");
By submitButton = By.xpath("//a[contains(@href, '#')]");
wait.until(ExpectedConditions.visibilityOfElementLocated(usernameInput));
wait.until(ExpectedConditions.visibilityOfElementLocated(passwordInput));
wait.until(ExpectedConditions.visibilityOfElementLocated(submitButton));
WebElement log = driver.findElement(usernameInput);
WebElement pass = driver.findElement(passwordInput);
WebElement click = driver.findElement(submitButton);
log.sendKeys("root");
pass.sendKeys("pass");
click.click();
By headerId = By.className("header");
wait.until(ExpectedConditions.visibilityOfElementLocated(headerId));
System.out.println("Page title is: " + driver.getTitle());
String var = (String) js.executeScript("var truc = 'bonjour'; return truc;");
System.out.println(var);
} finally {
driver.close();
}
}
これには複数あります:
ReferenceError: "xxx" は定義されていません。(urlOfmyAPp/jsp/ihm.js?ts=0.1975212200823856#1451)
ここでxxx
、javascript 要素へ。
このスクリプトの別のバージョンがありますが、firefoxWebDriver
ではうまく機能しています。しかし、なぜそれが で動作しないのか理解できませんhtmlUnitDriver
。
誰かがなぜ失敗したのか説明できますか? ありがとう。