How to create a headless Selenium Server vagrant box

Vagrantfile:

Vagrant.configure(“2”) do |config|
config.vm.box = “bento/ubuntu-16.04”
config.ssh.forward_agent = true

config.vm.network “forwarded_port”, guest: 4444, host: 4444

config.vm.provision ‘setup’, type: ‘shell’, path: ‘setup.sh’
config.vm.provision ‘cleanup’, type: ‘shell’, path: ‘cleanup’

end

setup.sh

#!/bin/sh
set -e

if [ -e /.installed ]; then
echo ‘Already installed.’

else
echo ”
echo ‘INSTALLING’
echo ‘———-‘

# Add Google public key to apt
wget -q -O – “https://dl-ssl.google.com/linux/linux_signing_key.pub” | sudo apt-key add –

# Add Google to the apt-get source list
echo “deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main” > /etc/apt/sources.list.d/google-chrome.list

# Update app-get
apt-get update

# Install Java, Chrome, Xvfb, and unzip
add-apt-repository ppa:webupd8team/java -y
apt-get update -y
echo “oracle-java8-installer shared/accepted-oracle-license-v1-1 select true” | debconf-set-selections
apt-get install -y oracle-java8-installer
apt-get -y install google-chrome-stable xvfb unzip

# Download and copy the ChromeDriver to /usr/local/bin
cd /tmp
wget -N http://chromedriver.storage.googleapis.com/2.29/chromedriver_linux64.zip
wget http://selenium-release.storage.googleapis.com/3.4/selenium-server-standalone-3.4.0.jar
unzip chromedriver_linux64.zip
mv chromedriver /usr/local/bin
mv selenium-server-standalone-3.4.0.jar /usr/local/bin

# So that running `vagrant provision` doesn’t redownload everything
touch /.installed
fi

echo Create xvfb startup script
touch /etc/systemd/system/xvfb.service
chmod 644 /etc/systemd/system/xvfb.service
cat <<EOT >> /etc/systemd/system/xvfb.service
[Unit]
Description=Xvfb
After=syslog.target network.target

[Service]
Type=oneshot
RemainAfterExit=yes
User=vagrant
ExecStart=/bin/xvfb.sh
Environment=DISPLAY=:10

[Install]
WantedBy=multi-user.target
EOT
touch /bin/xvfb.sh
chown :vagrant /bin/xvfb.sh
chmod 750 /bin/xvfb.sh
cat <<EOT >> /bin/xvfb.sh
#! /bin/bash

Xvfb :10 -screen 0 1366x768x24 -ac &
EOT

echo Create google-chrome startup script
touch /etc/systemd/system/google-chrome.service
chmod 644 /etc/systemd/system/google-chrome.service
cat <<EOT >> /etc/systemd/system/google-chrome.service
[Unit]
Description=Google Chrome
After=xvfb.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/google-chrome.sh
Environment=DISPLAY=:10

[Install]
WantedBy=multi-user.target
EOT
touch /bin/google-chrome.sh
chmod 750 /bin/google-chrome.sh
cat <<EOT >> /bin/google-chrome.sh
#! /bin/bash

google-chrome –remote-debugging-port=92222 &
EOT

echo Create selenium-server startup script
touch /etc/systemd/system/selenium-server.service
chmod 644 /etc/systemd/system/selenium-server.service
cat <<EOT >> /etc/systemd/system/selenium-server.service
[Unit]
Description=Selenium Standalone Server
After=google-chrome

[Service]
Type=oneshot
RemainAfterExit=yes
User=vagrant
ExecStart=/bin/selenium-server.sh

[Install]
WantedBy=multi-user.target
EOT
touch /bin/selenium-server.sh
chown :vagrant /bin/selenium-server.sh
chmod 750 /bin/selenium-server.sh
cat <<EOT >> /bin/selenium-server.sh
#! /bin/bash

export DISPLAY=:10
cd /usr/local/bin
nohup java -jar ./selenium-server-standalone-3.4.0.jar &
EOT

systemctl daemon-reload
systemctl enable xvfb
systemctl enable google-chrome
systemctl enable selenium-server
systemctl start xvfb
systemctl start google-chrome
systemctl start selenium-server

cleanup.sh

#! /bin/bash

apt-get autoclean
apt-get clean
apt-get autoremove

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s