2

我寫了一個用java啓動的使用FirefoxDriver啓動的Selenium測試,它在Firefox瀏覽器中執行得很好。Htmlunit驅動程序有Javascript的問題

然後我跟HtmlunitDriver更換FirefoxDriver這樣的:

driver = new FirefoxDriver(); 

driver = new HtmlUnitDriver(true); 

但後來我得到這個錯誤:

它缺少 ';'指令(http://local.project/bundles/app/js/socket.js#1

之前,這是socket.js文件:

class SocketHandler { 
    constructor(url) { 
     this.url = url; 
     this.session = null; 
    } 

    .... 
} 

我懷疑它不能識別類的聲明。任何想法如何糾正,請?

+0

爲什麼要使用HtmlUnitDriver? –

+0

@TarunLalwani因爲它使用比firefox更少的資源 – Mit94

+0

那麼最好使用PhantomJS。 'HtmlUnitDriver'不是最適合測試的驅動程序。 –

回答

1

你甚至不需要使用PhantomJs。由於PhantomJs現在還沒有太多維護。您可以在無頭模式下使用chromedriver。

你只需要添加的選項,如下無頭: -

chromeOptions.addArguments("--headless"); 

請在下面找到完整代碼:

System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe"); 
ChromeOptions chromeOptions = new ChromeOptions(); 
chromeOptions.addArguments("--headless"); 
chromeOptions.addArguments("--start-maximized"); 
WebDriver driver = new ChromeDriver(chromeOptions); 
driver.get("https://google.com"); 

如果仍然要使用phantomjs雖然。然後先下載phantomjs二進制從下面的位置: -

http://phantomjs.org/download.html

現在使用下面的代碼: -

System.setProperty("phantomjs.binary.path","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\phantomjs\\phantomjs.exe"); 
DesiredCapabilities capabilities = null; 
ArrayList<String> cliArgsCap = new ArrayList<String>(); 
capabilities = DesiredCapabilities.phantomjs(); 
cliArgsCap.add("--web-security=false"); 
cliArgsCap.add("--ssl-protocol=any"); 
cliArgsCap.add("--ignore-ssl-errors=true"); 
capabilities.setCapability("takesScreenshot", true); 
capabilities.setJavascriptEnabled(true); 
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap); 
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,new String[] { "--logLevel=2" }); 
driver = new PhantomJSDriver(capabilities); 
driver.get("https://www.google.co.in/"); 

希望它會幫助你:)

+0

謝謝你真的很有幫助。我正在使用Selenium進行一些測試加載,甚至在無頭模式下,我的計算機啓動「滯後」之前無法放置超過50個瀏覽器。你知道是否有更多的chromeOptions可以用來「減輕」瀏覽器嗎?或者如果它值得轉移到PhantomJS?我需要同時達到80個瀏覽器。 – Mit94

+0

是的,你可以嘗試phantomjs ..你也可以去硒網格的概念,其中你的主機將指令發送到奴隸機器..所以使用網格,你需要2臺機器,它將運行40個實例 –