2

I'm trying to run my selenium test in a headless browser (htmlUnit Webdriver, and phamtomJs). But whatever i use, some exception is thrown by the execution of javascript code, or error when i try to use an event which will use javascript in the html page i'm testing.

I'm unable to understand why it could not work.

here is the script I am trying with:

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();
    }

}

Fot this one i have multiple :

ReferenceError: "xxx" is not defined. (urlOfmyAPp/jsp/ihm.js?ts=0.1975212200823856#1451)

where the xxx to an javascript element.

I have another version of this script but with the firefox WebDriver, it's working well. But i don't understand why it"s not working with the htmlUnitDriver.

Could someone explain why it's failing? Thanks.

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
Omegaspard
  • 1,828
  • 2
  • 24
  • 52

0 Answers0