- Parallel Execution: Leverage TestNG and Selenium Grid.
- Centralized Configuration: Manage from a properties file.
- Detailed Reporting: Generate reports with Allure.
- Automated Email Notifications: Set up via Jenkins.
- Video & Screenshot Recording: Works on Selenium Grid and locally.
- Data-Driven Testing: Utilize Excel, JSON, etc.
- Telegram Bot Integration: Get real-time updates.
- Dynamic Selenium Grid Scaling: Automatically scales nodes.
- Ngrok Integration: Expose local ports to the internet.
- Cross-Platform Execution: Compatible with macOS, Windows, Linux, and containers(future will suport record in container).
- Page Object Model (POM): Promotes scalable and maintainable test code.
- Factory Method Design Pattern: Enables flexible creation of platform and page instances.
- Jenkins as code: Config Jenkins use Jenkins container have pre-install tools, user
- Local Machine:
Run tests directly in your IDE.- The
createLocalDriver()
method inBrowserFactory
handles browser instances.
- The
-
Browser info is specified in the TestNG suite file for multi-browser tests.
-
Parameterizing Suites:
- Create a
suite
property in thepom.xml
:<properties> <suite>local</suite> </properties>
- Update the profile to use
${suite}
:<profile> <id>web-execution</id> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/resources/suites/${suite}.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </profile>
- Use the
-Dsuite=suite_name
parameter to run a specific suite:mvn test -Dsuite=parallel
- Create a
- Executes tests on remote or cloud machines.
- The
getOptions
method inBrowserFactory
provides browser capabilities. - Requires setting
grid.url
andgrid.port
inconfig.properties
.
- Use
docker-compose.yml
for parallel test execution.
- Install Docker.
- Run
docker-compose-v3-dynamic-grid.yml
ordocker-compose-v3-dynamic-grid-standalone.yml
to set up the dynamic grid (it will auto-pull images fromconfig.toml
). - On Mac M1/M2, enable Rosetta in Docker Desktop:
Settings -> Features in development -> Use Rosetta
.
mvn test-Dsuite=selenium-grid -Dtarget=selenium-grid -Dheadless=true -Dgrid.video=true
- The
createRemoteInstance
method inDriverFactory
handlesRemoteWebDriver
. - Check
grid.properties
for correct grid configuration. - Verify
selenium-grid.xml
forparallel="tests"
configuration.
-
Use the provided Docker Compose file to start Selenium Grid with dynamic nodes and video recording.
docker-compose -f docker-compose-v3-dynamic-grid.yml up -d
-
Ensure your
docker-compose.yml
includes configurations for video recording.
- Add the
se:recordVideo
capability in your WebDriver options. - Specify the screen resolution if required.
ChromeOptions options = new ChromeOptions();
options.setCapability("se:recordVideo", true);
options.setCapability("se:screenResolution", "1920x1080");
options.setCapability("se:name", "test_case_name");
WebDriver driver = new RemoteWebDriver(new URL("http://selenium-grid-url"), options);
- Videos are stored in the
${PWD}/assets/<sessionId>/
directory. - Files follow this format:
test_case_name_<browserName>.mp4
.
FROM cuxuanthoai/chrome-firefox-edge:v1.2
WORKDIR /app
COPY . .
// can change base on your test case
CMD ["mvn", "test"]
Chrome uses /dev/shm
(shared memory) for runtime data, which is 64MB by default under Docker. If this memory is insufficient, it can cause Chrome to crash.
-
Increase the size of
/dev/shm
- Run your Docker container with the
--shm-size
flag to increase the shared memory size.docker run --shm-size=1g <image>
- Run your Docker container with the
-
Mount
/dev/shm
to the host's shared memory- Use a bind mount to link the container’s
/dev/shm
to the host’s shared memory.docker run -v /dev/shm:/dev/shm <image>
- Use a bind mount to link the container’s
-
Start Chrome with the
--disable-dev-shm-usage
flag- Disable Chrome's use of
/dev/shm
by adding this flag to your Chrome launch options. - Example usage in Selenium (Java):
ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-dev-shm-usage"); WebDriver driver = new ChromeDriver(options);
- Disable Chrome's use of
-
Build Image:
docker build -t selenium-tests .
-
Run Container(with chrome brower need shm_size: '2gb' or can run docker-compose-chrome-firefox-edge.yml it will auto build and run base on Dockerfile):
docker run selenium-tests
Docker image cuxuanthoai/jenkins-docker:v1.2
, which comes with pre-installed necessary tools to run UI automation.
Run Jenkins with pre-configured tools and plugins:
docker-compose -f docker-compose-jenkins-as-code.yml up -d
- Default users (
admin
,viewer
,developer
) are created with passwords from the configuration file. - Tools like Maven, Git, Node.js, and Allure are pre-installed.
-
Enable 2-Step Verification:
- Go to Google Account Security.
- Turn 2-Step Verification ON.
-
Generate an App Password:
- Go to App Passwords.
- Choose Mail as the app and Other for the device (e.g., "Jenkins").
- Click Generate and copy the generated password.
- Go to Manage Jenkins > Configure System.
- Fill in the Extended E-mail Notification section:
- SMTP Server:
smtp.gmail.com
- Port:
465
- Use SSL: Enabled
- Authentication: Enabled
- Username: Your Gmail.
- Password: App Password.
- SMTP Server:
- Save the configuration.
Step 3: Jenkins Pipeline with Email Notifications(gmaill not allow send some file it's related to security problem)
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
post {
always {
emailext(
to: EMAIL_RECIPIENT,
subject: emailSubject,
body: emailBody,
mimeType: 'text/html',
attachLog: true, // Attach the build log file
//compressLog: true // Compress the build log before attaching zip
attachmentsPattern: '**/build.log'
)
}
}
}
Run Ngrok to expose your local port:
docker run --net=host -it -e NGROK_AUTHTOKEN=your_ngrok_authtoken -p 8080:8080 ngrok/ngrok http 8080
Replace your_ngrok_authtoken
with your Ngrok token. After starting, Ngrok provides a public URL to access your local server.
- Use BotFather to create a bot and get the token.
- Use
https://api.telegram.org/bot<TOKEN>/getUpdates
to find your chat ID.
**Step 2: Send Messages via Jenkins (can combine send zip file, becaue gmaill not allow send some file it's related to security problem) **
pipeline {
agent any
environment {
JENKINS_SERVER_URL = 'https://96-46-98.ngrok-free.app'
EMAIL_RECIPIENT = '@gmail.com'
TELEGRAM_TOKEN = ':AAEAJ5GDawMGUC4Ofv9SvD3YBn5UGmVii7Q'
TELEGRAM_CHAT_ID = '5321745388'
}
stages {
stage('Test Execution') {
steps {
script {
echo "Executing Test Stage..."
}
}
}
}
post {
always {
script {
// Zip and send Allure report via Telegram
sendTelegramReport()
}
}
success {
echo 'Tests completed successfully!'
}
failure {
echo 'Tests failed!'
}
}
}
// Function to zip and send the Allure report via Telegram
def sendTelegramReport() {
echo "Zipping Allure report..."
// Compress the Allure report directory into a zip file
sh """
zip -r allure-report.zip ./allure-report
"""
echo "Allure report zipped successfully."
echo "Sending Telegram report..."
def caption = "Allure Report for Jenkins Build #${env.BUILD_NUMBER}"
// Use Telegram API to send the zip file
sh """
curl -X POST \
-F chat_id=${TELEGRAM_CHAT_ID} \
-F document=@allure-report.zip \
-F caption=\"${caption}\" \
https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendDocument
"""
echo "Telegram report sent successfully."
}
Example when integrate with Ngrok
Sent mail
Run inside container
Run cmd
bash mvn test -Dtest=TestLabNG
This Java class (App
) provides a simple way to download the jenkins.war
and selenium-server.jar
files and store them in the lib
folder.
- eliasnogueira(Java Developer)
- Selenium Docker GitHub Repository
- Ngrok Documentation
- Telegram Bot API
- SeleniumHQ/seleniumhq.github.io#2139
- https://support.google.com/mail/thread/183285153/i-m-trying-to-send-an-attachment-and-i-m-unable-to-send-saying-it-was-blocked-for-a-security-issue?hl=en
- https://stackoverflow.com/questions/35783964/jenkins-html-publisher-plugin-no-css-is-displayed-when-report-is-viewed-in-j/35785788#35785788
- SeleniumHQ/selenium#13077
- https://issues.chromium.org/issues/40432240
- mozilla/geckodriver#2199