check-blog-browser.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. guide = page.locator('main a[href="' + index_path(home_lang) + '"]')
  64. assert guide.count() == 1
  65. for width in [320, 390, 768, 1024, 1280, 1440]:
  66. page.set_viewport_size({'width': width, 'height': 844})
  67. assert page.evaluate('document.documentElement.scrollWidth <= innerWidth'), f'Homepage overflow {home_lang}/{width}'
  68. page.set_viewport_size({'width': 1440, 'height': 1080})
  69. page.screenshot(path=str(output / ('home-' + home_lang + '-desktop.png')), full_page=True)
  70. page.set_viewport_size({'width': 390, 'height': 844})
  71. page.screenshot(path=str(output / ('home-' + home_lang + '-mobile.png')), full_page=True)
  72. guide.focus()
  73. page.keyboard.press('Enter')
  74. page.wait_for_url(base + index_path(home_lang), wait_until='networkidle')
  75. assert page.locator('html').get_attribute('lang') == home_lang
  76. page.go_back(wait_until='networkidle')
  77. assert page.url == base + home_path
  78. assert page.locator('link[rel="canonical"]').get_attribute('href') == 'https://text.skin' + home_path
  79. 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})
  80. for locale in locales:
  81. records = [{'locale': locale, 'path': index_path(locale)}] + [dict(a, path=article_path(a)) for a in articles if a['locale'] == locale]
  82. for a in records:
  83. path = a['path']
  84. page.set_viewport_size({'width': 1440, 'height': 1080})
  85. response = load(path)
  86. for desktop_width in [1280, 1440]:
  87. page.set_viewport_size({'width': desktop_width, 'height': 1080})
  88. assert page.evaluate('document.documentElement.scrollWidth <= innerWidth'), f'Overflow {desktop_width}: {path}'
  89. assert page.locator('button[aria-controls="site-mobile-menu"]:visible').count() == 0, path
  90. assert page.locator('html').get_attribute('lang') == locale, path
  91. title = page.title()
  92. description = page.locator('meta[name="description"]').get_attribute('content')
  93. assert title and title.endswith(' | STRATA') and title not in titles, path
  94. assert description and description not in descriptions, path
  95. titles.add(title)
  96. descriptions.add(description)
  97. assert page.locator('h1').count() == 1 and page.locator('h1').text_content().strip() == title.removesuffix(' | STRATA'), path
  98. assert page.locator('link[rel="canonical"]').get_attribute('href') == 'https://text.skin' + path, path
  99. assert page.locator('meta[name="robots"]').get_attribute('content') == ('index' if indexing else 'noindex') + ', follow', path
  100. assert page.locator('meta[property="og:url"]').get_attribute('content') == 'https://text.skin' + path, path
  101. assert page.locator('meta[property="og:title"]').get_attribute('content') == title.removesuffix(' | STRATA'), path
  102. assert page.locator('meta[property="og:description"]').get_attribute('content') == description, path
  103. assert page.locator('link[rel="icon"]').get_attribute('href') == '/blog-icon.svg', path
  104. 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)}
  105. if 'en' in expected:
  106. expected['x-default'] = expected['en']
  107. alternates = page.locator('link[rel="alternate"][hreflang]').evaluate_all('(els) => Object.fromEntries(els.map(e => [e.hreflang, e.href]))')
  108. assert alternates == expected, path
  109. counterparts = [b for b in articles if b['id'] == a['id']] if 'id' in a else [{'locale': l} for l in locales]
  110. targets = [article_path(b) if 'id' in b else index_path(b['locale']) for b in counterparts]
  111. selector = page.locator('[data-blog-navigation] select:visible')
  112. assert sorted(selector.locator('option').evaluate_all('(els) => els.map(e => e.value)')) == sorted(targets), path
  113. assert selector.input_value() == path and selector.get_attribute('aria-label'), path
  114. assert 'nav.' not in page.locator('[data-blog-navigation]').inner_text(), path
  115. assert page.locator('main img').count() == 0, 'Review new images for alt/dimensions/performance'
  116. headings = page.locator('.blog-prose h2').all_inner_texts()
  117. if 'id' in a:
  118. assert headings == re.findall(r'^## (.+)$', a['body'], re.M), path
  119. assert page.locator('.blog-button').get_attribute('href') == a['officialBooking']['url'], path
  120. schema = json.loads(page.locator('script[type="application/ld+json"]').inner_text())
  121. assert schema[0]['@type'] == 'Article' and schema[0]['headline'] == a['title'], path
  122. assert schema[0]['description'] == description and schema[0]['inLanguage'] == locale, path
  123. assert schema[0]['dateModified'] == a['reviewedAt'] and 'datePublished' not in schema[0], path
  124. assert 'STRATA' in page.locator('.blog-reviewed').inner_text(), path
  125. assert schema[0]['citation'] == page.locator('.blog-sources li a').evaluate_all('(els) => els.map(e => e.href)'), path
  126. assert schema[1]['@type'] == 'BreadcrumbList' and schema[1]['itemListElement'][1]['name'] == a['title'], path
  127. assert schema[1]['itemListElement'][0]['item'] == 'https://text.skin' + index_path(locale), path
  128. assert page.locator('main a[href="' + localize('/services', locale) + '"]').count() == 1, path
  129. service_id = {'netherlands-appointment-booking': 35, 'greece-appointment-booking': 34, 'denmark-appointment-booking': 36}[a['id']]
  130. cta = page.locator('.blog-service-button')
  131. assert cta.count() == 1 and cta.get_attribute('href') == localize('/create-order/' + str(service_id), locale), path
  132. assert cta.evaluate('(el) => getComputedStyle(el).backgroundColor') == 'rgb(21, 128, 61)', path
  133. assert cta.evaluate('(el) => getComputedStyle(el).color') == 'rgb(255, 255, 255)', path
  134. cta.focus()
  135. assert cta.evaluate('(el) => el === document.activeElement && getComputedStyle(el).outlineStyle === \"solid\"'), path
  136. cta_info = {'id': service_id, 'href': cta.get_attribute('href'), 'background': 'rgb(21, 128, 61)', 'text': 'rgb(255, 255, 255)', 'keyboardFocus': True}
  137. toc = page.locator('.blog-toc a')
  138. assert toc.count() == len(headings), path
  139. for i in range(toc.count()):
  140. target = toc.nth(i).get_attribute('href')
  141. assert page.locator(target).count() == 1 and toc.nth(i).inner_text() == page.locator(target).inner_text(), path + target
  142. toc.first.focus()
  143. page.keyboard.press('Enter')
  144. target = toc.first.get_attribute('href')
  145. assert page.locator(target).evaluate('(el) => el === document.activeElement'), path
  146. assert page.locator(target).bounding_box()['y'] >= 64, path
  147. page.evaluate('window.scrollTo(0, 0)')
  148. else:
  149. assert page.locator('.blog-card').count() == len([b for b in articles if b['locale'] == locale]), path
  150. assert page.locator('.blog-routes tbody tr').count() == 3, path
  151. for b in [b for b in articles if b['locale'] == locale]:
  152. assert page.locator('.blog-routes a[href="' + article_path(b) + '"]').count() == 1, path
  153. assert page.locator('.blog-routes a[href="' + b['officialBooking']['url'] + '"]').count() == 1, path
  154. assert page.locator('footer a[href="mailto:support@text.skin"]').count() == 1, path
  155. table = page.locator('.blog-table-scroll')
  156. assert table.count() == 1 and table.get_attribute('role') == 'region', path
  157. assert table.get_attribute('tabindex') == '0' and (table.get_attribute('aria-label') or table.get_attribute('aria-labelledby')), path
  158. assert table.locator('thead th[scope="col"]').count() == 3, path
  159. assert table.locator('tbody tr').count() == (4 if 'id' in a else 3), path
  160. for href in page.locator('a[href]').evaluate_all('(els) => els.map(e => e.getAttribute("href"))'):
  161. if href.startswith('#'):
  162. assert page.locator(href).count() == 1, path + href
  163. elif href.startswith('/') and not href.startswith('//'):
  164. target = href.split('#')[0].split('?')[0]
  165. if target not in links:
  166. links[target] = context.request.get(base + target).status
  167. assert links[target] == 200, path + ' → ' + target
  168. shot = locale + '-' + (a.get('slug') or 'index')
  169. page.screenshot(path=str(output / (shot + '-desktop.png')), full_page=True)
  170. answer_y = None
  171. for width in [320, 390, 768, 1024]:
  172. page.set_viewport_size({'width': width, 'height': 844})
  173. assert page.evaluate('document.documentElement.scrollWidth <= innerWidth'), f'Overflow {width}: {path}'
  174. if width == 390:
  175. page.evaluate('window.scrollTo(0, 0)')
  176. if 'id' in a:
  177. answer_y = page.locator('.blog-prose > p').first.bounding_box()['y']
  178. assert answer_y < 620, f'Answer delayed: {path}: {answer_y}'
  179. page.screenshot(path=str(output / (shot + '-mobile.png')), full_page=True)
  180. page.screenshot(path=str(output / (shot + '-viewport.png')))
  181. toggle = page.locator('button[aria-controls="site-mobile-menu"]:visible')
  182. toggle.focus()
  183. page.keyboard.press('Enter')
  184. assert toggle.get_attribute('aria-expanded') == 'true', path
  185. page.locator('#site-mobile-menu a[href="' + index_path(locale) + '"]').focus()
  186. page.keyboard.press('Escape')
  187. assert toggle.get_attribute('aria-expanded') == 'false' and toggle.evaluate('(el) => el === document.activeElement'), path
  188. page.set_viewport_size({'width': 390, 'height': 844})
  189. table.evaluate('(el) => { el.scrollLeft = 0; }')
  190. table.focus()
  191. assert table.evaluate('(el) => el === document.activeElement && el.scrollWidth > el.clientWidth'), path
  192. page.keyboard.press('ArrowRight')
  193. page.wait_for_function('document.querySelector(".blog-table-scroll").scrollLeft > 0')
  194. table.evaluate('(el) => { el.scrollLeft = 0; }')
  195. if 'id' in a:
  196. byline = page.locator('.blog-reviewed a[href="#blog-sources"]')
  197. byline.focus()
  198. page.keyboard.press('Enter')
  199. assert page.locator('#blog-sources').evaluate('(el) => el === document.activeElement'), path
  200. assert page.locator('#blog-sources').bounding_box()['y'] >= 64, path
  201. correction = page.locator('.blog-sources a[href^="mailto:support@text.skin?"]')
  202. assert correction.count() == 1 and 'subject=' in correction.get_attribute('href'), path
  203. for b in counterparts:
  204. target = article_path(b) if 'id' in b else index_path(b['locale'])
  205. if page.locator('[data-blog-navigation] select:visible').input_value() != target:
  206. page.locator('[data-blog-navigation] select:visible').select_option(target)
  207. page.wait_for_url(base + target, wait_until='networkidle')
  208. assert page.locator('html').get_attribute('lang') == b['locale'], target
  209. if 'id' in b:
  210. assert page.locator('h1').text_content().strip() == b['title'], target
  211. assert page.locator('[data-blog-navigation] select:visible').input_value() == target, target
  212. load(path)
  213. page.reload(wait_until='networkidle')
  214. assert page.locator('html').get_attribute('lang') == locale, path
  215. page.locator('.blog-breadcrumb a' if 'id' in a else '.blog-card h2 a').first.click()
  216. page.wait_for_load_state('networkidle')
  217. assert page.locator('html').get_attribute('lang') == locale, path
  218. page.go_back(wait_until='networkidle')
  219. assert page.url == base + path and page.locator('h1').text_content().strip() == title.removesuffix(' | STRATA'), path
  220. load(path)
  221. page.keyboard.press('Tab')
  222. assert page.locator('.blog-skip').evaluate('(el) => el === document.activeElement'), path
  223. page.keyboard.press('Enter')
  224. assert page.locator('#blog-main').evaluate('(el) => el === document.activeElement'), path
  225. 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']})
  226. load('/fr/blogs')
  227. page.locator('button[aria-controls="site-mobile-menu"]:visible').click()
  228. page.locator('#site-mobile-menu a[href="/fr/services"]').click()
  229. page.wait_for_url(base + '/fr/services', wait_until='networkidle')
  230. page.go_back(wait_until='networkidle')
  231. assert page.url == base + '/fr/blogs' and page.locator('html').get_attribute('lang') == 'fr'
  232. assert page.locator('button[aria-controls="site-mobile-menu"]:visible').get_attribute('aria-expanded') == 'false'
  233. # Keep the specific isolated-API error and pre-existing app favicon failure in
  234. # the receipt. Both occur on /services, outside the blog implementation.
  235. # Do not suppress blog errors or any other resource/network failure.
  236. 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']
  237. 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)']
  238. unexpected_errors = [e for e in console_errors if e not in expected_preview_errors + known_app_errors]
  239. 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'}
  240. (output / 'browser-results.json').write_text(json.dumps(receipt, ensure_ascii=False, indent=2))
  241. assert not errors, errors
  242. assert not unexpected_errors, unexpected_errors
  243. browser.close()
  244. 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}')