<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Selenium WebDriver 中的 Cookie 處理 > 原文: [https://www.guru99.com/handling-cookies-selenium-webdriver.html](https://www.guru99.com/handling-cookies-selenium-webdriver.html) HTTP cookie 由有關用戶及其首選項的信息組成。 它使用鍵值對存儲信息。 當用戶瀏覽該網站時,它是從 Web 應用程序發送并存儲在 Web 瀏覽器中的一小段數據。 點擊此處了解 [Cookie 測試](/cookie-testing-tutorial-with-sample-test-cases.html)。 在本教程中,我們將學習- * [Cookie 的硒查詢命令](#1) * [為什么要處理硒中的餅干?](#2) * [演示:Selenium 中的 Cookie 處理。](#3) * [步驟 1)存儲 cookie 信息。](#4) * [步驟 2)使用存儲的 cookie 登錄應用程序。](#5) ## 硒查詢命令 **在 Selenium Webdriver 中,我們可以使用以下內置方法查詢 Cookie 并與之交互:** ``` driver.manage().getCookies(); // Return The List of all Cookies driver.manage().getCookieNamed(arg0); //Return specific cookie according to name driver.manage().addCookie(arg0); //Create and add the cookie driver.manage().deleteCookie(arg0); // Delete specific cookie driver.manage().deleteCookieNamed(arg0); // Delete specific cookie according Name driver.manage().deleteAllCookies(); // Delete all cookies ``` ## 為什么要處理硒中的曲奇? 每個 cookie 均與名稱,值,域,路徑,到期時間以及是否安全的狀態相關聯。 為了驗證客戶端,服務器將所有這些值解析為 cookie。 當使用硒 Web 驅動程序測試 [Web 應用程序時,您可能需要創建,更新或刪除 Cookie。](/software-testing.html) 例如,當自動化在線購物應用程序時,您許多人需要自動化測試場景,例如下訂單,查看購物車,付款信息,訂單確認等。 如果未存儲 cookie,則在執行上面列出的測試方案之前,您每次都需要執行登錄操作。 這將增加您的編碼工作量和執行時間。 解決方案是將 cookie 存儲在文件中。 稍后,從該文件中檢索 cookie 的值,并將其添加到當前瀏覽器會話中。 結果,您可以跳過每個[測試用例](/test-case.html)中的登錄步驟,因為您的驅動程序會話中包含此信息。 現在,應用程序服務器將您的瀏覽器會話視為已認證,并直接將您帶到您請求的 URL。 ## 演示:Selenium 中的 Cookie 處理。 我們將出于演示目的使用 [http://demo.guru99.com/test/cookie/selenium_aut.php](http://demo.guru99.com/test/cookie/selenium_aut.php) 。 這將是一個兩步過程。 **步驟 1)**登錄到應用程序并存儲生成的身份驗證 cookie。 **步驟 2)**使用存儲的 cookie,無需使用用戶名和密碼即可再次登錄應用程序。 ### 步驟 1)存儲 cookie 信息。 ``` package CookieExample; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.Set; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.Cookie; public class cookieRead{ public static void main(String[] args) { WebDriver driver; System.setProperty("webdriver.chrome.driver","G:///chromedriver.exe"); driver=new ChromeDriver(); driver.get("http://demo.guru99.com/test/cookie/selenium_aut.php"); // Input Email id and Password If you are already Register driver.findElement(By.name("username")).sendKeys("abc123"); driver.findElement(By.name("password")).sendKeys("123xyz"); driver.findElement(By.name("submit")).click(); // create file named Cookies to store Login Information File file = new File("Cookies.data"); try { // Delete old file if exists file.delete(); file.createNewFile(); FileWriter fileWrite = new FileWriter(file); BufferedWriter Bwrite = new BufferedWriter(fileWrite); // loop for getting the cookie information // loop for getting the cookie information for(Cookie ck : driver.manage().getCookies()) { Bwrite.write((ck.getName()+";"+ck.getValue()+";"+ck.getDomain()+";"+ck.getPath()+";"+ck.getExpiry()+";"+ck.isSecure())); Bwrite.newLine(); } Bwrite.close(); fileWrite.close(); } catch(Exception ex) { ex.printStackTrace(); } } } ``` **代碼說明:** * 創建 WebDriver 實例 * 我們使用 driver.get(“ http://demo.guru99.com/test/cookie/selenium_aut.php”)訪問該網站 * 登錄到應用程序 * 使用 ``` driver.manage().getCookies(); ``` 閱讀 Cookie 信息 * 使用 FileWriter 類存儲 cookie 信息以寫入字符流,并使用 BufferedWriter 存儲文本以將文本寫入文件以將其創建為文件 Cookies.data * “ Cookies.data”文件與“名稱,值,域,路徑”一起存儲所有 cookie 信息。 我們可以檢索此信息并登錄到應用程序,而無需輸入登錄憑據。 * Once you run above code the Cookie.data file is created into the project folder structure as shown in below screen. Open the Cookie.data file, you can see login credential of the AUT is saved in the format of Cookie, see below-highlighted screen ![Cookie Handling in Selenium WebDriver](https://img.kancloud.cn/da/a6/daa6f31d3df90cab59876d0616df8168_950x194.png "Handling Cookies in Selenium WebDriver") ### 步驟 2)使用存儲的 cookie 登錄應用程序。 現在,我們將訪問在步驟 1 中生成的 cookie,并使用生成的 cookie 對應用程序中的會話進行身份驗證 ``` package CookieExample; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Date; import java.util.StringTokenizer; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class CookieWrite { public static void main(String[] args){ WebDriver driver; System.setProperty("webdriver.chrome.driver","G://chromedriver.exe"); driver=new ChromeDriver(); try{ File file = new File("Cookies.data"); FileReader fileReader = new FileReader(file); BufferedReader Buffreader = new BufferedReader(fileReader); String strline; while((strline=Buffreader.readLine())!=null){ StringTokenizer token = new StringTokenizer(strline,";"); while(token.hasMoreTokens()){ String name = token.nextToken(); String value = token.nextToken(); String domain = token.nextToken(); String path = token.nextToken(); Date expiry = null; String val; if(!(val=token.nextToken()).equals("null")) { expiry = new Date(val); } Boolean isSecure = new Boolean(token.nextToken()). booleanValue(); Cookie ck = new Cookie(name,value,domain,path,expiry,isSecure); System.out.println(ck); driver.manage().addCookie(ck); // This will add the stored cookie to your current session } } }catch(Exception ex){ ex.printStackTrace(); } driver.get("http://demo.guru99.com/test/cookie/selenium_aut.php"); } } ``` 輸出:您無需輸入輸入的用戶名和密碼即可直接進入登錄成功屏幕 注意:使用硬刷新,以防執行上述腳本后看到登錄頁面。 **結論** 因此,您可以避免在 Selenium Webdriver 的幫助下在服務器上輸入用戶名和密碼來一次又一次地驗證用戶名和密碼,從而節省大量時間。 ***本文由 Mangesh Waghmare*** 貢獻
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看