vnc | selenium vnc server | install vnc entry | Search

This Bash script sets up a headless environment with a virtual display, starts Selenium, a window manager, and a VNC server for remote Selenium testing.

Run example

npm run import -- "vnc entry point"

vnc entry point

#!/bin/bash
#
# IMPORTANT: Change this file only in directory StandaloneDebug!

function shutdown {
  kill -s SIGTERM $NODE_PID
  wait $NODE_PID
}

if [ ! -z "$SE_OPTS" ]; then
  echo "appending selenium options: ${SE_OPTS}"
fi

rm -f /tmp/.X*lock

export DISPLAY=:99
xvfb-run --server-args="-screen 0 8160x6120x24 -ac +extension RANDR" \
  java -Dwebdriver.chrome.chromedriver=/opt/selenium/chromedriver-* -jar /opt/selenium/selenium-server-standalone.jar  &
NODE_PID=$!

trap shutdown SIGTERM SIGINT
for i in $(seq 1 10)
do
  xdpyinfo -display $DISPLAY >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    break
  fi
  echo Waiting xvfb...
  sleep 0.5
done

fluxbox -display $DISPLAY &

x11vnc -scale 2700x2048 -noxdamage -forever -nopw -shared -rfbport 5900 -display $DISPLAY &

wait $NODE_PID

What the code could have been:

bash
#!/bin/bash
#
# IMPORTANT: Change this file only in directory StandaloneDebug!

# Function to shutdown the process
shutdown() {
  # Send SIGTERM signal to the process
  kill -s SIGTERM $NODE_PID
  # Wait for the process to exit
  wait $NODE_PID
}

# Check if selenium options are provided
if [ -n "$SE_OPTS" ]; then
  # Log the selenium options
  echo "Appending selenium options: ${SE_OPTS}"
fi

# Clean up X11 locks
rm -f /tmp/.X*lock

# Set the display environment variable
export DISPLAY=:99

# Run xvfb with custom screen arguments
xvfb-run --server-args="-screen 0 8160x6120x24 -ac +extension RANDR" \
  java -Dwebdriver.chrome.chromedriver=/opt/selenium/chromedriver-* -jar /opt/selenium/selenium-server-standalone.jar  &
NODE_PID=$!

# Trap SIGTERM and SIGINT signals to call the shutdown function
trap shutdown SIGTERM SIGINT

# Wait for xvfb to start
for i in $(seq 1 10); do
  # Check if xvfb is running using xdpyinfo
  xdpyinfo -display $DISPLAY >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    # If xvfb is running, break out of the loop
    break
  fi
  # Log a message to indicate that xvfb is still starting
  echo "Waiting for xvfb..."
  # Wait for 0.5 seconds before checking again
  sleep 0.5
done

# Run fluxbox on the display
fluxbox -display $DISPLAY &

# Run x11vnc on the display with custom settings
x11vnc -scale 2700x2048 -noxdamage -forever -nopw -shared -rfbport 5900 -display $DISPLAY &

# Wait for the xvfb process to exit
wait $NODE_PID

This Bash script sets up a remote desktop environment with a VNC server for Selenium testing. Here's a breakdown:

Initialization:

Environment Setup:

Waiting for Xvfb:

Starting Applications:

Cleanup:

In essence, this script creates a headless environment with a virtual display, starts Selenium, a window manager, and a VNC server, allowing remote control and Selenium testing.