51新一代自动化测试神器playwright+python系列课程_playwright_route修改接口响应

Route类修改接口响应

在做web的UI自动化测试时,我们需要尽可能的测试全面,各个功能的正常场景和异常场景都需要覆盖到。这种情况下为了覆盖这些场景我们需要准备大量的测试数据,但是有些测试数据可能是不好准备的,尤其是一些异常场景的测试数据。这种情况下如果要测试全面,我们怎么办呢?

Playwright下的route类可以捕获和修改请求和响应,通过这种方式可以尽可能的覆盖所有测试场景。

Route可以拦截和处理特定的网络请求,以模拟不同的行为或进行自定义操作。您可以使用`page.route()`方法创建`Route`对象,并指定要拦截的请求URL或使用正则表达式进行匹配。一旦创建了`Route`对象,您可以通过调用`route.continue()`、`route.fulfill()`或`route.abort()`来控制请求的进一步处理。还可以通过`route.request()`和`route.response()`属性来访问请求和响应对象。使用`Route`类,您可以拦截和修改网络请求,从而实现各种目的,如网络请求的模拟、修改请求头、延迟响应等。它是一个非常有用的工具,可用于各种Web自动化和测试场景中。

我们以禅道的添加用例来演示一下如何修改接口响应。

执行以下脚本用例是可以添加成功的

# '''

# author: 测试-老姜   交流微信/QQ:349940839

# 欢迎添加微信或QQ,加入学习群共同学习交流。

# QQ交流群号:877498247

# 西安的朋友欢迎当面交流。

# '''

from playwright.sync_api import Playwright, sync_playwright, expect

def run(playwright: Playwright) -> None:

    browser = playwright.chromium.launch(headless=False,slow_mo=2000)

    context = browser.new_context(record_video_dir="videos") #设置录屏保存目录

    page = context.new_page()

    page.goto("http://127.0.0.1/zentao/user-login.html") # 使用相对地址

    page.locator("#account").fill("admin")

    page.locator("input[name=\"password\"]").click()

    page.locator("input[name=\"password\"]").fill("Deshifuzhi01")

    page.locator('text="登录"').click()

    page.goto('http://127.0.0.1/zentao/testcase-create-1-0-0.html')

    page.frame_locator('#appIframe-qa').locator('#title').fill('123456')

    page.frame_locator('#appIframe-qa').locator('#submit').click()

    page.wait_for_timeout(10000)

    # ---------------------

    context.close() # 上下文关闭时保存录屏

    browser.close()

with sync_playwright() as playwright:

    run(playwright)

添加成功后,添加用例的的接口返回如下响应:

这个时候我们来模拟一下将保存成功的响应修改为标题为空,用例添加失败。

实践代码:

# '''

# author: 测试-老姜   交流微信/QQ:349940839

# 欢迎添加微信或QQ,加入学习群共同学习交流。

# QQ交流群号:877498247

# 西安的朋友欢迎当面交流。

# '''

from playwright.sync_api import Playwright, sync_playwright, expect

def run(playwright: Playwright) -> None:

    browser = playwright.chromium.launch(headless=False,slow_mo=2000)

    context = browser.new_context(record_video_dir="videos") #设置录屏保存目录

    page = context.new_page()

    page.goto("http://127.0.0.1/zentao/user-login.html") # 使用相对地址

    page.locator("#account").fill("admin")

    page.locator("input[name=\"password\"]").click()

    page.locator("input[name=\"password\"]").fill("Deshifuzhi01")

    page.locator('text="登录"').click()

    page.goto('http://127.0.0.1/zentao/testcase-create-1-0-0.html')

    page.frame_locator('#appIframe-qa').locator('#title').fill('123456')

    def handle(route):

        #定义接口响应为标题为空

        json={"result":"fail","message":{"title":["\u300e\u7528\u4f8b\u6807\u9898\u300f\u4e0d\u80fd\u4e3a\u7a7a\u3002"]}}

        route.fulfill(json=json) # 返回自定义的响应

    url = 'http://127.0.0.1/zentao/testcase-create-1-0-0.html'

    page.route(url, handle) # 监听添加用例接口

    page.frame_locator('#appIframe-qa').locator('#submit').click() # 点击保存,调用添加用例接口,接口响应按照自定义的内容返回。

    page.wait_for_timeout(10000)

    # ---------------------

    context.close() # 上下文关闭时保存录屏

    browser.close()

脚本执行后,用例标题实际填写,但是提示标题为空。

posted @ 2024-03-21 11:08  测试_老姜  阅读(103)  评论(0)    收藏  举报