【发布时间】: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
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