2016-03-23 61 views
7

我有一個運行Selenium的Behat測試,只要需要Javascript。 如果Javascript(所以Selenium被禁用),我目前的Behat測試工作得很好。Javascript測試:Selenium cookies數據url

目前,硒我唯一的錯誤反饋如下語句:

 
unknown: Failed to set the 'cookie' property on 'Document': Cookies are disabled inside 'data:' URLs. 
     (Session info: chrome=48.0.2564.109) 
     (Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Linux 4.2.0-16-generic x86_64) (WARNING: The server did not provide any stacktrace information) 
     Command duration or timeout: 7 milliseconds 
     Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42' 
     System info: host: 'ca7a41afbfee', ip: '172.17.0.10', os.name: 'Linux', os.arch: 'amd64', os.version: '4.2.0-16-generic', java.version: '1.8.0_45-internal' 
     Driver info: org.openqa.selenium.chrome.ChromeDriver 
     Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=/tmp/.com.google.Chrome.WWmJvH}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=48.0.2564.109, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] 
     Session ID: b8a3f435fe337ca77d523d5b72f6235b (WebDriver\Exception\UnknownError) 

什麼我錯在這裏做什麼?有些帖子是關於cookies的,但我真的不知道在哪裏添加它們。

回答

9

在完全加載頁面之前,您無法使用Chrome驅動程序設置Cookie。使用硒等待頁面加載,然後設置cookie。

+0

最後,我從硒切換到[Nightwatch.js(https://github.com/nightwatchjs/nightwatch),因爲所有的清晰。 –

1

您需要打開網頁前添加的Cookie:

$capabilities = DesiredCapabilities::chrome(); 
$host = 'http://localhost:4444/wd/hub'; // this is the default 
$this->webDriver = RemoteWebDriver::create($host, $capabilities, 5000); 
$this->urnOpen('/'); 
$cookie = array(); 
$cookie['name'] = 'NAME'; 
$cookie['value'] = 'VALUE'; 
$this->webDriver->manage()->addCookie($cookie);