nomadro/apps/taro/web/scripts/test_home_visibility.py

50 lines
2.2 KiB
Python

"""Browser regression: global app overlays must not hide Taro router pages.
Start H5 first. Set TARO_H5_TEST_URL to check a non-default port.
API requests are mocked so the check never depends on a production backend.
"""
import os
from playwright.sync_api import sync_playwright, expect
def main():
origin = os.environ.get('TARO_H5_TEST_URL', 'http://127.0.0.1:10087').rstrip('/')
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
try:
for width, height in [(390, 844), (1440, 900)]:
context = browser.new_context(viewport={'width': width, 'height': height})
page = context.new_page()
errors = []
page.on('pageerror', lambda error: errors.append(str(error)))
def mock_api(route):
# Exercise packaged offline defaults, including disconnected live config.
route.fulfill(status=503, content_type='application/json', body='{}')
context.route('**/api/**', mock_api)
context.route('**/ebook-api/**', mock_api)
context.route('**/community-api/**', mock_api)
page.goto(f'{origin}/#/pages/home/index', wait_until='domcontentloaded')
home = page.locator('.taro_page_show .hub-root')
expect(home).to_be_visible(timeout=30000)
expect(home).to_contain_text('nomadro')
expect(home).to_contain_text('随处工作')
expect(page.get_by_text('调试', exact=True).first).to_be_visible()
# Navigate and return without reloading to exercise stacked route pages.
page.evaluate("location.hash = '#/pages/nav/index'")
expect(page.locator('.taro_page_show .navmini-root, .taro_page_show .app-shell').first).to_be_visible(timeout=30000)
page.go_back(wait_until='domcontentloaded')
expect(home).to_be_visible(timeout=30000)
assert not errors, errors
print(f'PASS {width}x{height}: home, navigation, back, debug overlay; no runtime errors')
context.close()
finally:
browser.close()
if __name__ == '__main__':
main()