【问题标题】:GitLab job randomly failing with Selenium showing white screenGitLab 作业随机失败,Selenium 显示白屏
【发布时间】:2022-01-25 19:14:57
【问题描述】:

我正在 GitLab CI(免费层)上运行简单的 selenium 测试。

有时我会因为找不到元素而工作失败(截屏后页面显示为白屏)。

在运行几次失败的作业后,它开始工作。很难复制,因为它非常罕见。

如何获得更多信息这是什么类型的问题?跑步者或其他人的一些记忆问题?

硒脚本示例:

import unittest
from selenium import webdriver


class SmokeTests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        self.driver = webdriver.Chrome(options=chrome_options)

        base_url = 'https://google.com'
        self.driver.get(base_url)

    @classmethod
    def tearDownClass(self):
        self.driver.quit()

    def test_name(self):
        expected_title = 'Google'
        observed_title = self.driver.title
        self.assertEqual(expected_title, observed_title)

编辑

多次运行作业时的样子

当找不到标题时,日志包含标准信息:

AssertionError: 'Google' != ''
- Google
+ 
----------------------------------------------------------------------
Ran 1 test in 1.193s
FAILED (failures=1)
tearDown
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1

我在不同的页面/服务器/存储库上遇到了这个问题。可能的问题是共享跑步者内存不足,因此使用以下选项可能会有所帮助:

options.add_argument('--disable-dev-shm-usage')

但由于失败的随机性,无法对其进行测试。

CI 脚本:

test staging ui:
  image: jaktestowac/python-chromedriver:3.9-selenium
  before_script:
    - pip install -r tests/requirements.txt
  script:
    - python -m unittest tests/smoke_tests.py

用于测试的图片:https://hub.docker.com/layers/jaktestowac/python-chromedriver/3.9-selenium/images/sha256-5feb585b9ebdac3f5191e7c24f29e8e78beb4f69c9bc734d7050157544f4653f?context=explore

EDIT2

这里不使用等待(测试了 20 秒)。由于这个随机错误,页面根本没有加载(标题元素不可用)。

目前我正在尝试通过日志记录找到一些线索:

    d = DesiredCapabilities.CHROME
    d['goog:loggingPrefs'] = { 'browser':'ALL' }
    self.driver = webdriver.Chrome(desired_capabilities=d, options=self.chrome_options)

【问题讨论】:

  • 堆栈跟踪/错误跟踪日志说什么?
  • 您正在测试的网站是字面意义上的google.com 吗?这可能只是页面加载中的竞争条件问题。您的测试似乎没有对预期条件进行任何等待,因此页面加载事件可能会完成(意味着driver.get 停止阻塞)并且您尝试过早地对该元素采取行动。相反,使用 WebDriverWait(driver, timeout=5).until(...) 将阻塞,直到元素可用(或发生超时)。如果页面本身确实无法加载,您可以考虑对 flakey 测试进行重试逻辑。
  • @DebanjanB 添加了日志和一些修复线索 - 但此日志表明在 CI 机器上测试的内容为空页面(我在失败期间设法获取屏幕截图)
  • @sytech 我没有直接测试 google.com - 我正在测试部署到不同服务器的许多站点。在不同的部署中观察到了这个问题,因此它与我的环境没有紧密的关系。有些脚本就像这个插入问题一样简单。也许这是巧合,但我认为这是共享跑步者和内存的问题。添加超时可以是一个选项,但我真的需要知道如何在 GitLab 上调试这个问题。我添加了日志和一些修复线索。

标签: python selenium google-chrome selenium-chromedriver gitlab-ci


【解决方案1】:

截屏后页面显示为白屏可以解释为网页在尝试截取screenshot时没有完全render

理想情况下,在验证 Page Title 之前,您需要为以下任一情况诱导 WebDriverWait

  • visibility_of_element_located() 用于DOM Tree 中的任何可见 元素。例如,等待搜索框如下所示:

    def test_name(self):
        WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.NAME, "q")))
        expected_title = 'Google'
        observed_title = self.driver.title
        self.assertEqual(expected_title, observed_title)
    
  • element_to_be_clickable() 用于HTML DOM 中的任何可点击 元素。例如,等待 搜索框 可点击如下:

    def test_name(self):
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
        expected_title = 'Google'
        observed_title = self.driver.title
        self.assertEqual(expected_title, observed_title)
    

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

【讨论】:

  • 感谢您的回答,但我之前测试过等待,白页是持久状态。我正在寻找更多关于调试 GitLab 运行器、浏览器状态问题调试的解决方案,以找出这些随机问题的根源。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2018-06-05
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
相关资源
最近更新 更多