AWS Windows Server 2019运行Selenium测试时Chrome崩溃问题求助
Selenium+Cucumber项目在AWS CodeBuild运行报错:Chrome启动失败
问题背景
我用Cucumber框架搭建了一个Selenium项目,test/java目录下包含feature文件、TestRunner和Testbase类。本地运行正常,能生成测试用例,但在AWS CodeBuild中运行时抛出错误:
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed.
已尝试的解决方法
- 通过pom.xml直接使用Maven配置依赖
- 调用
WebDriverManager.chromedriver().setup()配置ChromeDriver - 手动指定Chrome二进制文件路径(chrome.exe的路径)
相关配置文件和代码
buildspec.yml
version: 0.2 phases: install: runtime-versions: java: corretto17 commands: - echo "Install dependencies" - choco install -y googlechrome - choco install -y chromedriver - choco install -y maven - choco install -y nodejs-lts - npm install -g selenium-webdriver - npm install -g cucumberVersion /value - chromeDriver -v pre_build: commands: - dir - echo "Compile source code" - mvn clean - mvn install build: commands: - echo "Run tests" - mvn test
TestBaseClass.java
package Util; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class TestBaseClass { public static WebDriver driver; public static Properties prop; public TestBaseClass() { try { prop = new Properties(); FileInputStream fls = new FileInputStream("src\\test\\java\\Config\\config.properties"); prop.load(fls); }catch (IOException e) { e.getMessage(); } } public static void initializationWithKey(String KeyOfURL) { String sbrowser = prop.getProperty("browser"); String sUrl = prop.getProperty(KeyOfURL); if(sbrowser.equalsIgnoreCase("chrome")) { WebDriverManager.chromedriver().setup(); // System.setProperty("webdriver.chrome.driver", "C:\\ProgramData\\chocolatey\\lib\\chromedriver\\tools\\chromedriver.exe"); // System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\drivers\\chromedriver.exe"); // this code is added to fix the chrome browser issue ChromeOptions co = new ChromeOptions(); co.addArguments("--disable-dev-shm-usage"); // co.addArguments("--ignore-ssl-errors=yes"); // co.addArguments("--ignore-certificate-errors"); co.addArguments("--remote-allow-origins=*"); co.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"); driver = new ChromeDriver(co); }else if(sbrowser.equalsIgnoreCase("firefox")) { System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\Drivers\\geckodriver.exe"); // System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe"); driver = new FirefoxDriver(); } driver.manage().window().maximize(); driver.manage().deleteAllCookies(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.get(sUrl); } public static void closeBrowser() { driver.quit(); } }
解决思路与方案
AWS CodeBuild默认是无桌面环境的,Chrome无法在无GUI环境下启动,这是核心问题。需要调整Chrome配置和构建脚本:
1. 给Chrome添加无头模式及必要参数
修改TestBaseClass中ChromeOptions的配置,添加无头模式、禁用沙箱等参数,同时移除硬编码的Chrome二进制路径(WebDriverManager会自动匹配):
if(sbrowser.equalsIgnoreCase("chrome")) { WebDriverManager.chromedriver().setup(); ChromeOptions co = new ChromeOptions(); co.addArguments("--headless=new"); // 适配新版Chrome的无头模式 co.addArguments("--no-sandbox"); // 避免权限限制导致启动失败 co.addArguments("--disable-dev-shm-usage"); // 解决临时文件共享内存不足问题 co.addArguments("--remote-allow-origins=*"); // 跨域访问许可 co.addArguments("--window-size=1920,1080"); // 设置窗口大小,避免页面布局异常 driver = new ChromeDriver(co); }
2. 优化buildspec.yml脚本
- 移除不必要的npm依赖安装(Java+Cucumber项目无需全局安装selenium-webdriver和cucumber)
- 去掉手动安装chromedriver的步骤,WebDriverManager会自动匹配Chrome版本,避免版本不一致
修改后的install阶段:
install: runtime-versions: java: corretto17 commands: - echo "Install dependencies" - choco install -y googlechrome - choco install -y maven - chrome --version # 验证Chrome安装版本
3. 优化配置文件读取路径
用类加载器读取config.properties,避免硬编码路径:
FileInputStream fls = new FileInputStream(TestBaseClass.class.getClassLoader().getResource("Config/config.properties").getFile());
4. 验证构建环境
在pre_build阶段添加命令,确认Chrome安装路径:
pre_build: commands: - dir "C:\\Program Files\\Google\\Chrome\\Application" # 确认Chrome是否安装在指定路径 - echo "Compile source code" - mvn clean install
内容的提问来源于stack exchange,提问作者Ujjay Hirbhagat
相关产品推荐
相关产品推荐

