check-app-browser.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. """Real Chromium UI verification for seven-language visitor and user routes.
  2. Public service/schema fixtures are dated read-only catalogue snapshots. Account,
  3. order, ticket and payment responses are synthetic UI fixtures. ALL API requests
  4. are intercepted; this is not real login, payment, booking or refund verification.
  5. """
  6. import json, os, re
  7. from pathlib import Path
  8. from urllib.parse import urlparse, parse_qs
  9. from playwright.sync_api import sync_playwright
  10. base = os.environ.get('BLOG_TEST_URL', 'http://127.0.0.1:3108').rstrip('/')
  11. indexing = os.environ.get('BLOG_INDEXING_ENABLED') == 'true'
  12. output = Path(os.environ.get('APP_SCREENSHOT_DIR', 'output/playwright/multilingual-user-cta/app-browser'))
  13. output.mkdir(parents=True, exist_ok=True)
  14. locales = ['en', 'de', 'fr', 'es', 'it', 'pt', 'zh']
  15. public_paths = ['/', '/services', '/slots', '/terms', '/refund-policy', '/privacy', '/cookie-policy', '/contact']
  16. copy = json.loads(Path('src/lib/i18n/public-pages.json').read_text())
  17. base_en = json.loads(Path('src/lib/i18n/messages.json').read_text())
  18. catalog = json.loads(Path('src/lib/i18n/catalog-messages.json').read_text())
  19. fixtures = Path('scripts/fixtures')
  20. products_response = json.loads((fixtures / 'public-products.json').read_text())
  21. products = products_response['data'] if isinstance(products_response['data'], list) else products_response['data']['items']
  22. products_by_id = {p['id']: p for p in products}
  23. schemas = {i: json.loads((fixtures / f'schema-{i}.json').read_text()) for i in [13, 14, 15]}
  24. user = {'id': 90001, 'email': 'qa@example.invalid', 'nickname': 'QA fixture', 'phone': '', 'role': 'user', 'avatar': '', 'created_at': '2026-10-01T12:00:00Z'}
  25. order = {'id': 90002, 'product_id': 35, 'product_title': 'Ireland Netherlands Visa Slot', 'base_amount': 7000, 'base_currency': 'EUR', 'final_amount': 7000, 'final_currency': 'EUR', 'amount': 7000, 'currency': 'EUR', 'status': 'pending', 'created_at': '2026-10-01T12:00:00Z', 'user_inputs': {'first_name': 'QA', 'last_name': 'EXAMPLE'}, 'user_id': user['id']}
  26. ticket = {'id': 90003, 'order_id': order['id'], 'type': 'refund', 'status': 'pending', 'reason': 'QA fixture request', 'created_at': '2026-10-01T12:00:00Z'}
  27. rows, api_calls, page_errors, console_errors, actions, layout_errors, control_errors, states = [], [], [], [], [], [], [], []
  28. def localize(path, locale):
  29. return path if locale == 'en' else '/' + locale + ('' if path == '/' else path)
  30. def attach_fixtures(context, authenticated=False):
  31. # Move the isolated API placeholder to a local intercepted URL; no unsafe-port
  32. # exception or external server is enabled. Axios request/response code still runs.
  33. context.add_init_script(r"""
  34. const open = XMLHttpRequest.prototype.open;
  35. XMLHttpRequest.prototype.open = function(method, url, ...args) {
  36. const fixtureUrl = String(url).replace(/^http:\/\/127\.0\.0\.1:9(?=\/api\/)/, location.origin);
  37. return open.call(this, method, fixtureUrl, ...args);
  38. };
  39. localStorage.setItem('app_lang', 'zh');
  40. """)
  41. if authenticated:
  42. context.add_init_script("""if (!localStorage.getItem('qa-fixture-initialized')) {
  43. localStorage.setItem('rsid', 'INVALID-QA-FIXTURE-NOT-A-REAL-SESSION');
  44. localStorage.setItem('user_info', %s);
  45. localStorage.setItem('qa-fixture-initialized', 'true');
  46. }""" % json.dumps(json.dumps(user)))
  47. def intercept(route):
  48. req = route.request
  49. u = urlparse(req.url); q = parse_qs(u.query)
  50. api_calls.append({'method': req.method, 'path': u.path, 'intercepted': True, 'synthetic': u.path not in ['/api/vas/product/list-enable', '/api/vas/product/detail', '/api/vas/schema/detail']})
  51. if req.method != 'GET' and not (req.method == 'POST' and u.path == '/api/vas/payment/create'):
  52. route.fulfill(status=400, content_type='application/json', body=json.dumps({'code': 400, 'message': 'Blocked fixture mutation'})); return
  53. if u.path.endswith('/product/list-enable'): response = products_response
  54. elif u.path.endswith('/product/detail'): response = {'code': 0, 'data': products_by_id.get(int(q.get('product_id', ['0'])[0]))}
  55. elif u.path.endswith('/schema/detail'): response = schemas[int(q['schema_id'][0])]
  56. elif u.path.endswith('/order/list_by_user'): response = {'code': 0, 'data': {'items': [order], 'total': 1}}
  57. elif u.path.endswith('/ticket/list_by_user'): response = {'code': 0, 'data': {'items': [ticket], 'total': 1}}
  58. elif u.path.endswith('/order/detail'): response = {'code': 0, 'data': order}
  59. elif u.path.endswith('/payment_provider/list_enabled'): response = {'code': 0, 'data': [{'id': 90004, 'name': 'stripe', 'title': 'Stripe QA fixture', 'currency': 'EUR', 'provider_type': 'stripe', 'enabled': 1}]}
  60. elif u.path.endswith('/payment/create'):
  61. # Invoice rendering only. No request leaves Playwright, and the link
  62. # uses the reserved .invalid domain. It is never opened or confirmed.
  63. response = {'code': 0, 'data': {'id': 90005, 'payment_id': 90005, 'order_id': order['id'], 'base_amount': 7000, 'base_currency': 'EUR', 'amount': 7000, 'currency': 'EUR', 'fee_amount': 0, 'random_offset': 0, 'channel': 'online_link', 'payment_url': 'https://example.invalid/qa-payment', 'provider_type': 'stripe', 'status': 'pending'}}
  64. elif u.path.endswith('/payment/confirm'): response = {'code': 0, 'data': {'status': 'succeeded'}}
  65. elif 'message' in u.path: response = {'code': 0, 'data': {'items': [], 'total': 0}}
  66. elif 'snapshot' in u.path or 'slots' in u.path: response = {'code': 0, 'data': []}
  67. elif u.path.endswith('/list_by_order') or 'events' in u.path: response = {'code': 0, 'data': []}
  68. else: response = {'code': 0, 'data': {'items': [], 'total': 0}}
  69. route.fulfill(status=200, content_type='application/json', body=json.dumps(response))
  70. context.route('**/api/**', intercept)
  71. # The fixture catalogue has remote marketing images. Their network performance
  72. # is not measured here; stop those remote assets from leaving the test context.
  73. context.route('https://api.text.skin/**', lambda route: route.abort())
  74. def observe(page):
  75. page.on('pageerror', lambda error: page_errors.append({'url': page.url, 'message': str(error)}))
  76. page.on('console', lambda msg: console_errors.append({'url': page.url, 'message': msg.text, 'source': msg.location.get('url')}) if msg.type == 'error' else None)
  77. def inspect_view(page, label, locale, widths=(320, 390, 1440)):
  78. for width in widths:
  79. page.set_viewport_size({'width': width, 'height': 900})
  80. if not page.evaluate('document.documentElement.scrollWidth <= innerWidth'):
  81. layout_errors.append({'path': localize(label, locale), 'width': width, 'scrollWidth': page.evaluate('document.documentElement.scrollWidth')})
  82. controls = page.locator('input:not([type="hidden"]), textarea, select, button').evaluate_all("""els => els.filter(el => el.getClientRects().length).map(el => ({tag: el.tagName, type: el.type, name: el.getAttribute('aria-label') || el.getAttribute('aria-labelledby') || [...(el.labels || [])].map(l => l.innerText).join(' ').trim() || (el.tagName === 'BUTTON' ? el.innerText.trim() || el.title || [...el.querySelectorAll('img')].map(i => i.alt).join(' ').trim() : '')}))""")
  83. if not all(c['name'] for c in controls): control_errors.append({'url': page.url, 'state': label, 'controls': [c for c in controls if not c['name']]})
  84. states.append({'path': localize(label, locale), 'checkedWidths': list(widths), 'layoutPass': not any(e['path'] == localize(label, locale) for e in layout_errors), 'namedControls': sum(bool(c['name']) for c in controls)})
  85. return len(controls)
  86. def verify_page(page, path, locale, public=False):
  87. response = page.goto(base + localize(path, locale), wait_until='networkidle')
  88. assert response and response.status == 200, page.url
  89. assert page.locator('html').get_attribute('lang') == ('zh-CN' if locale == 'zh' else locale), page.url
  90. assert page.locator('nav select:visible').first.input_value() == locale, page.url
  91. assert page.locator('link[rel="canonical"]').get_attribute('href') == 'https://text.skin' + localize(path, locale), page.url
  92. if public:
  93. assert page.title() == copy[locale][path][0] + ' | STRATA', page.url
  94. assert page.locator('h1').count() == 1, page.url
  95. assert page.locator('link[rel="alternate"][hreflang]').count() == 8, page.url
  96. else:
  97. assert page.locator('meta[name="robots"]').get_attribute('content') == 'noindex, nofollow', page.url
  98. assert response.headers.get('x-robots-tag') == 'noindex, nofollow', page.url
  99. widths = [320, 390, 768, 1024, 1440]
  100. count = inspect_view(page, path, locale, widths)
  101. rows.append({'path': localize(path, locale), 'language': locale, 'status': response.status, 'title': page.title(), 'public': public, 'checkedWidths': widths, 'layoutPass': not any(e['path'] == localize(path, locale) for e in layout_errors), 'namedControls': count, 'api': 'Isolated UI fixtures; no live account or transaction'})
  102. (output / 'progress.json').write_text(json.dumps({'completedPages': rows, 'layoutErrors': layout_errors, 'unnamedControlErrors': control_errors, 'pageErrors': page_errors}, ensure_ascii=False, indent=2))
  103. print('Checked', localize(path, locale), flush=True)
  104. return response
  105. with sync_playwright() as p:
  106. options = {'headless': True}
  107. if os.environ.get('CHROMIUM_PATH'): options['executable_path'] = os.environ['CHROMIUM_PATH']
  108. browser = p.chromium.launch(**options)
  109. context = browser.new_context(viewport={'width': 1440, 'height': 900}, locale='en-IE')
  110. attach_fixtures(context)
  111. page = context.new_page(); observe(page)
  112. for locale in locales:
  113. for path in public_paths:
  114. verify_page(page, path, locale, public=True)
  115. if path == '/slots':
  116. assert page.locator('#slot-country').input_value() == 'IE'
  117. assert page.locator('#slot-city').input_value() == 'Dublin'
  118. page.locator('#slot-country').select_option('GB')
  119. assert page.locator('#slot-city').input_value() == 'London'
  120. assert page.locator('#slot-city option').evaluate_all('(els) => els.map(el => el.value)') == ['London', 'Manchester']
  121. if path == '/services':
  122. for product in products:
  123. heading = page.get_by_role('heading', name=catalog[product['title']][locales.index(locale)], exact=True)
  124. assert heading.count() == 1
  125. heading.click()
  126. modal = page.locator('main .fixed.inset-0.z-50').first
  127. assert modal.is_visible()
  128. inspect_view(page, '/services#detail-' + str(product['id']), locale)
  129. modal.locator('button[aria-label]').first.click()
  130. if locale in ['de', 'fr', 'zh']:
  131. page.set_viewport_size({'width': 390, 'height': 900})
  132. page.screenshot(path=str(output / f'{locale}-services-mobile.png'), full_page=True)
  133. # Client navigation updates metadata for the newly selected page.
  134. page.goto(base + localize('/', locale), wait_until='networkidle')
  135. page.locator('main a[href="' + localize('/services', locale) + '"]').focus(); page.keyboard.press('Enter')
  136. page.wait_for_url(base + localize('/services', locale), wait_until='networkidle')
  137. assert page.title() == copy[locale]['/services'][0] + ' | STRATA'
  138. assert page.locator('link[rel="canonical"]').get_attribute('href').endswith(localize('/services', locale))
  139. # The URL owns the language even with a contradictory saved preference.
  140. page.goto(base + localize('/services', locale) + '?source=qa#details', wait_until='networkidle')
  141. target_locale = 'fr' if locale != 'fr' else 'de'
  142. page.locator('nav select:visible').select_option(target_locale)
  143. page.wait_for_url(base + localize('/services', target_locale) + '?source=qa#details', wait_until='networkidle')
  144. assert page.locator('html').get_attribute('lang') == target_locale
  145. assert page.title() == copy[target_locale]['/services'][0] + ' | STRATA'
  146. actions.append({'locale': locale, 'clientNavigationMetadata': True, 'languagePreservesQueryAndFragment': True, 'slotValuesPreserved': True})
  147. context.close()
  148. for locale in locales:
  149. context = browser.new_context(viewport={'width': 1440, 'height': 900}, locale='en-IE')
  150. attach_fixtures(context, authenticated=True)
  151. page = context.new_page(); observe(page)
  152. for path in ['/login', '/dashboard', '/dashboard/settings', '/create-order/34', '/create-order/35', '/create-order/36', '/payment/90002', '/payment/confirm']:
  153. verify_page(page, path, locale)
  154. if path == '/login':
  155. page.locator('main form button[type="button"]').click()
  156. inspect_view(page, '/login#forgot-password', locale)
  157. page.locator('main .fixed.inset-0.z-50 button[aria-label]').first.click()
  158. page.locator('main > div > div > div.flex.border-b-2 button').nth(1).click()
  159. inspect_view(page, '/login#register-info', locale)
  160. if path == '/dashboard':
  161. page.locator('main button').filter(has=page.locator('svg.lucide-eye')).first.click()
  162. inspect_view(page, '/dashboard#order-detail', locale)
  163. page.locator('main .fixed.inset-0.z-50 button[aria-label]').first.click()
  164. page.set_viewport_size({'width': 390, 'height': 900})
  165. tabs = page.locator('main > div > div.md\\:hidden button')
  166. tabs.nth(1).click()
  167. inspect_view(page, '/dashboard#tickets', locale)
  168. page.locator('main button').filter(has=page.locator('svg.lucide-eye')).first.click()
  169. inspect_view(page, '/dashboard#ticket-detail', locale)
  170. page.locator('main .fixed.inset-0.z-50 button[aria-label]').first.click()
  171. page.locator('main button').filter(has=page.locator('svg.lucide-plus')).first.click()
  172. inspect_view(page, '/dashboard#new-ticket', locale)
  173. page.locator('main .fixed.inset-0.z-50 button[aria-label]').first.click()
  174. if path.startswith('/create-order/'):
  175. product = products_by_id[int(path.rsplit('/', 1)[1])]
  176. assert page.locator('h1').inner_text() == catalog[product['title']][locales.index(locale)]
  177. nationality = page.locator('#field-nationality')
  178. if nationality.count():
  179. schema_record = schemas[product['schema_id']]['data']
  180. schema = schema_record['schema_json']
  181. if isinstance(schema, str): schema = json.loads(schema)
  182. expected_default = schema['properties']['nationality'].get('default', '')
  183. assert nationality.input_value() == expected_default, (page.url, expected_default)
  184. if nationality.evaluate('(el) => el.tagName') == 'SELECT':
  185. assert nationality.locator('option').evaluate_all('(els) => els.slice(1).map(el => el.value)') == schema['properties']['nationality']['enum']
  186. if nationality.evaluate('(el) => el.tagName') == 'SELECT':
  187. nationality.select_option('China'); assert nationality.input_value() == 'China'
  188. else:
  189. nationality.fill('Chinese'); assert nationality.input_value() == 'Chinese'
  190. gender = page.locator('#field-gender')
  191. if gender.count():
  192. gender.select_option('female'); assert gender.input_value() == 'female'
  193. if product['id'] == 35 and locale in ['de', 'zh']:
  194. page.set_viewport_size({'width': 390, 'height': 900})
  195. page.screenshot(path=str(output / f'{locale}-netherlands-order-mobile.png'), full_page=True)
  196. if path == '/dashboard/settings':
  197. page.locator('main button').filter(has=page.locator('svg.lucide-pen')).click()
  198. inspect_view(page, '/dashboard/settings#edit-profile', locale)
  199. page.locator('main form button[type="button"]').click()
  200. page.locator('main button').filter(has_text=re.compile(r'^\[')).last.click()
  201. assert page.locator('#changepasswordmodal-field-1').is_visible()
  202. inspect_view(page, '/dashboard/settings#password-form', locale)
  203. page.locator('main .fixed.inset-0.z-50 button[aria-label]').first.click()
  204. page.set_viewport_size({'width': 390, 'height': 900})
  205. if locale == 'zh': page.screenshot(path=str(output / 'zh-settings-mobile.png'), full_page=True)
  206. if path == '/payment/90002':
  207. page.locator('main button').filter(has_text='Stripe QA fixture').click()
  208. page.locator('main a[href="https://example.invalid/qa-payment"]').wait_for(state='visible')
  209. inspect_view(page, '/payment/90002#invoice-fixture', locale)
  210. if locale == 'fr':
  211. page.set_viewport_size({'width': 390, 'height': 900})
  212. page.screenshot(path=str(output / 'fr-invoice-fixture-mobile.png'), full_page=True)
  213. # Follow each visible article conversion button to its correct localized
  214. # service using the same fully intercepted UI context. No order is submitted.
  215. for service_id, slug in [(35, 'netherlands'), (34, 'greece'), (36, 'denmark')]:
  216. page.goto(base + localize('/blogs/' + slug + '-visa-appointments-dublin', locale), wait_until='networkidle')
  217. cta = page.locator('.blog-service-button'); cta.focus(); page.keyboard.press('Enter')
  218. page.wait_for_url(base + localize('/create-order/' + str(service_id), locale), wait_until='networkidle')
  219. assert page.locator('h1').inner_text() == catalog[products_by_id[service_id]['title']][locales.index(locale)]
  220. assert page.locator('html').get_attribute('lang') == ('zh-CN' if locale == 'zh' else locale)
  221. actions.append({'locale': locale, 'articleServiceCTA': service_id, 'destination': localize('/create-order/' + str(service_id), locale), 'keyboard': True, 'api': 'Public catalogue snapshot; no order submission'})
  222. # Real DOM/native-control language change, with service ID and parameters kept.
  223. page.goto(base + localize('/create-order/35', locale) + '?routing_key=qa_route&slot_country=Netherlands&slot_city=Dublin&slot_visa_type=Tourist#details', wait_until='networkidle')
  224. target_locale = 'pt' if locale != 'pt' else 'it'
  225. page.locator('nav select:visible').select_option(target_locale)
  226. page.wait_for_url(re.compile(re.escape(base + localize('/create-order/35', target_locale)) + r'\?'), wait_until='networkidle')
  227. assert page.locator('input[name="slot_country"]').input_value() == 'Netherlands'
  228. assert page.locator('input[name="slot_city"]').input_value() == 'Dublin'
  229. assert page.locator('input[name="slot_visa_type"]').input_value() == 'Tourist'
  230. assert 'routing_key=qa_route' in page.url and page.url.endswith('#details')
  231. # Keyboard menu operation and same-locale user navigation.
  232. page.set_viewport_size({'width': 390, 'height': 900})
  233. toggle = page.locator('button[aria-controls="site-mobile-menu"]:visible')
  234. toggle.focus(); page.keyboard.press('Enter'); assert toggle.get_attribute('aria-expanded') == 'true'
  235. page.keyboard.press('Escape'); assert toggle.get_attribute('aria-expanded') == 'false'
  236. assert toggle.evaluate('(el) => el === document.activeElement')
  237. page.goto(base + localize('/dashboard', locale), wait_until='networkidle')
  238. toggle = page.locator('button[aria-controls="site-mobile-menu"]:visible')
  239. toggle.click(); page.locator('#site-mobile-menu a[href="' + localize('/dashboard', locale) + '"]').click()
  240. page.wait_for_url(base + localize('/dashboard', locale), wait_until='networkidle')
  241. actions.append({'locale': locale, 'serviceIdAndRawQueryValuesPreserved': True, 'keyboardMenuAndEscape': True, 'userSession': 'Synthetic local marker only'})
  242. context.close()
  243. browser_version = browser.version
  244. browser.close()
  245. # Remote fixture images are intentionally not fetched. Record each blocked image
  246. # separately; every JS/hydration error or other console error still fails the run.
  247. image_errors = [e for e in console_errors if 'api.text.skin/api/r2/file' in (e['source'] or '') and 'ERR_FAILED' in e['message']]
  248. unexpected = [e for e in console_errors if e not in image_errors]
  249. receipt = {'base': base, 'indexing': indexing, 'browser': browser_version, 'pages': rows, 'uiStates': states, 'actions': actions, 'apiRequests': api_calls, 'pageErrors': page_errors, 'unexpectedConsoleErrors': unexpected, 'fixtureImageErrors': image_errors, 'layoutErrors': layout_errors, 'unnamedControlErrors': control_errors, 'scope': 'Real Chromium rendering/navigation with intercepted API fixtures. Invoice creation POST is handled entirely by Playwright; no payment link is opened or confirmed. NOT real authentication, payment, booking, email, refund or image performance verification'}
  250. (output / 'app-browser-results.json').write_text(json.dumps(receipt, ensure_ascii=False, indent=2) + '\n')
  251. assert not layout_errors, layout_errors
  252. assert not control_errors, control_errors
  253. assert not page_errors, page_errors
  254. assert not unexpected, unexpected
  255. assert all(call['method'] == 'GET' or (call['method'] == 'POST' and call['path'] == '/api/vas/payment/create' and call['intercepted'] and call['synthetic']) for call in api_calls), 'Unexpected business mutation'
  256. assert sum(call['method'] == 'POST' for call in api_calls) == 7, 'Only seven intercepted invoice fixture requests are expected'
  257. print(f'PASS: {len(rows)} seven-language visitor/user pages in real Chromium; localized metadata, named controls, phone/tablet/desktop overflow, service and enum values, query-preserving language changes, client navigation and keyboard menu. API responses are isolated UI fixtures. Receipt: {output}')