跳到内容

重新加载

根据文件更改,在运行时重新加载选择器。

这对调试和在测试运行期间编写新的选择器很有用。

代码示例

带有代码

from playwright.sync_api import expect, sync_playwright
from hitchpage import PlaywrightPageConfig
from pathlib import Path

browser = sync_playwright().start().chromium.connect("ws://127.0.0.1:3605")
page = browser.new_page()

conf = PlaywrightPageConfig(
    *Path(".").glob("*.yml"),    # all .yml files in this folder
    playwright_page=page,
)

page.goto("https://127.0.0.1:8001")

selectors.yml

login:
  element:
    username: "#id_username"
    password: "#id_password"
    ok: "#id_ok_button"
new_selectors_file.txt

login:
  element:
    username: "#id_username"
    password: "#id_password"
    ok: "#id_ok_button"
dashboard:
  element:
    dashboard: "#id_this_is_a_dashboard_element"
    message: "#id_dashboard_message"
    next:
      text: next

带有 HTML

index.html

<div class="form-login">
<input type="text" id="id_username" placeholder="username" /></br>
<input type="text" id="id_password" placeholder="password" /></br>
<div class="wrapper">
<span class="group-btn">     
<a id="id_ok_button" href="/dashboard.html" class="btn btn-primary btn-md">login <i class="fa fa-sign-in"></i></a>
</span>
</div>
</div>

dashboard.html

<div class="form-login">
  <h4 id="id_this_is_a_dashboard_element">Dashboard</h4>
  <p id="id_dashboard_message">hello!</a>
</div>
conf.next_page("login")
print("login page")
conf.element("username").fill("myusername")
conf.element("password").fill("mypassword")
conf.element("ok").click()

# overwrite the YAML file so it can be reloaded
import shutil
shutil.copyfile("new_selectors_file.txt", "selectors.yml")
conf.reload_config()

conf.next_page("dashboard")
print("dashboard page")
expect(conf.element("message")).to_be_visible()

将输出

login page                                                                                                                                                      
dashboard page

可执行规范

文档自动生成自 reload.story storytests。