php网站开发心得,如何制造公司网站,phpok企业建站系统,成都it公司排名目录 方案实现思路#xff1a;
方案一#xff1a;
方案二#xff1a; 方案实现思路#xff1a; 依照上图所见#xff0c;就知道#xff0c;一个账号是pytest-playwright默认的环境#xff0c;一个是 账号登录的环境
方案一#xff1a;
直接上代码#xff1a;
imp…目录 方案实现思路
方案一
方案二 方案实现思路 依照上图所见就知道一个账号是pytest-playwright默认的环境一个是 账号登录的环境
方案一
直接上代码
import time
from playwright.sync_api import Page, BrowserContext
import pytest
from pages.explore.explore import ExplorePage
from playwright.sync_api import expect
class TestLogin():pytest.fixture(autouseTrue)def for_each(self,page:Page):self.login ExplorePage(page)# 打开页面1已登录的账号进行操作self.login.gotoURL()yieldprint(后置操作)def testmore(self,playwright):browser playwright.chromium.launch(channelchrome, headlessFalse)context browser.new_context()page2 context.new_page()self.login2 ExplorePage(page2)# 打开页面2未登录的账号进行操作self.login2.gotoURL()代码解读
在case执行前会自动调用for_each方法这里面默认的登录用户就是conftest.py中配置的用户执行case时我们又重新打开浏览器重新创建一个context上下文这里我们就是账号2了此时是未登录状态。此时就有了两个浏览器同时打开第一个是登陆状态第二个是未登录状态~这种方式实现并不好因为如果我们需要登录的话很可能有些网站支持不了 方案二
在conftest.py文件中来配置
pytest.fixture(scopemodule)
def more_context(browser_context_args, browser, pytestconfig):# 将browser_context_args中配置的信息复制到context_args中除了storage_state【这个是登录用户的信息】context_args {k: v for k, v in browser_context_args.items() if k ! storage_state}# 获取新的登录用户的信息并且需要将\\替换为/否则找不到文件storage_state str(pytestconfig.rootpath.joinpath(auth/cookie.json)).replace(\\, /)# 从文件中读取出新的登录用户的信息并且将内容填充到context_args的storage_state中# 关于新的登录用户信息这部分也可以不做那就表示第二个用户是未登录状态也可以满足一些自动化的测试点with open(storage_state, r) as file:context_args[storage_state] json.load(file)# 根据新的context_args信息来创建新的context上下文对象context browser.new_context(**context_args)return context
上述中cookie.json的内容怎么来可以看这篇文章【pytest、playwright】构建POM项目以及解决登录问题allure环境问题
case中的实现
import timeimport playwright.sync_api
from playwright.sync_api import Page, BrowserContext
import pytest
from pages.explore.explore import ExplorePage
from playwright.sync_api import expect
class TestLogin():pytest.fixture(autouseTrue)def for_each(self, page:Page):self.login ExplorePage(page)# 打开页面1已登录的账号进行操作self.login.gotoURL()yieldprint(后置操作)def testmore02(self, more_context:BrowserContext):page2 more_context.new_page()self.login2 ExplorePage(page2)# 打开页面2未登录的账号进行操作self.login2.gotoURL()time.sleep(5)print(账号2)上述就会产生两个用户了一个是playwright默认生成的一个是我们手动创建的~