📝 面试求职: 「面试试题小程序」 ,内容涵盖 测试基础、Linux操作系统、MySQL数据库、Web功能测试、接口测试、APPium移动端测试、Python知识、Selenium自动化测试相关、性能测试、性能测试、计算机网络知识、Jmeter、HR面试,命中率杠杠的。(大家刷起来…)
📝 职场经验干货:
Playwright对键盘的操作主要是键盘打字和按键。这里准备一个网页,用来学习键盘的操作。页面上有一个文本框,可以通过键盘向其输入字符。另外,还有三个单选按钮,可以通过键盘的上下箭头控制选中哪一个选项。
网页源码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Playwright键盘操作</title>
</head>
<body>
<span>①键盘操作:</span><br>
<textarea rows="10" cols="30">
</textarea>
<button type="submit">提交</button>
<br>
<label> Key Up <input id="r1" type="radio" name="a"> </label><br>
<label> Key Down <input id="r2" type="radio" name="a"> </label><br>
<label> Key Press <input id="r3" type="radio" name="a"> </label><br>
</body>
</html>
Playwright通过locator.type()可以模拟通过键盘一个字一个字输入的效果,其中delay参数控制每个字输入的间隔时间。大部分情况下, 我们会使用locator.fill()填充文本内容,locator.type()用的并不多。
通过locator.press()模拟对键盘上单个按键的操作。
下面代码是通过对上面网页进行的键盘操作。
def test_mouse_keyboard(page):
page.goto("file:///Users/chunming.liu/PycharmProject/playwright_demo/html/keyboard.html")
page.locator("textarea").type("大学之道在明明德在亲民在止于至善", delay=500)
page.locator("textarea").press("$")
page.locator("textarea").press('A')
page.locator("textarea").press('0')
page.locator("textarea").press("F1")
page.locator("textarea").press("Escape") #键入Esc键
page.locator("textarea").press("Equal") # 键入等号按键
page.locator("textarea").press("Backspace") # 键入退格按键
page.locator("textarea").press("Enter") # 键入回车键
page.locator("textarea").press("Enter") # 键入回车键
page.get_by_text("Key Up").click() # 点击Key Up按钮
page.get_by_label("Key Up").press("ArrowDown") # 键入向下箭头按键
page.get_by_label("Key Down").press("ArrowDown")# 键入向下箭头按键
page.get_by_label("Key Press").press("ArrowUp")# 键入向上箭头按键
page.pause()
这段代码中,通过locator.type()输入“大学之道在明明德在亲民在止于至善”的文本,每个字的输入间隔是500ms。另外通过locator.press()键入单个按键内容,具体内容参考代码中的注释。下面是键盘上一些不可打印的特殊按键的名字,供参考:
Playwright支持的鼠标操作,常见的有单击、双击、右击、Hover等。下面设计一个测试网页,当用鼠标双击“DOUBLE_CLICK”按钮,上面文本框显示“[按钮被双击]”。当用鼠标单击“CLICK”按钮,上面文本框中显示“[按钮被单击]”。当用鼠标右击“RIGHT_CLICK”按钮,上面文本框中显示“[按钮被右击]”。当鼠标悬到“鼠标悬浮试验区”上时,下面文本框会显示“mouseOver”。
网页源码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Playwright键盘操作</title>
</head>
<body>
<span>①键盘操作:</span><br>
<textarea rows="10" cols="30">
</textarea>
<button type="submit">提交</button>
<br>
<label> Key Up <input id="r1" type="radio" name="a"> </label><br>
<label> Key Down <input id="r2" type="radio" name="a"> </label><br>
<label> Key Press <input id="r3" type="radio" name="a"> </label><br>
</body>
</html>
Playwright通过locator.type()可以模拟通过键盘一个字一个字输入的效果,其中delay参数控制每个字输入的间隔时间。大部分情况下, 我们会使用locator.fill()填充文本内容,locator.type()用的并不多。
通过locator.press()模拟对键盘上单个按键的操作。
下面代码是通过对上面网页进行的键盘操作。
def test_mouse_keyboard(page):
page.goto("file:///Users/chunming.liu/PycharmProject/playwright_demo/html/keyboard.html")
page.locator("textarea").type("大学之道在明明德在亲民在止于至善", delay=500)
page.locator("textarea").press("$")
page.locator("textarea").press('A')
page.locator("textarea").press('0')
page.locator("textarea").press("F1")
page.locator("textarea").press("Escape") #键入Esc键
page.locator("textarea").press("Equal") # 键入等号按键
page.locator("textarea").press("Backspace") # 键入退格按键
page.locator("textarea").press("Enter") # 键入回车键
page.locator("textarea").press("Enter") # 键入回车键
page.get_by_text("Key Up").click() # 点击Key Up按钮
page.get_by_label("Key Up").press("ArrowDown") # 键入向下箭头按键
page.get_by_label("Key Down").press("ArrowDown")# 键入向下箭头按键
page.get_by_label("Key Press").press("ArrowUp")# 键入向上箭头按键
page.pause()
这段代码中,通过locator.type()输入“大学之道在明明德在亲民在止于至善”的文本,每个字的输入间隔是500ms。另外通过locator.press()键入单个按键内容,具体内容参考代码中的注释。下面是键盘上一些不可打印的特殊按键的名字,供参考:
Playwright支持的鼠标操作,常见的有单击、双击、右击、Hover等。下面设计一个测试网页,当用鼠标双击“DOUBLE_CLICK”按钮,上面文本框显示“[按钮被双击]”。当用鼠标单击“CLICK”按钮,上面文本框中显示“[按钮被单击]”。当用鼠标右击“RIGHT_CLICK”按钮,上面文本框中显示“[按钮被右击]”。当鼠标悬到“鼠标悬浮试验区”上时,下面文本框会显示“mouseOver”。
网页源码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Playwright鼠标操作</title>
<script>
function rcl(e){
if (!e) e = window.event;
document.f1.t2.value += "[按钮被右击]";
}
function dcl(e){
if (!e) e = window.event;
document.f1.t2.value += "[按钮被双击]";
}
function cl(e){
if (!e) e = window.event;
document.f1.t2.value += "[按钮被单击]";
}
function showMouseOver(e){
if (!e) e = window.event;
var el = document.getElementById("result");
var s = "";
if (e.ctrlKey) s += "<ctrl>";
if (e.altKey) s += "<alt>";
if (e.shiftKey) s += "<shift>";
if (e.metaKey) s += "<meta>";
el.value = s + "mouseOver";
}
</script>
</head>
<body>
<span>鼠标操作:</span><br>
<form name="f1">
<textarea name="t2" style="width:200px;height:100px;"></textarea><br>
<input type="button" onclick="document.f1.t2.value=''" value="Clear" name="t1"><br>
<input type="button" value="DOUBLE_CLICK" ondblclick="dcl(event)">
<input type="button" value="CLICK" onclick="cl(event)">
<input type="button" value="RIGHT_CLICK" oncontextmenu="rcl(event)">
<div onmouseover="showMouseOver(event)" style="height:100px;width:250px;border:1px solid gray;">
<div style=";padding:35px;text-align:center">鼠标悬浮试验区</div>
</div>
<input type="text" id="result">
</form>
</body>
</html>
下面代码是对这个网页进行的鼠标操作:
def test_mouse(page):
page.goto("file:///Users/chunming.liu/PycharmProject/playwright_demo/html/form.html")
page.get_by_role("button", name="DOUBLE_CLICK").dblclick()
page.get_by_role("button", name="CLICK").click()
page.get_by_role("button", name="RIGHT_CLICK").click(button="right")
page.get_by_text("鼠标悬浮试验区").hover()
Playwright通过dblclick()实现鼠标双击,通过click()不带参数实现鼠标单击,带参数button="right"实现鼠标右击。通过hover()实现鼠标悬浮到元素上。
实际应用中,键盘和鼠标组合操作也是很常见的。例如按住Shift键+鼠标点击、按住Alt键+鼠标点击、按住Contrl键+鼠标点击等等。下面设计一个网页,当键盘上按下Ctrl、Shift、Command、option按键,并且点击“鼠标键盘组合试验区”按钮时,将会在上面的文本框中打印出此时的鼠标和键盘事件。
网页源码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Playwright键盘鼠标组合操作</title>
<script>
function show(e){
if (!e) e = window.event;
var el = document.getElementById("tb");
var s = "";
if (e.ctrlKey) s += "<ctrl>";
if (e.altKey) s += "<alt>";
if (e.shiftKey) s += "<shift>";
if (e.metaKey) s += "<meta>";
el.value = s + "click";
}
</script>
</head>
<body>
<span>鼠标和键盘组合:</span><input type="text" id="tb">
<div onclick="show(event)" style="height:100px;width:300px;border:1px solid gray;">
<div style=";padding:35px;text-align:center">鼠标键盘组合试验区</div>
</div>
</div>
</body>
</html>
使用Playwright进行鼠标键盘的组合操作使用的依旧是click()方法,其中的modifiers参数带上点击鼠标时,键盘上的按键。
下面的代码是分别模拟按住Shift键、Alt键和Command键并配合鼠标点击的操作。
def test_example(page):
page.goto("file:///Users/chunming.liu/PycharmProject/playwright_demo/html/form.html")
page.get_by_text("鼠标键盘组合试验区").click(modifiers=["Alt"])
time.sleep(2)
page.get_by_text("鼠标键盘组合试验区").click(modifiers=["Shift"])
time.sleep(2)
page.get_by_text("鼠标键盘组合试验区").click(modifiers=["Meta"])
page.pause()
执行代码可以看到文本框内容会依次显示:click、click、click。
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取 【保证100%免费】