首页 > 其他分享 >【playwright学习】fixture和closure学习

【playwright学习】fixture和closure学习

时间:2024-07-03 11:54:51浏览次数:1  
标签:closure playwright fixture pytest test order def first

学习的话主要材料是官网:https://playwright.dev/python/docs/auth 

基础的话,像我一样薄弱就可以了。会一些java,但python会的不多,用得很少。上手贼容易,但是selenium也不怎么难

在学习之前,要弄清楚@pytest.fixture,closure(nested func)是怎么回事。

个人理解:

fixture:(面试的时候那个人叫夹具)就是跑test之前想做的事情,和跑test之后想做的事情。比如说建立数据库连接,拿到一个登录后的session信息。

一个test可以有多个fixture,每个fixture也可以用别的fixture。一个fixture可能在一个test被request好几次。这个案例有意思。

# contents of test_append.py
import pytest


# Arrange
@pytest.fixture
def first_entry():
    return "a"


# Arrange
@pytest.fixture
def order():
    return []


# Act
@pytest.fixture
def append_first(order, first_entry):
    return order.append(first_entry)


def test_string_only(append_first, order, first_entry):
    # Assert
    assert order == [first_entry]


这段是不是看起来很奇怪?按照常理思考,order 应该是[], [first_entry]是["a"],他们怎么可能相等?

Fixtures can also be requested more than once during the same test, and pytest won’t execute them again for that test. This means we can request fixtures in multiple fixtures that are dependent on them (and even again in the test itself) without those fixtures being executed more than once.

--https://docs.pytest.org/en/6.2.x/fixture.html

结论:即使同一个fixture被请求多次,在一个test里也只调用一次。在test_string_only执行中,先遇到append_first,order就被赋值为['a']了,尽管下一个参数还是fixture order,也不执行了。所以这个case是可以pass的。

fixture可以有哪些其他设定?

1. autouse 顾名思义,就是跑任何test之前都会自动做的set-up

@pytest.fixture(autouse=True)

2.scope设定:functionclassmodulepackage or session

  • function: the default scope, the fixture is destroyed at the end of the test.

  • class: the fixture is destroyed during teardown of the last test in the class.

  • module: the fixture is destroyed during teardown of the last test in the module.

  • package: the fixture is destroyed during teardown of the last test in the package.

  • session: the fixture is destroyed at the end of the test session.

 

closure:方法套方法。套在里面的方法可以用外层方法的变量。外层方法把内层函数名作为返回值

def outer_func(x):

 def inner_func(y):

   return x+y

return inner_func

   

标签:closure,playwright,fixture,pytest,test,order,def,first
From: https://www.cnblogs.com/jin-wen-xin/p/18281323

相关文章

  • 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-1-环境准备与搭建
    1.简介Python+Playwright系列的文章还没有结束,就有好的小伙伴或者童鞋们私信公众号留言,问宏哥什么时候出Java语言的Playwright的自动化测试文章。本来想趁热打铁将Python+Playwright完结后,就开始Java语言的Playwright的自动化测试文章,但是好多人私信留言,索性就两个系列的文章......
  • 《最新出炉》系列入门篇-Python+Playwright自动化测试-52- 字符串操作 - 下篇
    1.简介在日常的自动化测试工作中进行断言的时候,我们可能经常遇到的场景。从一个字符串中找出一组数字或者其中的某些关键字,而不是将这一串字符串作为结果进行断言。这个时候就需要我们对字符串进行操作,宏哥这里介绍两种方法:正则和字符串切片函数split()。2.测试场景宏哥在这里......
  • playwright 连接browserless 服务
    playwright是与puppeteer类似的包装,但是playwright实现的周边更加丰富,也是一个很不错的工具,值得使用下,以下是关于playwright连接browserless服务的简单说明环境准备docker-composeversion:"3"services:nginx:image:nginx:alpinevolumes......
  • 【Playwright+Python】系列教程(一)环境搭建及脚本录制
    一、前言看到这个文章,有的同学会说:六哥,你为啥不早早就写完python系列的文章。因为有徒弟需要吧,如果你也想学自学,那这篇文章,可以说是我们结缘一起学习的开始吧!如果对你有用,建议收藏和转发!二、Playwright是什么?微软开源自动化测试工具Playwright,支持主流浏览器,包括:Chrome、Fir......
  • Pytest框架中fixture功能详解
    文章目录1定义Fixture函数2Fixture的函数参数2.1传入其他fixture函数作为参数2.2传入request对象参数示例1:访问fixture的调用者示例2:使用fixture的参数3Fixture的作用域参数scope3.1scope=class场景3.2scope=session场景4Fixture的自动使用参数autouse=......
  • python-爬虫-playwright模块反爬
    fromplaywright.sync_apiimportPlaywright,sync_playwright,expectdefrun(playwright:Playwright)->None:#指定浏览器启动的端口#打开cmd:chrome--remote-debugging-port=9412--user-data-dir="F:\\google"#--remote-debugging-port=指定的端口......
  • 【PL理论】(20) 函数式语言:定义函数 Func = Var × E × Env | 闭包 (Closure) | 定义
    ......
  • XML-RPC实现WebService简单PHP程序示例 及 Closure闭包中的bind与bindTo方法的区别
    一、XML-RPC实现WebService简单PHP程序示例    WebService就是为了异构系统的通信而产生的,它基本的思想就是使用基于XML的HTTP的远程调用提供一种标准的机制,而省去建立一种新协议的需求。目前进行WebService通信有两种协议标准,一种是XML-RPC,另外一种是SOAP。XML-RPC比较......
  • 容器启动playwright报错TargetClosedError
    运行环境docker:Dockerversion24.0.5,build24.0.5-0ubuntu1~22.04.1ubuntu:22.04playwrightimage:itisfoundation/osparc-playwright-e2e:2024-05mavenimage:maven:3.8.5-openjdk-17-slim报错信息Causedby:com.microsoft.playwright.impl.TargetClosedError:Er......
  • 如何使用带有 typescript 的 playwright 查找 react 应用程序的 LCP?
    需要获取使用playwright和typescript的react应用程序的最大Contentful画面这是用于网络性能测试的,应该是精确的值我尝试了https://web.dev/articles/lcp#measure-lcp-in-javascript并使用Typescript实现了相同的功能但是我们需要知道,我们是否还有其他东西returnp......