Web automation testing cookie reuse

1. Introduction to cookies

Cookies are some data that are stored in text files on the user's computer.
When the web server sends a web page to the browser, the server will not record the user information after the link is closed.

 

2. Why use cookies for automated login?

  • Reusing browsers still requires human intervention at the beginning of each use case
  • If the use case needs to be executed frequently, reusing browsers is not a good choice.
  • Most cookies have a long shelf life and can be used multiple times by scanning them once.

3. Cookie reuse ideas

 

4. Frequently Asked Questions about Cookie Reuse

  1. Enterprise WeChat cookies have a kicking mechanism. After successfully obtaining the cookie. Don’t scan the QR code again! ! ! !
  2. When obtaining cookies, that is, when executing code to obtain cookies, be sure to log in
  3. After implanting the cookie, you need to enter the login page and refresh to verify whether the automatic login is successful.

5. Obtain implanted cookies

  • Get cookies: driver.get_cookies()
  • Implant cookies: driver.add_cookies()
def test_get_cookies(self):
        self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
        time.sleep(20)
        cookies = self.driver.get_cookies()
        print(cookies)
        # 将cookie写入文件
        with open("cookies.yaml", "w") as f:
            yaml.safe_dump(cookies, f)

    def test_add_cookies(self):
        self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
        # 从文件中读取cookie
        cookies = yaml.safe_load(open("cookies.yaml"))
        # 植入cookies
        for i in cookies:
            self.driver.add_cookie(i)
        time.sleep(3)
        self.driver.refresh()
        time.sleep(10)

Finally: The following is the supporting learning materials, which should be the most comprehensive and complete preparation warehouse for friends who do [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you! [100% free to receive without any tricks]

Software testing interview applet

A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!

Covers the following interview question sections:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. Web, app, interface automation, 7. Performance testing, 8. Programming basics, 9. HR interview questions, 10. Open test questions, 11. Security testing, 12. Computer basics

  How to obtain the full set of information: Click on the small card below to get it yourself

Guess you like

Origin blog.csdn.net/weixin_57794111/article/details/133638725