Jenkins运行RobotFramework脚本失败求助
问题:AWS Linux服务器Jenkins中运行RobotFramework脚本失败
在AWS Linux服务器部署的Jenkins上执行RobotFramework脚本时,.robot文件任务持续失败,已尝试StackOverflow同类问题方案但未解决,需求是成功运行*.robot脚本。
详细信息
1. Robot脚本内容
*** Settings *** Documentation Basic Test Library SeleniumLibrary *** Variables *** #${result} /opt/google/chrome/chrome ${Browser} chrome ${options} binary_location = '/opt/google/chrome/chrome' *** Test Cases *** Abrir web Pangea Open Browser url=https://www.google.com/ browser=${Browser} options=${options} Close All Browsers
2. Jenkins构建步骤
robot --outputdir results --nostatusrc Test/basic_2.robot
3. 控制台错误信息
[Robot_QA_Executions] $ /bin/sh -xe /tmp/jenkins3290544249294601087.sh + robot --outputdir results --nostatusrc Test/basic_2.robot ============================================================================== Basic 2 :: Basic Test ============================================================================== Abrir web Pangea | FAIL | WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /opt/google/chrome/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) ------------------------------------------------------------------------------ Basic 2 :: Basic Test | FAIL | 1 test, 0 passed, 1 failed ==============================================================================
4. 可正常运行的Python Selenium脚本对比
脚本内容
from selenium import webdriver from selenium.webdriver.chrome.options import Options import time # Ruta del controlador de Chrome webdriver.ChromeOptions.binary_location = '/opt/google/chrome/chrome' # Opciones para el navegador ChromeOptions = webdriver.ChromeOptions() ChromeOptions.add_argument('--headless') # Crea una instancia del navegador Chrome driver = webdriver.Chrome(chrome_options=ChromeOptions) # Navega a la pagina web de Google Espana driver.get("https://www.google.es") print("google abierto") time.sleep(2) driver.get("https://pangea.es/") print("pangea abierto") # Cierra el navegador time.sleep(10) driver.quit() print("fin del test")
Jenkins构建步骤
#!/usr/bin/env python3 ./Test/ejemplo_Python_2.py
5. 环境信息
$ /opt/google/chrome/chrome --version Google Chrome 110.0.5481.177 $ chromedriver --version Starting ChromeDriver 110.0.5481.77 (65ed616c6e8ee3fe0ad64fe83796c020644d42af-refs/branch-heads/5481@{#839}) on port 9515 pip (9.0.3) Pygments (2.14.0) robotframework (6.0.2) robotframework-datadriver (1.6.1) robotframework-pythonlibcore (3.0.0) robotframework-seleniumlibrary (6.0.0) selenium (3.141.0) setuptools (39.2.0) urllib3 (1.26.14)
额外尝试
使用Create Webdriver关键字替代Open Browser,仍报错:
WebDriverException: Message: Service /opt/google/chrome/chrome unexpectedly exited. Status code was: 1
解决方案
问题根源在于Robot脚本中Chrome参数配置错误:
- 原脚本将
options设为字符串,而非实际的ChromeOptions对象 - 缺少Linux无头环境下运行Chrome的必要参数(如
--headless、--no-sandbox等)
修改后的Robot脚本如下:
*** Settings *** Documentation Basic Test Library SeleniumLibrary *** Variables *** ${Browser} chrome ${ChromeBinary} /opt/google/chrome/chrome *** Test Cases *** Abrir web Pangea # 创建ChromeOptions实例 ${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver # 添加无头模式参数(适配Chrome 110+版本) Call Method ${chrome_options} add_argument --headless=new # 解决Linux权限问题 Call Method ${chrome_options} add_argument --no-sandbox # 解决/dev/shm内存不足问题 Call Method ${chrome_options} add_argument --disable-dev-shm-usage # 设置Chrome二进制文件路径 Call Method ${chrome_options} set_binary_location ${ChromeBinary} # 打开浏览器 Open Browser url=https://www.google.com/ browser=${Browser} options=${chrome_options} Close All Browsers
修改说明
- 通过
Evaluate关键字直接创建Selenium的ChromeOptions对象,而非字符串变量 - 添加
--headless=new启用无头模式(Linux服务器无图形界面必须配置) --no-sandbox绕过Chrome的沙箱机制,解决Jenkins运行时的权限限制--disable-dev-shm-usage避免Linux系统中/dev/shm分区内存不足导致Chrome崩溃- 用
Call Method正确设置Chrome二进制文件路径
保持原Jenkins构建步骤不变,重新执行即可成功运行脚本。
内容的提问来源于stack exchange,提问作者Pablo Gamazo
相关产品推荐
相关产品推荐

