How to start selenium in a background process and wait for it to start
source link: https://www.mailslurp.com/blog/start-selenium-in-background-process-wait-for/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Easily start Selenium Server in a background process - and how to wait for it to start
If you write software or QA tests you may have encountered Selenium. It’s a popular browser automation tool used for end-to-end acceptance testing. This means you can test applications in a real browser like Firefox or Chrome using code. There are many frameworks that support Selenium (such as Codeception, Specflow, ThirtyFour, and JUnit) but many of them lack a clear way to start Selenium before tests begin. Many developers start Selenium processes manually - but there is an easier way!
Starting background processes (and capturing the PID)
You can start a process and send it to the background in linux by appending an ampersand to the command:
java -jar selenium-server.jar &
Capturing the process ID
You can save the PID of the process you created with the special variable $!
.
java -jar selenium-server.jar &
PID=$!
Waiting for Selenium to start
One issue many encounter with this technique is that Selenium can take some time to start. This means that any tests executed after the Selenium server is started may run before the server is ready. To make sure the server is ready we can use this nifty one-liner:
timeout 300 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:4444)" != '200' ]]; do sleep 5; done' || false
This will retry the server endpoint http://localhost:444
every 5 seconds until a 200 response status is returned or 300 seconds elapses.
Running tests and killing Selenium afterwards
We can use this technique to start Selenium before tests, run tests, then kill the process with a simple bash script:
#!/bin/bash
set -e
echo "Starting selenium"
# start selenium
java -Dwebdriver.gecko.driver="$DRIVER_LOCATION" -jar "$SELENIUM_LOCATION" &
PID=$!
echo "Server running on process $PID"
echo "Waiting for server response"
timeout 300 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:4444)" != '200' ]]; do sleep 5; done' || false
echo "Running tests"
{ # try
mix test && echo "Tests passed. Killing process $PID" && kill "$PID"
} || { # catch
echo "Tests failed. Killing process $PID" && kill "$PID"
}
In the example above we ran mix test
to run some Elixir ExUnit tests but you could run whatever you like. Hope that helps! For more information on testing with Selenium or using test email accounts see our developers page.
Related
Receive emails in Java …
Receive emails in Java test suites using MailSlurp, Junit, and Selenium.
Test Emails in Selenium with …
Send and receive email in DotNET Nunit tests using Selenium and MailSlurp.
Test user sign-up and email …
Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.
Testing
Use MailSlurp to test email related functionality using real email addresses.
Codeception wait for Selenium …
Example tutorial for how to wait until webdriver and Selenium have started during Codeception PHP tests
How to test an email address
Test email accounts for testing email addresses in code or online. Create fake email accounts for testing.
Email API for email marketing …
APIs for email marketing and social campaign testing. Send, receive, validate and test emails in code and online.
CypressJS Email Test Plugin
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Email for testing
Test email accounts for email testing. Alternatives to Mailinator, MailTrap, Mailosaur and more.
Email testing before you send
There are many free tools to test emails before sending. This can help prevent spam warnings and increase deliverability.
Email APIs for developer and QA teams
MailSlurp is the leading email-as-a-service API system. Create unlimited disposable email accounts then send and receive emails from code and tests.
Create free accountWhat is MailSlurp?
MailSlurp is a free email API service for developers, QA testers and marketing campaigns. You can create disposable test mail accounts using the random email address generator via HTTP, GraphQL, dashboard or official code clients in Javascript, Csharp, Golang, Python, Java, Ruby, PHP and more!
What can MailSlurp do?
MailSlurp offers permanent email addresses using custom domains or random fake emails ending in @mailslurp.com
. You can generate as many inboxes as you need for end-to-end email testing, email marketing campaigns and application development.
Getting started
Create a free MailSlurp account to get started using alternatives to MailTrap, Mailinator, Mailosaur and more. MailSlurp is the most advanced email platform with many features including: HTTP webhook push, graphql email APIs, sdks with long-polling (wait for emails), inbox forwarding, complex routing rulesets, and spam filters. See the pricing page for a full overview.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK