2013-05-17 58 views
0

例如,在gmail登錄中,當我們考慮進行登錄測試時,首次手動進行登錄時,我們會從下一次開始登錄頁面直接進入收件箱頁面。selenium webdriver如何爲每次運行維護單獨的會話?

如果您嘗試在webdriver中執行相同的操作(運行兩次登錄測試),在所有這些嘗試中,我們都會獲得登錄頁面,因爲我們之前沒有從此計算機登錄。在幕後維護與cookies或會話相關的會話時發生了什麼?

回答

2

這裏是硒文檔描述&代碼片斷添加或刪除的cookie:

之前我們讓這些未來的步驟,你可能有興趣在 瞭解如何使用Cookie。首先,您需要位於Cookie有效的域名 。如果您在開始與網站互動之前嘗試預設 Cookie,那麼您需要花費很長時間才能加載替代方法,即在網站上找到較小的頁面 ,通常404頁面很小 (http://example.com/some404page

// Go to the correct domain 
driver.get("http://www.example.com"); 

// Now set the cookie. This one's valid for the entire domain 
Cookie cookie = new Cookie("key", "value"); 
driver.manage().addCookie(cookie); 

// And now output all the available cookies for the current URL 
Set<Cookie> allCookies = driver.manage().getCookies(); 
for (Cookie loadedCookie : allCookies) { 
    System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue())); 
} 

// You can delete cookies in 3 ways 
// By name 
driver.manage().deleteCookieNamed("CookieName"); 
// By Cookie 
driver.manage().deleteCookie(loadedCookie); 
// Or all of them 
driver.manage().deleteAllCookies(); 
+0

感謝您的回覆,在這裏我想明白了,硒的方式,而不明確管理cookie執行所有這些活動?例如,每當我們登錄到Gmail,我們都會得到一個登錄頁面,而不是直接登錄到帳戶。它如何管理(刪除)以前生成的會話? – user2365105

+1

您可以配置硒是否使用cookie。這裏是它的片段:FirefoxProfile profile = new FirefoxProfile(); profile.setPreference(「network.cookie.cookieBehavior」,2);檢查network.cookie.cookieBehavior的不同值的鏈接:http://kb.mozillazine.org/Network.cookie.cookieBehavior –