Why doesn't HtmlUnitDriver execute JavaScript?

前端 未结 5 1862
滥情空心
滥情空心 2020-12-20 12:00

I got the following problem: I am running a JUnit testCase with Selenium 2.9 using HtmlUnitDriver with Browserversion Firefox_3_6. JavaScript is enabled. Now when it should

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 12:29

    You can enable JavaScript by doing either

    • new HtmlUnitDriver(true);
    • driver.setJavascriptEnabled(true);

    What you need to do is to wait until the JavaScript is executed after get(url).

    You can use Thread.sleep() method for adding some delay.

    HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
    driver.setJavascriptEnabled(true);
    driver.get(url);
    
    Thread.sleep(100);
    
    runTest();
    

    Update

    As @Corey indicated in the comments, it could be nicer to use Explicit and Implicit Waits instead of Thread.sleep(). As I don't use them these days, I cannot confirm, though. It would be great if someone test them and update this answer.

提交回复
热议问题