2017-05-30 54 views
0

我的硒碼下面的序列,Node.js的書面:餅乾添加硒的webdriver

it('tests cookies', t => { 
     driver.manage().getCookies().then(function (cookies) { 
     console.log('all cookies => ', cookies); 
     }); 
     driver.manage().addCookie({name:'foo', value: 'bar'}); 
     driver.manage().getCookies().then(function (cookies) { 
     console.log('all cookies => ', cookies); 
     }); 
     driver.manage().deleteCookie('foo'); 
     return driver.manage().getCookies().then(function (cookies) { 
     console.log('all cookies => ', cookies); 
     }); 
    }); 

,我得到這樣的輸出:

all cookies => [] 
all cookies => [] 
all cookies => [] 

有人知道爲什麼的addCookie功能止跌不工作?我不知道我明白爲什麼這不會在餅乾罐中產生一些餅乾。

回答

2

問題是Cookie域未定義。在使用Cookie之前,您需要導航到某個網址。在獲取所有cookie 之後和設置新的cookie之前,嘗試添加driver.get('<some_url>')

it('tests cookies', t => { 
 
     driver.get('127.0.0.1'); // <-- This will set the domain 
 
     driver.manage().getCookies().then(function (cookies) { 
 
     console.log('all cookies => ', cookies); 
 
     }); 
 
     driver.manage().addCookie({name:'foo', value: 'bar'}); 
 
     driver.get('127.0.0.1'); // <-- Navigate again after setting a new cookie 
 
     driver.manage().getCookies().then(function (cookies) { 
 
     console.log('all cookies => ', cookies); 
 
     }); 
 
     driver.manage().deleteCookie('foo'); 
 
     return driver.manage().getCookies().then(function (cookies) { 
 
     console.log('all cookies => ', cookies); 
 
     }); 
 
    });

看到這個還有:Selenium JS add cookie to request

+0

感謝,我期待在這裏:https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium -webdriver/lib/webdriver_exports_Options.html –

+0

由於某種原因本調用:driver.manage()。addCookie({name:'cec_user_id',value:val,secure:true,httpOnly:true});產率: –

+0

所有Cookie => [{路徑: '/', 域: 'CDT-服務器', 名: 'cec_user_id', 僅Http:假, 安全:假, hCode:-810793967, 值: 'alexamil', class:'org.openqa.selenium.Cookie'}] –