check-blog-browser.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. """Real local Chromium QA for the public homepage, 21 guides and seven indexes.
  2. Requires Python Playwright and Chromium; CHROMIUM_PATH can select an installed
  3. browser. Tests a fresh logged-out context, never a booking or login submission.
  4. BLOG_INDEXING_ENABLED must match the build. Output is a local QA receipt, not
  5. field Core Web Vitals or an authenticated end-to-end test.
  6. """
  7. import json
  8. import os
  9. import re
  10. from pathlib import Path
  11. from playwright.sync_api import sync_playwright
  12. base = os.environ.get('BLOG_TEST_URL', 'http://127.0.0.1:3108').rstrip('/')
  13. indexing = os.environ.get('BLOG_INDEXING_ENABLED') == 'true'
  14. all_articles = json.loads(Path('src/content/blog/articles.json').read_text())
  15. articles = [a for a in all_articles if not indexing or a['published']]
  16. published = [a for a in all_articles if a['published']]
  17. locales = ['en', 'de', 'fr', 'es', 'it', 'pt', 'zh']
  18. output = Path(os.environ.get('BLOG_SCREENSHOT_DIR', 'output/playwright'))
  19. output.mkdir(parents=True, exist_ok=True)
  20. rows, errors, console_errors, links = [], [], [], {}
  21. home_rows = []
  22. titles, descriptions = set(), set()
  23. def index_path(locale):
  24. return ('' if locale == 'en' else '/' + locale) + '/blogs'
  25. def article_path(article):
  26. return index_path(article['locale']) + '/' + article['slug']
  27. with sync_playwright() as p:
  28. options = {'headless': True}
  29. if os.environ.get('CHROMIUM_PATH'):
  30. options['executable_path'] = os.environ['CHROMIUM_PATH']
  31. browser = p.chromium.launch(**options)
  32. context = browser.new_context(viewport={'width': 1440, 'height': 1080}, locale='zh-CN')
  33. # Saved app language and browser language must not override the blog route.
  34. context.add_init_script("localStorage.setItem('app_lang', 'zh')")
  35. page = context.new_page()
  36. page.on('pageerror', lambda e: errors.append({'url': page.url, 'error': str(e)}))
  37. page.on('console', lambda m: console_errors.append({'url': page.url, 'sourceUrl': m.location.get('url'), 'error': m.text}) if m.type == 'error' else None)
  38. def load(path):
  39. response = page.goto(base + path, wait_until='networkidle')
  40. assert response and response.status == 200, path
  41. assert page.locator('[data-blog-navigation] select:visible').count() == 1, path
  42. return response
  43. public_copy = json.loads(Path('src/lib/i18n/public-pages.json').read_text())
  44. def localize(path, locale):
  45. return path if locale == 'en' else '/' + locale + ('' if path == '/' else path)
  46. for home_lang in locales:
  47. home_path = localize('/', home_lang)
  48. page.set_viewport_size({'width': 1440, 'height': 1080})
  49. home_response = page.goto(base + home_path, wait_until='networkidle')
  50. assert home_response and home_response.status == 200
  51. assert page.locator('html').get_attribute('lang') == ('zh-CN' if home_lang == 'zh' else home_lang)
  52. assert page.locator('h1').count() == 1
  53. assert page.title() == public_copy[home_lang]['/'][0] + ' | STRATA'
  54. assert page.locator('meta[name="description"]').get_attribute('content') == public_copy[home_lang]['/'][1]
  55. robots = 'index, follow' if indexing or home_lang == 'en' else 'noindex, follow'
  56. assert page.locator('meta[name="robots"]').get_attribute('content') == robots
  57. assert page.locator('link[rel="canonical"]').count() == 1
  58. assert page.locator('link[rel="canonical"]').get_attribute('href') == 'https://text.skin' + home_path
  59. assert page.locator('meta[property="og:url"]').get_attribute('content') == 'https://text.skin' + home_path
  60. assert page.locator('link[rel="alternate"][hreflang]').count() == 8
  61. assert page.locator('nav select:visible').input_value() == home_lang
  62. assert page.locator('link[rel="icon"]').get_attribute('href') == '/blog-icon.svg'
  63. # The homepage hero keeps its original title and two primary actions.
  64. # The existing shared Navbar still provides the localized blog entry.
  65. assert page.locator('main a[href="' + index_path(home_lang) + '"]').count() == 0
  66. guide = page.locator('nav a[href="' + index_path(home_lang) + '"]:visible').first
  67. assert guide.count() == 1
  68. for width in [320, 390, 768, 1024, 1280, 1440]:
  69. page.set_viewport_size({'width': width, 'height': 844})
  70. assert page.evaluate('document.documentElement.scrollWidth <= innerWidth'), f'Homepage overflow {home_lang}/{width}'
  71. page.set_viewport_size({'width': 1440, 'height': 1080})
  72. page.screenshot(path=str(output / ('home-' + home_lang + '-desktop.png')), full_page=True)
  73. page.set_viewport_size({'width': 390, 'height': 844})
  74. page.screenshot(path=str(output / ('home-' + home_lang + '-mobile.png')), full_page=True)
  75. page.set_viewport_size({'width': 1440, 'height': 1080})
  76. guide.focus()
  77. page.keyboard.press('Enter')
  78. page.wait_for_url(base + index_path(home_lang), wait_until='networkidle')
  79. assert page.locator('html').get_attribute('lang') == home_lang
  80. page.go_back(wait_until='networkidle')
  81. assert page.url == base + home_path
  82. assert page.locator('link[rel="canonical"]').get_attribute('href') == 'https://text.skin' + home_path
  83. home_rows.append({'path': home_path, 'status': home_response.status, 'uiLanguage': home_lang, 'serverMetadataLanguage': home_lang, 'canonical': 'https://text.skin' + home_path, 'robots': robots, 'widthsWithoutOverflow': [320, 390, 768, 1024, 1280, 1440], 'keyboardGuideLink': True, 'historyCanonicalRestored': True})
  84. for locale in locales:
  85. records = [{'locale': locale, 'path': index_path(locale)}] + [dict(a, path=article_path(a)) for a in articles if a['locale'] == locale]
  86. for a in records:
  87. path = a['path']
  88. page.set_viewport_size({'width': 1440, 'height': 1080})
  89. response = load(path)
  90. for desktop_width in [1280, 1440]:
  91. page.set_viewport_size({'width': desktop_width, 'height': 1080})
  92. assert page.evaluate('document.documentElement.scrollWidth <= innerWidth'), f'Overflow {desktop_width}: {path}'
  93. assert page.locator('button[aria-controls="site-mobile-menu"]:visible').count() == 0, path
  94. assert page.locator('html').get_attribute('lang') == locale, path
  95. title = page.title()
  96. description = page.locator('meta[name="description"]').get_attribute('content')
  97. assert title and title.endswith(' | STRATA') and title not in titles, path
  98. assert description and description not in descriptions, path
  99. titles.add(title)
  100. descriptions.add(description)
  101. assert page.locator('h1').count() == 1 and page.locator('h1').text_content().strip() == title.removesuffix(' | STRATA'), path
  102. assert page.locator('link[rel="canonical"]').get_attribute('href') == 'https://text.skin' + path, path
  103. assert page.locator('meta[name="robots"]').get_attribute('content') == ('index' if indexing else 'noindex') + ', follow', path
  104. assert page.locator('meta[property="og:url"]').get_attribute('content') == 'https://text.skin' + path, path
  105. assert page.locator('meta[property="og:title"]').get_attribute('content') == title.removesuffix(' | STRATA'), path
  106. assert page.locator('meta[property="og:description"]').get_attribute('content') == description, path
  107. assert page.locator('link[rel="icon"]').get_attribute('href') == '/blog-icon.svg', path
  108. expected = {b['locale']: 'https://text.skin' + article_path(b) for b in published if b['id'] == a['id']} if 'id' in a else {l: 'https://text.skin' + index_path(l) for l in locales if any(b['locale'] == l for b in published)}
  109. if 'en' in expected:
  110. expected['x-default'] = expected['en']
  111. alternates = page.locator('link[rel="alternate"][hreflang]').evaluate_all('(els) => Object.fromEntries(els.map(e => [e.hreflang, e.href]))')
  112. assert alternates == expected, path
  113. counterparts = [b for b in articles if b['id'] == a['id']] if 'id' in a else [{'locale': l} for l in locales]
  114. targets = [article_path(b) if 'id' in b else index_path(b['locale']) for b in counterparts]
  115. selector = page.locator('[data-blog-navigation] select:visible')
  116. assert sorted(selector.locator('option').evaluate_all('(els) => els.map(e => e.value)')) == sorted(targets), path
  117. assert selector.input_value() == path and selector.get_attribute('aria-label'), path
  118. assert 'nav.' not in page.locator('[data-blog-navigation]').inner_text(), path
  119. assert page.locator('main img').count() == 0, 'Review new images for alt/dimensions/performance'
  120. headings = page.locator('.blog-prose h2').all_inner_texts()
  121. if 'id' in a:
  122. assert headings == re.findall(r'^## (.+)$', a['body'], re.M), path
  123. assert page.locator('.blog-button').get_attribute('href') == a['officialBooking']['url'], path
  124. schema = json.loads(page.locator('script[type="application/ld+json"]').inner_text())
  125. assert schema[0]['@type'] == 'Article' and schema[0]['headline'] == a['title'], path
  126. assert schema[0]['description'] == description and schema[0]['inLanguage'] == locale, path
  127. assert schema[0]['dateModified'] == a['reviewedAt'] and 'datePublished' not in schema[0], path
  128. assert 'STRATA' in page.locator('.blog-reviewed').inner_text(), path
  129. assert schema[0]['citation'] == page.locator('.blog-sources li a').evaluate_all('(els) => els.map(e => e.href)'), path
  130. assert schema[1]['@type'] == 'BreadcrumbList' and schema[1]['itemListElement'][1]['name'] == a['title'], path
  131. assert schema[1]['itemListElement'][0]['item'] == 'https://text.skin' + index_path(locale), path
  132. assert page.locator('main a[href="' + localize('/services', locale) + '"]').count() == 1, path
  133. service_id = {'netherlands-appointment-booking': 35, 'greece-appointment-booking': 34, 'denmark-appointment-booking': 36}[a['id']]
  134. cta = page.locator('.blog-service-button')
  135. assert cta.count() == 1 and cta.get_attribute('href') == localize('/create-order/' + str(service_id), locale), path
  136. assert cta.evaluate('(el) => getComputedStyle(el).backgroundColor') == 'rgb(21, 128, 61)', path
  137. assert cta.evaluate('(el) => getComputedStyle(el).color') == 'rgb(255, 255, 255)', path
  138. cta.focus()
  139. assert cta.evaluate('(el) => el === document.activeElement && getComputedStyle(el).outlineStyle === \"solid\"'), path
  140. cta_info = {'id': service_id, 'href': cta.get_attribute('href'), 'background': 'rgb(21, 128, 61)', 'text': 'rgb(255, 255, 255)', 'keyboardFocus': True}
  141. toc = page.locator('.blog-toc a')
  142. assert toc.count() == len(headings), path
  143. for i in range(toc.count()):
  144. target = toc.nth(i).get_attribute('href')
  145. assert page.locator(target).count() == 1 and toc.nth(i).inner_text() == page.locator(target).inner_text(), path + target
  146. toc.first.focus()
  147. page.keyboard.press('Enter')
  148. target = toc.first.get_attribute('href')
  149. assert page.locator(target).evaluate('(el) => el === document.activeElement'), path
  150. assert page.locator(target).bounding_box()['y'] >= 64, path
  151. page.evaluate('window.scrollTo(0, 0)')
  152. else:
  153. assert page.locator('.blog-card').count() == len([b for b in articles if b['locale'] == locale]), path
  154. assert page.locator('.blog-routes tbody tr').count() == 3, path
  155. for b in [b for b in articles if b['locale'] == locale]:
  156. assert page.locator('.blog-routes a[href="' + article_path(b) + '"]').count() == 1, path
  157. assert page.locator('.blog-routes a[href="' + b['officialBooking']['url'] + '"]').count() == 1, path
  158. assert page.locator('footer a[href="mailto:support@text.skin"]').count() == 1, path
  159. table = page.locator('.blog-table-scroll')
  160. assert table.count() == 1 and table.get_attribute('role') == 'region', path
  161. assert table.get_attribute('tabindex') == '0' and (table.get_attribute('aria-label') or table.get_attribute('aria-labelledby')), path
  162. assert table.locator('thead th[scope="col"]').count() == 3, path
  163. assert table.locator('tbody tr').count() == (4 if 'id' in a else 3), path
  164. for href in page.locator('a[href]').evaluate_all('(els) => els.map(e => e.getAttribute("href"))'):
  165. if href.startswith('#'):
  166. assert page.locator(href).count() == 1, path + href
  167. elif href.startswith('/') and not href.startswith('//'):
  168. target = href.split('#')[0].split('?')[0]
  169. if target not in links:
  170. links[target] = context.request.get(base + target).status
  171. assert links[target] == 200, path + ' → ' + target
  172. shot = locale + '-' + (a.get('slug') or 'index')
  173. page.screenshot(path=str(output / (shot + '-desktop.png')), full_page=True)
  174. answer_y = None
  175. for width in [320, 390, 768, 1024]:
  176. page.set_viewport_size({'width': width, 'height': 844})
  177. assert page.evaluate('document.documentElement.scrollWidth <= innerWidth'), f'Overflow {width}: {path}'
  178. if width == 390:
  179. page.evaluate('window.scrollTo(0, 0)')
  180. if 'id' in a:
  181. answer_y = page.locator('.blog-prose > p').first.bounding_box()['y']
  182. assert answer_y < 620, f'Answer delayed: {path}: {answer_y}'
  183. page.screenshot(path=str(output / (shot + '-mobile.png')), full_page=True)
  184. page.screenshot(path=str(output / (shot + '-viewport.png')))
  185. toggle = page.locator('button[aria-controls="site-mobile-menu"]:visible')
  186. toggle.focus()
  187. page.keyboard.press('Enter')
  188. assert toggle.get_attribute('aria-expanded') == 'true', path
  189. page.locator('#site-mobile-menu a[href="' + index_path(locale) + '"]').focus()
  190. page.keyboard.press('Escape')
  191. assert toggle.get_attribute('aria-expanded') == 'false' and toggle.evaluate('(el) => el === document.activeElement'), path
  192. page.set_viewport_size({'width': 390, 'height': 844})
  193. table.evaluate('(el) => { el.scrollLeft = 0; }')
  194. table.focus()
  195. assert table.evaluate('(el) => el === document.activeElement && el.scrollWidth > el.clientWidth'), path
  196. page.keyboard.press('ArrowRight')
  197. page.wait_for_function('document.querySelector(".blog-table-scroll").scrollLeft > 0')
  198. table.evaluate('(el) => { el.scrollLeft = 0; }')
  199. if 'id' in a:
  200. byline = page.locator('.blog-reviewed a[href="#blog-sources"]')
  201. byline.focus()
  202. page.keyboard.press('Enter')
  203. assert page.locator('#blog-sources').evaluate('(el) => el === document.activeElement'), path
  204. assert page.locator('#blog-sources').bounding_box()['y'] >= 64, path
  205. correction = page.locator('.blog-sources a[href^="mailto:support@text.skin?"]')
  206. assert correction.count() == 1 and 'subject=' in correction.get_attribute('href'), path
  207. for b in counterparts:
  208. target = article_path(b) if 'id' in b else index_path(b['locale'])
  209. if page.locator('[data-blog-navigation] select:visible').input_value() != target:
  210. page.locator('[data-blog-navigation] select:visible').select_option(target)
  211. page.wait_for_url(base + target, wait_until='networkidle')
  212. assert page.locator('html').get_attribute('lang') == b['locale'], target
  213. if 'id' in b:
  214. assert page.locator('h1').text_content().strip() == b['title'], target
  215. assert page.locator('[data-blog-navigation] select:visible').input_value() == target, target
  216. load(path)
  217. page.reload(wait_until='networkidle')
  218. assert page.locator('html').get_attribute('lang') == locale, path
  219. page.locator('.blog-breadcrumb a' if 'id' in a else '.blog-card h2 a').first.click()
  220. page.wait_for_load_state('networkidle')
  221. assert page.locator('html').get_attribute('lang') == locale, path
  222. page.go_back(wait_until='networkidle')
  223. assert page.url == base + path and page.locator('h1').text_content().strip() == title.removesuffix(' | STRATA'), path
  224. load(path)
  225. page.keyboard.press('Tab')
  226. assert page.locator('.blog-skip').evaluate('(el) => el === document.activeElement'), path
  227. page.keyboard.press('Enter')
  228. assert page.locator('#blog-main').evaluate('(el) => el === document.activeElement'), path
  229. rows.append({'path': path, 'status': response.status, 'locale': locale, 'title': title, 'titleCharacters': len(title), 'descriptionCharacters': len(description), 'h2': len(headings), 'renderedH2': page.locator('main h2').count(), 'tableKeyboardScrolling': True, 'correctionLinksValidated': True, 'firstAnswerY390': answer_y, 'canonical': 'https://text.skin' + path, 'robots': ('index' if indexing else 'noindex') + ', follow', 'hreflang': alternates, 'counterpartsTested': targets, 'widthsWithoutOverflow': [320, 390, 768, 1024, 1280, 1440], 'serviceCTA': cta_info if 'id' in a else None, 'screenshots': [shot + '-desktop.png', shot + '-mobile.png', shot + '-viewport.png']})
  230. load('/fr/blogs')
  231. page.locator('button[aria-controls="site-mobile-menu"]:visible').click()
  232. page.locator('#site-mobile-menu a[href="/fr/services"]').click()
  233. page.wait_for_url(base + '/fr/services', wait_until='networkidle')
  234. page.go_back(wait_until='networkidle')
  235. assert page.url == base + '/fr/blogs' and page.locator('html').get_attribute('lang') == 'fr'
  236. assert page.locator('button[aria-controls="site-mobile-menu"]:visible').get_attribute('aria-expanded') == 'false'
  237. # Keep the specific isolated-API error and pre-existing app favicon failure in
  238. # the receipt. Both occur on /services, outside the blog implementation.
  239. # Do not suppress blog errors or any other resource/network failure.
  240. expected_preview_errors = [e for e in console_errors if re.fullmatch(re.escape(base) + r'/(?:[a-z]{2}/)?services', e['url']) and e['sourceUrl'] == 'http://127.0.0.1:9/api/vas/product/list-enable?size=1000' and e['error'] == 'Failed to load resource: net::ERR_UNSAFE_PORT']
  241. known_app_errors = [e for e in console_errors if re.fullmatch(re.escape(base) + r'/(?:[a-z]{2}/)?services', e['url']) and e['sourceUrl'] == base + '/favicon.ico' and e['error'] == 'Failed to load resource: the server responded with a status of 404 (Not Found)']
  242. unexpected_errors = [e for e in console_errors if e not in expected_preview_errors + known_app_errors]
  243. receipt = {'base': base, 'indexing': indexing, 'browser': browser.version, 'homepage': home_rows, 'pages': rows, 'internalLinkStatuses': links, 'pageErrors': errors, 'consoleErrors': console_errors, 'expectedPreviewErrors': expected_preview_errors, 'knownNonBlogErrors': known_app_errors, 'unexpectedConsoleErrors': unexpected_errors, 'authentication': 'Fresh logged-out context; no real login or booking availability checked'}
  244. (output / 'browser-results.json').write_text(json.dumps(receipt, ensure_ascii=False, indent=2))
  245. assert not errors, errors
  246. assert not unexpected_errors, unexpected_errors
  247. browser.close()
  248. print(f'PASS: seven route-localized homepages and {len(rows)} real browser blog pages; all locale counterparts, metadata/schema/visible content, 320/390/768/1024/1280/1440px overflow, desktop navigation, contents/skip/menu/table/source keyboard controls, correction links, internal links, history/reload and no unexpected page/console errors. Expected offline API errors on services: {len(expected_preview_errors)}; known app favicon errors: {len(known_app_errors)}. Screenshots: {output}')