我在页面上输入了 ID 为“email30”的文本字段,我正在尝试从 Playwright 读取它的值
let dd_handle = await page.$("#email30");
let value = await dd_handle.getAttribute("value");
Run Code Online (Sandbox Code Playgroud)
但是它返回“”,尽管我在输入文本中有一个值。当我检查时,我也没有看到 value 属性设置为当前值。
下面的普通 JS 代码给了我正确的值
document.getElementById("email30").value
Run Code Online (Sandbox Code Playgroud)
不确定如何从剧作家框架中读取价值。任何人都可以请指教吗?他们的文件在这里完全没有帮助。
在 playwright-python 中我知道我可以得到一个elementHandleusing querySelector().
示例(同步):
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto('https://duckduckgo.com/')
element = page.querySelector('input[id=\"search_form_input_homepage\"]')
Run Code Online (Sandbox Code Playgroud)
如何根据 this 获取相对于此的元素elementHandle?即父母、祖父母、兄弟姐妹、孩子的句柄?
什么是正确的查询选择器来获取所有标签并迭代以捕获表数据?下面的查询选择器无法在 DOM 中找到元素。先感谢您
const allusers = await page.$$eval('tbody .MuiTableRow-root', (users) => {
return users.map(user => {
const email = user.querySelector('tr:nth-child(1)');
const edition = user.querySelector('tr:nth-child(2)');
return {
email: email.innerText.trim(),
edition: edition.innerText.trim()
};
});
});
console.log('${allusers.length} users found');
console.dir(allusers);
Run Code Online (Sandbox Code Playgroud)
我目前正在尝试使用 Expect 来进行断言const { expect } = require('@playwright/test');,但每次我都会收到错误:找不到模块“@playwright/test”。这是一个非常短的脚本,但有些问题。
const { chromium } = require("playwright");
const { expect } = require('@playwright/test');
const { matchers } = require('playwright-expect');
console.log("##########", expect)
// add custom matchers
expect.extend(matchers);
(async () => {
const browser = await chromium.launch({
headless: false,
});
const page = await browser.newPage();
await page.goto("someurl");
await page.fill("input[name='userLoginId']", 'nnn');
await page.fill("input[name='password']", 'nnn');
await page.click("button[type=submit]");
})();
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "playwright",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "node ./index.js"
},
"keywords": …Run Code Online (Sandbox Code Playgroud) 找不到任何关于如何使用剧作家模拟/存根服务器端请求的好文档。
一个例子是拦截 nextjs 中的 getServerSideProps:点击路由使服务器发出请求(db API 等)。然后它可以执行一些业务逻辑(也应该通过测试涵盖),然后将其作为 props 传递给组件,然后发送到客户端(在服务器端渲染)。
我希望找到答案,即在不将一些测试逻辑混合到业务逻辑中的情况下模拟数据库 API 请求。
在我的 Playwright 测试中,我根据文档设置了base-url :
const config: PlaywrightTestConfig = {
projects: [
{
name: 'Safari MacBook Air',
use: {
browserName: 'webkit',
viewport: {
width: 2560,
height: 1620,
},
contextOptions: {
ignoreHTTPSErrors: true,
},
},
},
],
use: {
baseURL: process.env.PLATFORMSH_URL,
headless: false,
locale: 'ja-JP',
// Debugging artifacts.
screenshot: 'on',
trace: 'on',
video: 'on',
},
};
export default config;
Run Code Online (Sandbox Code Playgroud)
这适用于goto:
await this.page.goto('/myDirectory');
Run Code Online (Sandbox Code Playgroud)
但是,它失败了expect:
expect(page.url()).toBe('/myPage');
Run Code Online (Sandbox Code Playgroud)
错误是:
Run Code Online (Sandbox Code Playgroud)Expected: "/myPage" Received: "https://www.example.com/myPage"
我该如何使用expectwith baseURL?
I'm using playwright and vscode, especially the "debug test" function which pops up a real browser window to run the test. When it fails the test auto closes, how can I make it not auto close so that I can modify the test and also visually see what the state of the test is?
我正在使用 Playwright 上传文件并下载结果。当输入文件很大并且需要很长时间来处理时,我会从剧作家那里得到一个超时;“下载”按钮需要很长时间才会出现。
raise exception
playwright._impl._api_types.TimeoutError: Timeout 30000.0ms exceeded while waiting for event "download"
=========================== logs ===========================
waiting for event "download"
============================================================
Run Code Online (Sandbox Code Playgroud)
我怎样才能让剧作家在那个特定事件上等待更长时间?
with page.expect_download() as download_info:
page.locator("text=Download").click()
#todo: wait longer?
download = download_info.value
# expect(page).to_have_url("http://localhost:8080/swagger/#/NER/post_ner")
path = download.path()
suggested_filename = file_out
download.save_as(suggested_filename)
Run Code Online (Sandbox Code Playgroud) 在浏览器中,通常有在历史记录中前进和后退的箭头按钮:
这些按钮可以通过剧作家点击吗?
他们没有任何元素,所以我想知道这是否可能......
await navigationObject(page).backButton.click();
Run Code Online (Sandbox Code Playgroud) 最近 Playwright 实现了新的 UI 模式,可以与npx playwright test --ui
我在 Playwright 中使用cucumber-js一起运行。有什么方法可以在 --ui 模式下与剧作家一起运行黄瓜测试吗?
我尝试使用该标志运行 cucumber-js,但正如预期的那样,这没有帮助。
playwright ×10
javascript ×4
node.js ×2
python ×2
bdd ×1
cucumber ×1
cucumberjs ×1
dom ×1
handle ×1
interception ×1
mocking ×1
stubbing ×1
testing ×1
typescript ×1