Selenium 2 Java uses mysq to resolve token expiration when inserting cookies into browsers

Keywords: Programming Mobile SQL Database Java

When I study selenium 2 Java and insert cookies into the browser to simulate user login, I found a problem, that is, the token value expired. Later, I learned selenium 2 java to connect to the database and found a better solution. Every time you insert cookies, you always get the latest token from the database, which perfectly solves the problem of expiration.

This is the cookies I got from my browser after I logged in:

[Automatic_login=18436035355%7Ce3ceb5881a0a1fdaad01296d7554868d%7CStudent; expires=Tuesday, 21 March 2017 01:59:55 CST; path=/;  domain=www.dz101.com, Hm_lvt_52b97b391587eb6d3e582caa097d6f91=1489471192; expires=Wednesday, 14 March 2018 01:59:56 CST; path=/;   domain=.dz101.com, MyName=18436035355; expires=Tuesday, 21 March 2017 01:59:54 CST; path=/;   domain=www.dz101.com, User_token_Session=f24f16d472b222271e6dcf27077231b9; expires=Tuesday, 21 March 2017 01:59:54 CST; path=/;   domain=www.dz101.com, User_identity_Session=1; expires=Tuesday, 21 March 2017 01:59:54 CST; path=/;   domain=www.dz101.com, PHPSESSID=1s2uvdrj33d72qvj2qlqojhsl7; path=/; domain=www.dz101.com, Hm_lpvt_52b97b391587eb6d3e582caa097d6f91=1489471196;  path=/; domain=.dz101.com]

After analysis and trial, it is found that only two items, MyName and user token session, can be inserted.   Following is my successful insert cookies:

[Hm_lvt_52b97b391587eb6d3e582caa097d6f91=1489472871; expires=Wednesday, 14 March 2018 02:27:53 CST; path=/;  domain=.dz101.com, MyName=18436035355; path=/;  domain=www.dz101.com, User_token_Session=f24f16d472b222271e6dcf27077231b9; path=/;  domain=www.dz101.com, PHPSESSID=uahgb7ll1405h0p5jhloipt7a2; path=/;  domain=www.dz101.com, Hm_lpvt_52b97b391587eb6d3e582caa097d6f91=1489472873; path=/; domain=.dz101.com]

Here is the code I wrote.

//Adding cookies to browsers
public static void addCookies(WebDriver driver, String mobile) throws ClassNotFoundException, SQLException, IOException {
Cookie a = new Cookie("MyName", mobile);
Cookie b = new Cookie("User_token_Session", MySql.getNewToken(mobile));
driver.manage().addCookie(a);
driver.manage().addCookie(b);
driver.navigate().refresh();
//View Browser cookies
// Set<Cookie> cooies = driver.manage().getCookies();
// System.out.println(cooies);
}

The following is the getNewToken (String mobile) method:

    public static String getNewToken(String mobile) throws ClassNotFoundException, SQLException, IOException {
// load driver
        Class.forName(driver);
// Connect to the database
        Connection conn = DriverManager.getConnection(url, user, password);
        if (!conn.isClosed())
            System.out.println("Succeeded connecting to the Database!");
//State is used to execute SQL statement s
        Statement statement = conn.createStatement();
// SQL statement to execute
        String sql = "select * from users where mobile = " + mobile;
        output(sql);
// Result set
        ResultSet rs = statement.executeQuery(sql);
        System.out.println("The results of the query are as follows:");
        String id = null;
        while (rs.next()) {
            // Select column data
            id = rs.getString("id");
            // Output result
            System.out.println(rs.getString("id") + "\t" + id);
        }
        rs.close();
        String sql2 = "select * from users_token where uid = " + id + " ORDER BY create_time DESC LIMIT 1";
        ResultSet rs2 = statement.executeQuery(sql2);
        String token = null;
        System.out.println("The results of the query are as follows:");
        while (rs2.next()) {
            token = rs2.getString(token);
            output(token);
            saveToFile(getNow() + token, "runlog.log", false);
        }
        conn.close();
        return token;
    }

Selection of previous articles

  1. One line of java code prints a heart
  2. Linux performance monitoring software netdata Chinese version
  3. Interface Test Code Coverage (jacoco) Scheme Sharing
  4. Performance testing framework
  5. How to Enjoy Performance Testing on Linux Command Line Interface
  6. Graphic HTTP Brain Map
  7. Writing to everyone about programming thinking
  8. json data formatting output to console
  9. How to Test Probabilistic Business Interface
  10. Automatically convert swagger documents into test code
  11. Mac+httpclient High Concurrency Configuration Example
  12. httpclient handles multi-user simultaneous online

Public Number Map ☢️ Together ~FunTester

Posted by jameslloyd on Fri, 04 Oct 2019 04:29:29 -0700