티스토리 뷰

DevOps/System&Tools

Selenium with Jenkins

Jacob_baek 2021. 1. 21. 23:16

Selenium을 CentOS 상에서 별도의 GUI없이 사용할수 있도록 구성하고 이를 Jenkins Slave node로 등록하여 pipeline을 구동하는 방법에 대하여 알아보자.

CentOS7 에서 GUI 없이 selenium 구동

chrome webdriver와 python을 이용해 selenium UI test를 구현한것이며
아래내용은 jenkins slave node에 수행해 주어야 job 수행이 이슈가 없다.

아래 링크에서 chromedriver를 다운로드 받아 특정 path에 복사해둔다.
(특정 Path는 향후 python code에서 사용되기때문에 인지가능하고 쉬운 Path로 하는것을 추천한다.)

실제 다운로드 받은 chromedriver이 잘동작되는지 확인해보자.
관련 library를 설치하지 않는 경우 아래와 같은 에러가 발생되고 CentOS7 기준 아래와 같은 Package를 설치하자.

[root@deploy selenium-test]# ./chromedriver
./chromedriver: error while loading shared libraries: libxcb.so.1: cannot open shared object file: No such file or directory

[root@deploy selenium-test]# yum install libxcb

이후 chromedriver 가 정상구동되는것을 확인한다.

[root@deploy selenium-test]# ./chromedriver
Starting ChromeDriver 88.0.4324.96 (68dba2d8a0b149a1d3afac56fa74648032bcf46b-refs/branch-heads/4324@{#1784}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

이제 selenium을 설치하자.

기본적으로 python3-pip가 설치되어 pip3 명령이 동작된다는 가정하에 진행되었다.

실제 sample code를 생성하여 실행해보자.

[root@deploy selenium-test]# python webtest.py
Traceback (most recent call last):
  File "webtest.py", line 10, in <module>
    driver = webdriver.Chrome(chromedriver, options=webdriver_options )
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

headless 방식으로 동작하려하니 chrome자체를 찾지 못했다고 메세지를 출력했다.

하여 chrome browser를 설치하였다.

[root@deploy selenium-test]# yum install -y https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

그럼에도 아래와 같은 에러가 발생된다.

  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

이는 chrome의 option을 추가하여 해결할수 있다.

#!/usr/env python

from selenium import webdriver
import time

webdriver_options = webdriver.ChromeOptions()
webdriver_options.add_argument('--headless')
webdriver_options.add_argument('--no-sandbox')
webdriver_options.add_argument('--disable-dev-shm-usage')

chromedriver = './chromedriver'
driver = webdriver.Chrome(chromedriver, options=webdriver_options )

driver.get('https://entertain.daum.net/')

print(driver.title)
print(driver.current_url)

이후 정상적으로 데이터를 가져오는것을 확인할수 있었다.

Jenkins 구성

slave node를 위에 필요한 패키지 및 도구를 설치하여 selenium이 동작 가능한 환경으로 만들고 Jenkins slave 등록을 한후에 해당 slave에서 selenium관련 python code가 동작되도록 해보자.

 

아래와 같이 Jenkinsfile을 만들고(물론 테스트를 위한 환경이기에 각자의 환경에 맞게 수정이 필요하다.) git 서버에 추가한다.

pipeline {
    agent { label "selenium-agent" }

    stages {
        stage('Prepare') {
            steps {
                sh 'rm -f chromedriver*'
                sh 'wget https://chromedriver.storage.googleapis.com/87.0.4280.88/chromedriver_linux64.zip && unzip chromedriver_linux64.zip'
            }
        }
        stage('run selenium script') {
            steps {
                sh 'python3 console-test/VMCreation.py'
            }
        }
    }
}

이후 아래와 같이 앞서 저장했던 git server의 repo 주소를 아래에 기입한다.

또한 Jenkinsfile을 repo 최상단 directory에 두지 않고 console-test/Jenkinsfile 경로에 두었다.

이후 Jenkins를 구동하면 아래와 같이 Job이 정상 실행됨을 확인할 수 있다.

참고
아래 docker-compose로 좀더 쉽게 jenkins와 jenkins slave selenium을 구동하도록 하였다.

참고사이트

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함