Hello Bitbucket Support,
I am facing an issue while setting up the Bitbucket Pipeline for my automated testing using Selenium, particularly when running in a headless environment with Chrome. Specifically, when the pipeline runs on Bitbucket's environment, I encounter the following error:
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
This issue occurs when I am using Selenium in an environment without the DISPLAY
information, causing an error when attempting to run the automated tests in the pipeline.
Environment Configuration Information:
I am using Chrome version 136.0.7103.93 (official build).
Locally, my tests run successfully in headless mode without any issues.
My pipeline configuration is using Xvfb to create a virtual framebuffer, and the environment lacks the DISPLAY
variable, causing Selenium to fail.
My current pipeline configuration:
pipelines:
default:
- step:
name: Build and Test with JDK 22
caches:
- maven
script:
# Tạo thư mục user-data-dir duy nhất cho mỗi commit
- export CHROME_USER_DATA_DIR="/tmp/chrome_user_data_$BITBUCKET_COMMIT"
- mkdir -p $CHROME_USER_DATA_DIR
# Cài đặt Maven nếu chưa có
- apt-get update && apt-get install -y wget
- wget https://archive.apache.org/dist/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
- tar -xvzf apache-maven-3.9.6-bin.tar.gz
- mv apache-maven-3.9.6 /tmp/maven
- export MAVEN_HOME=/tmp/maven
# Cài đặt ChromeDriver
- export CHROME_DRIVER_VERSION=110.0.5481.77
- wget https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip
- unzip chromedriver_linux64.zip
- mv chromedriver /usr/local/bin/
# Tải Selenium Server từ phiên bản mới hơn
- wget https://selenium-release.storage.googleapis.com/4.9/selenium-server-4.9.0.jar -O selenium-server.jar
# Chạy Selenium với ChromeDriver
- java -Duser-data-dir=$CHROME_USER_DATA_DIR -jar selenium-server.jar
# Build và chạy unit tests
- mvn clean install
- mvn test
Issues I am encountering:
HeadlessException
error: This error is related to the missing DISPLAY
information in the pipeline's headless environment, preventing Selenium from performing graphical operations.
Synchronization between local and pipeline environments: Tests run successfully locally but fail in the Bitbucket Pipeline. I want to ensure that the environment in the pipeline matches the local environment.
Pipeline configuration insufficient: It seems my current configuration is not optimized for running UI tests in a headless environment. I would need assistance to improve this process.
Support Requests:
Please confirm if additional environment variables or configurations are required to run Selenium tests in headless mode without encountering the HeadlessException
.
Verify if any other steps need to be configured for DISPLAY
or Xvfb
in the pipeline to avoid the HeadlessException
error.
Provide guidance on running UI tests like Selenium in Bitbucket Pipeline, especially in a headless environment.
Looking forward to your support.
Thank you!
Hello @Đặng Thị Thu Cẩm ,
and welcome to the Community!
I don't really have expertise on Selenium configuration in particular, but I would recommend trying to debug your pipelines locally with Docker to remove pipelines from the equation and identify whether the issue is with your current image/code or with the pipelines environment itself.
You can find instructions on how to run your pipelines locally with Docker in the article below:
Once the issue is fixed locally, you can replicate the same changes to your Bitbucket Pipelines, and it should also work.
During my research, I've also found this thread of a user who was able to fix a similar error by using a different docker image.
I hope this helps! Let us know in case you have any questions.
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.