Trong Automation Test sử dụng Selenium WebDỉver với Python 3, nếu bạn đã đăng nhập thì làm sao lưu được trạng thái đăng nhập khi bật lại cửa sổ trình duyệt. Chúng ta phải thao tác cookie của trình duyệt. Bài hướng dẫn sau sẽ hướng dẫn lưu (Save) và nhập (import) lại Cookie Show Để lưu cookie của trình duyệt chúng ta sử dụng đoạn code sau: import selenium.webdriver driver = selenium.webdriver.Firefox() driver.get("https://vinasupport.com") pickle.dump( driver.get_cookies() , open("cookies.pkl","wb")) Để import lại cookie chúng ta sử dụng đoạn code bên dưới import selenium.webdriver driver = selenium.webdriver.Firefox() driver.get("https://vinasupport.com") cookies = pickle.load(open("cookies.pkl", "rb")) for cookie in cookies: driver.add_cookie(cookie) Nguồn: vinasupport.com Cookie HTTP bao gồm thông tin về người dùng và sở thích của họ. Nó lưu trữ thông tin bằng cách sử dụng cặp khóa-giá trị. Nó là một phần dữ liệu nhỏ được gửi từ Ứng dụng web và được lưu trữ trong Trình duyệt web, trong khi người dùng đang duyệt trang web đó. Lệnh truy vấn Selenium cho cookieTrong Selenium Webdriver, chúng tôi có thể truy vấn và tương tác với cookie bằng phương thức tích hợp bên dưới: driver.manage (). getCookies (); // Trả về Danh sách tất cả Cookie driver.manage (). getCookieNamed (arg0); // Trả lại cookie cụ thể theo tên driver.manage (). addCookie (arg0); // Tạo và thêm cookie driver.manage (). deleteCookie (arg0); // Xóa cookie cụ thể driver.manage (). deleteCookieNamed (arg0); // Xóa cookie cụ thể theo Tên driver.manage (). deleteAllCookies (); // Xóa tất cả cookie Tại sao phải xử lý Cookie trong Selenium?Mỗi cookie được liên kết với tên, giá trị, miền, đường dẫn, thời hạn sử dụng và trạng thái xem nó có an toàn hay không. Để xác thực máy khách, máy chủ phân tích cú pháp tất cả các giá trị này trong cookie. Khi Kiểm tra ứng dụng web bằng trình điều khiển web selen, bạn có thể cần tạo, cập nhật hoặc xóa cookie. Ví dụ, khi tự động hóa Ứng dụng Mua sắm Trực tuyến, nhiều bạn cần phải tự động hóa các kịch bản thử nghiệm như đặt hàng, Xem giỏ hàng, Thông tin thanh toán, xác nhận đơn hàng, v.v. Nếu cookie không được lưu trữ, bạn sẽ cần thực hiện hành động đăng nhập mọi lúc trước khi thực hiện các tình huống kiểm tra được liệt kê ở trên. Điều này sẽ làm tăng nỗ lực viết mã và thời gian thực thi của bạn. Giải pháp là lưu trữ cookie trong một Tệp. Sau đó, truy xuất các giá trị của cookie từ tệp này và thêm vào đó phiên trình duyệt hiện tại của bạn. Do đó, bạn có thể bỏ qua các bước đăng nhập trong mọi Trường hợp kiểm tra vì phiên trình điều khiển của bạn có thông tin này trong đó. Máy chủ ứng dụng hiện coi phiên trình duyệt của bạn là đã được xác thực và trực tiếp đưa bạn đến URL mà bạn yêu cầu. Chúng tôi sẽ sử dụng http://demo.guru99.com/test/cookie/selenium_aut.php cho mục đích demo của chúng tôi. Đây sẽ là một quy trình gồm 2 bước. Bước 1) Đăng nhập vào ứng dụng và lưu trữ cookie xác thực được tạo. Bước 2) Sử dụng cookie đã lưu trữ để đăng nhập lại vào ứng dụng mà không cần sử dụng userid và mật khẩu. Bước 1) Lưu trữ thông tin 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(); } } } Giải thích mã:
Bước 2) Sử dụng cookie được lưu trữ để đăng nhập vào ứng dụng.Bây giờ, chúng tôi sẽ truy cập cookie được tạo ở bước 1 và sử dụng cookie được tạo để xác thực phiên của chúng tôi trong ứng dụng 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”); } } OUTPUT: Bạn được đưa trực tiếp đến màn hình đăng nhập thành công mà không cần nhập id người dùng và mật khẩu đầu vào LƯU Ý: Sử dụng làm mới cứng trong trường hợp bạn thấy trang đăng nhập sau khi thực hiện tập lệnh trên. Sự kết luận Do đó, bạn có thể tránh nhập tên người dùng và mật khẩu trên máy chủ xác thực chúng lặp đi lặp lại cho mỗi lần kiểm tra với sự trợ giúp của Selenium Webdriver, và do đó tiết kiệm rất nhiều thời gian. |