LoginSignup
10
10

More than 5 years have passed since last update.

いいからやる。Jenkins + Xvfb + firefox + Selenium + ruby

Last updated at Posted at 2016-03-08

WebサイトのUIの自動テストがどうしてもしたくなったので、環境つくった。
何も考えなくても以下の手順を流せば環境完成する。
詳細な説明は先達の情報を参考に。

サーバを用意する

今回は、AWS Marketplace の CentOS6.5 を利用(手順は割愛)

サーバ初期設定

  • とりあえず
yum update -y
yum groupinstall -y 'Development tools'
yum install -y wget ImageMagick
  • iptables停止&自動起動off

(今回は閉じられたVPC環境内なので、セキュリティまわりはあとまわし。とにかく検証環境を手に入れたい。)

service iptables stop
chkconfig iptables off
  • SELinux無効化
vi /etc/selinux/config
SELINUX=disabled
  • 有効にするためにサーバ再起動する

  • 他、ユーザや権限等、適宜必要な設定を行う(まずは環境がほしいのであとでやる)

Jenkinsをインストールする

  • javaをインストールする
yum install -y wget java-1.7.0-openjdk
  • リポジトリを追加する
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
  • jenkinsをインストールする
yum -y install jenkins
  • jenkinsの自動起動設定
chkconfig jenkins on
  • Jenkinsを起動する
service jenkins start
  • 動作確認 http://[publicIP]:8080

Jenkinsユーザの設定(OSユーザ)

  • Jenkinsユーザに変更できるようにする
vi /etc/passwd
jenkins:x:498:499:Jenkins Continuous Build server:/var/lib/jenkins:/bin/bash
  • Jenkinsユーザでsudoが使えるようにする
visudo
# Defaults    requiretty
Defaults:jenkins !requiretty
jenkins ALL=(ALL) NOPASSWD:ALL

Jenknsを 8080 → 80 で動かす(任意)

(こちらの記事を参考にさせていただきました)
http://qiita.com/gitya107/items/25887864f07fa4cc7ce1

  • Jenkinsの設定ファイルを修正する
 vi /etc/sysconfig/jenkins
 # 以下の行を修正
 JENKINS_ARGS="--prefix=/jenkins"

httpdをインストール&設定する(任意)(80で動かしたい場合)

yum install -y httpd
chkconfig httpd on
 vi /etc/httpd/conf/httpd.conf
 # 以下の行を追加
 <Proxy *>
     Order deny,allow
     Allow from all
 </Proxy>
 ProxyPass /jenkins http://localhost:8080/jenkins
service httpd start
service jenkins restart

Jenkins最低限の設定

ユーザや権限等、適宜設定する(とにかくまずは環境がほしいのであとでやる)

Xvfbをインストールする

Xvfbをインストールする

yum install -y xorg-x11-server-Xvfb

起動スクリプトを設定する

今回はこちらを拝借

wget  https://raw.githubusercontent.com/kohkimakimoto/chef-cookbooks-selenium/master/templates/default/init.d/xvfb.erb
mv xvfb.erb /etc/init.d/Xvfb
chmod 755 /etc/init.d/Xvfb
  • 自動起動設定
chkconfig Xvfb on
  • Xvfb起動
service Xvfb start

Firefoxをインストールする

Firefoxをインストールする

  • 相性の関係で以下のバージョンをインストールする
wget ftp.mozilla.org/pub/mozilla.org/firefox/releases/34.0.5/linux-x86_64/en-US/firefox-34.0.5.tar.bz2
tar -xjvf firefox-34.0.5.tar.bz2
rm -rf /opt/firefox*
mv firefox /opt/firefox34.0.5
ln -sf /opt/firefox34.0.5/firefox /usr/bin/firefox
  • 日本語パッケージをインストールする
yum groupinstall -y "Japanese Support"

rbenvを使ってrubyをインストールする

※ 以下Jenkinsユーザで実行する (とりあえずJenkinsから実行できるようにしたいため)

su - jenkins

rbenvをインストールする

  • rbenvをcloneする
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
  • 環境設定
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
exec $SHELL -l
  • rbenvの確認
rbenv --version
  • ruby-buildをインストールする
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

rubyをインストールする

  • インストール可能なrubyのバージョンを確認する
rbenv install --list
  • rubyをインストールする
sudo yum install -y openssl-devel readline-devel zlib-devel
rbenv install -v 2.2.3
rbenv rehash
  • 確認(インストールされているruby一覧を表示)
rbenv versions
  • 全体で使うrubyのバージョンを指定
rbenv global 2.2.3

サンプルPGを実行する

  • gem install
gem install selenium-webdriver
gem install headless
  • サンプルPG
/var/lib/jenkins/test.rb
require 'selenium-webdriver'
require 'headless'

@headless = Headless.new
@headless.start
@driver = Selenium::WebDriver.for :firefox

@driver.get 'https://www.google.co.jp/'
@driver.save_screenshot '/var/lib/jenkins/test.png'

@driver.quit
@headless.destroy
  • Jenkinsのプロジェクトを作成し、シェルを設定する
#!/bin/bash
export PATH="/var/lib/jenkins/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

ruby /var/lib/jenkins/test.rb

以下のファイルが作成されていればOK(googleの画面キャプチャ)
/var/lib/jenkins/test.png

苦労したところ

FireFoxの最新版ではうまく動かなかった。(なんかいろいろ依存ライブラリが足りないぞと怒られる)
運良く(?)回避策(バージョン落とす)の情報に先に辿り着いてしまったので、解決策の追求はしていない。
そのうち調査したい。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10