Tips of "teach you by hand" - java+ selenium automated test - cookie - Part 2 (detailed tutorial)

Posted by devain on Sat, 12 Feb 2022 01:56:56 +0100

1. Introduction

Today, according to the original plan, Hongge will use an example to demonstrate to children's partners or children's shoes how to use cookies to skip the verification code to log in. This scenario is automatic login. Many system login information is stored in cookies, so you can automatically log in as long as you add the correct value to the cookie. What picture verification code and login use case are floating clouds. And login is often the first step of automated testing. The system can't log in. No matter how tall it is, it's useless. Therefore, brother Hong took out an article to introduce and share it, hoping to be helpful to you.

2.Fiddler

2.1 IE and Google browser grab HTTPS requests

1.IE and Google browser grab HTTPS requests

Tools -- Options -- HTTPS -- check Decrypt HTTPS traffic -- click OK to directly grab the HTTPS request. If not, set the certificate

2. Set certificate

Tools -- Options -- HTTPS -- check Decrypt HTTPS traffic -- click Actions -- select the last option to reset all certificates -- click OK -- and then keep selecting OK to install successfully.

3. Check whether the installation is successful:

Tools -- Options -- HTTPS -- check Decrypt HTTPS traffic -- click actions -- open windows certificate management -- actions -- find certificate -- enter fix -- to see if you want the relevant certificate,

If it is found, the installation is successful. Restart fiddler and browser to capture the package successfully!

3. Automation practice

3.1 packet capture

1. Open Fiddler tool, as shown in the figure below:

2. Log in to baidu account normally through browser. As shown below

3. Get the Cookie of login request through Fiddler. Find the URL whose Host is "passport.baidu.com" and view the Cookie of the request in the window on the right.

4. Then, find the two important parameters "BAIDUID" and "BDUSS".

3.2 code design

3.3 reference code

package lessons;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author Beijing - Hongge
 * 
 * @Official account: Beijing, Hong brother
 * 
 * <Teach you by hand series tips (63) - java+ selenium automated testing - cookie s - Part 2 (detailed tutorial) 
 *
 * 2022 January 26
 */
public class CookieBaidu {
    
    public static void main(String[] args) throws InterruptedException {
        
        System.setProperty("webdriver.gecko.driver", ".\\Tools\\chromedriver.exe"); //Specify drive path
           
        WebDriver driver = new ChromeDriver ();
        driver.get("http://www.baidu.com");
        driver.manage().window().maximize();
        Thread.sleep(5000);
        System.out.println(driver.manage().getCookies());
        
        // Use fiddler to capture packages , Find the URL whose Host is "passport.baidu.com" , View the cookie of the request in the right window ,
        // Find the two important parameters "BAIDUID" and "BDUSS"
        Cookie c1 = new Cookie("BAIDUID", "70EB956C3AD6908B86068C5D02205F87:FG=1");
        Cookie c2 = new Cookie("BDUSS", "AAAAAAAAAAAAAAAAAAAAAAAAA4X7mEOF-5hM");
        
        driver.manage().addCookie(c1);
        driver.manage().addCookie(c2);
        
        driver.navigate().refresh();
        
        // Get login user name
        String username = driver.findElement(By.className("user-name")).getText();
        System.out.println("username = " +username);
        
        System.out.println(driver.manage().getCookies());
        
        Thread.sleep(6000);
        System.out.println("browser will be close");
        driver.quit();
    }
}

3.4 operation code

1. Run the code, right-click run as - > java appliance, and the console will output, as shown in the following figure:

4. Summary

Here is a demonstration of how to skip the verification code to log in. If there is no verification code, we can log in directly through the user name and password, obtain the cookie, and then save the cookie, Carry out corresponding operations (for example, during interface testing, subsequent interfaces may need cookies, session s and token s, which will be explained in detail in the subsequent interface part, so there will be no repetition here). Well, it's getting late today. Let's share and explain here. Thank you for reading patiently!