软件测试|web自动化测试神器playwright教程(二十四)

前语

咱们在运用selenium进行自动化测试的过程中,遇到输入框时,咱们能够运用send_keys()输入内容,也能够运用键盘事情向输入框内输入内容,仅仅运用键盘事情时需求导入Keys,作为一款强壮的工具,selenium有的功用,playwright也有,playwright相同能够运用键盘输入内容,本篇文章就来为大家介绍一下playwright的键盘事情。

获取更多技术资料,请点击!

键盘操作

playwright的Keyboard 方法是用于键盘操作的函数,供给了一个用于办理虚拟键盘的 API。高级 api 是keyboard.type(),它接受原始字符并在您的页面上生成恰当的keydown、keypress/input和keyup事情。 为了更好地控制,您能够运用keyboard.down()、keyboard.up()和keyboard.insert_text()手动触发事情,就好像它们是从真实键盘生成的一样。

  1. 选择和删去文本
from playwright.sync_api import sync_playwright
def key_option():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        page.locator("#kw").click()
        page.keyboard.type("playwright")
        page.keyboard.down("Shift")
        for i in range(6):
            page.keyboard.press("ArrowLeft")
        page.keyboard.up("Shift")
        page.keyboard.press("Backspace")
        page.pause()
        context.close()
        browser.close()
key_option()

运转脚本之后,playwright的文本中后面6个字母被删去,成果如下图所示:

软件测试|web自动化测试神器playwright教程(二十四)

  1. 输入大写字母
from playwright.sync_api import sync_playwright
def key_option():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        page.locator("#kw").click()
        page.keyboard.press("Shift+KeyA")
        page.pause()
        context.close()
        browser.close()
key_option()

运转脚本,界面如下图所示:

软件测试|web自动化测试神器playwright教程(二十四)

注:运用Shift+A一样能够按出大写字母A

  1. 全选操作
from playwright.sync_api import sync_playwright
def key_option():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        page.locator("#kw").click()
        page.keyboard.type("playwright")
        page.keyboard.press("Control+A")
        page.pause()
        context.close()
        browser.close()
key_option()

运转脚本之后,界面如下所示:

软件测试|web自动化测试神器playwright教程(二十四)

注:Mac体系的键盘操作为‘Meta+A’

Down事情

调度keydown事情

key能够指定预期的keyboardEvent.key值或单个字符来为其生成文本。能够在此处key找到这些值的超集。键的比如是:

F1- F12, Digit0- Digit9, KeyA- KeyZ, Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape, ArrowDown, End, Enter, Home, , , Insert,等_PageDownPageUpArrowRightArrowUp

还支撑以下修正快捷方式:Shift, Control, Alt, Meta, ShiftLeft.

按住将键入对应于大写字母Shift的文本。key

假如key是单个字符,则区别大小写,因此值a和A将生成各自不同的文本。

假如key是润饰键 、Shift、Meta、Control或Alt,则后续按键将在该润饰键激活的情况下发送。要开释润饰键,请运用keyboard.up()。

按下一次键后,对keyboard.down()的后续调用会将repeat设置为 true。要开释键,请运用keyboard.up()。

语法如下:

keyboard.down(key)

insert_text 刺进文本

语法如下:

page.keyboard.insert_text("xxxx")

注:该操作只调度input事情,不发出keydown,keyup或keypress事情。

press 长按

key能够指定预期的keyboardEvent.key值或单个字符来为其生成文本。能够在此处key找到这些值的超集。键的比如是:F1- F12, Digit0- Digit9, KeyA- KeyZ, Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape, ArrowDown, End, Enter,Insert等

示例代码:

from playwright.sync_api import sync_playwright
def key_option():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://keycode.info")
        page.keyboard.press("a")
        page.screenshot(path="a.png")
        page.keyboard.press("ArrowLeft")
        page.screenshot(path="arrow_left.png")
        page.keyboard.press("Shift+O")
        page.screenshot(path="o.png")
        page.pause()
        context.close()
        browser.close()
key_option()

运转脚本,截取的图片如下:

软件测试|web自动化测试神器playwright教程(二十四)

Type 操作

为文本中的每个字符发送keydown、keypress/input和事情。

示例如下:

from playwright.sync_api import sync_playwright
def key_option():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        page.locator("#kw").click()
        page.keyboard.type("拜仁")
        page.keyboard.type("慕尼黑", delay=100)
        page.pause()
        context.close()
        browser.close()
key_option()

运转脚本,成果如下图:

软件测试|web自动化测试神器playwright教程(二十四)

up 方法

调度一个keyup事情,语法示例如下:

keyboard.up(key)

总结

本文主要介绍了playwright对键盘操作的方法,与selenium类似,playwright相同支撑对键盘的操作,同时,playwright的键盘操作比之selenium愈加方便快捷。

获取更多技术资料,请点击!