Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法巡检需要登录后才能进入的页面 #3

Open
zhuweileo opened this issue Nov 12, 2024 · 1 comment
Open

无法巡检需要登录后才能进入的页面 #3

zhuweileo opened this issue Nov 12, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@zhuweileo
Copy link

某些系统页面需要用户登录后才能看到,未登录时访问页面会被重定向到登录页,需要支持访问页面时设置cookies信息。

@zdx0122
Copy link
Contributor

zdx0122 commented Nov 13, 2024

嗯,考虑过这个问题,卡点主要是在登录信息泄露的安全风险上,所以开源版本未做这个功能。
但可以在你们内部团队自行实现,自行实现容易考虑好安全风险,更加可控、合规。

自行实现是很简单的,主要原理是playwright支持浏览器上下文进行隔离测试,代码如下:
1、新增context对象,用于带Cookies的测试:

   @BeforeMethod
    void createContextAndPage() {
        logger.info("创建newContext对象");
        loggedContext = browser.newContext(new Browser.NewContextOptions()
                //浏览器上下文中设置好cookies信息,cookies保存在src/test/resources/cookies.json路径中
                .setStorageStatePath(Paths.get("src/test/resources/cookies.json"))  // cookies.json为登录后保存的cookies文件
                .setUserAgent("online_inspection_tracker Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.31 Mobile Safari/537.36")
                );
        logger.info("创建newPage()对象");
        logger.info("Cookies信息:" + loggedContext .storageState());
        page = loggedContext .newPage(); //利用带有cookies信息的loggedContext创建page
    }

2、cookies.json文件示例,自行更新想用的cookies信息即可,也可以做成类、入库等操作:

{
  "cookies":
  [
    {"name":"uid","value":"xxxxxxxxxxxxxxxxxxxxxx","domain":".itest.ren","path":"/","expires":-1,"httpOnly":false,"secure":false},
    {"name":"token","value":"xxxxxxxxxxxxxxxxxxx","domain":".itest.ren","path":"/","expires":-1,"httpOnly":false,"secure":false},
    {"name":"exp","value":"2592000","domain":".itest.ren","path":"/","expires":-1,"httpOnly":false,"secure":false}
  ],
  "origins":[]}

如有新的idea,随时交流

@zdx0122 zdx0122 added the enhancement New feature or request label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants