Gitlab CI - Django functional tests - splinter - python

I want to run some automation tests on github on a project of mine with a Django framework. Therefore I am using a Django functional test. While executing the test on my local pc works fine, my pipeline is always failing with those tests.
I assumed, that chromedriver wasn't working correctly and after some research on the internet, I found out, that I need to install chrome as browser, so I modified my requirements.txt for pip like this:
applescript==2018.11.19
astroid==2.1.0
autopep8==1.4.3
chromedriver==2.24.1
decorator==4.3.0
detect==2018.11.19
Django==2.1.3
flake8==3.6.0
google-chrome==2018.11.19
google-chrome-cli==2018.11.19
isort==4.3.4
lazy-object-proxy==1.3.1
mccabe==0.6.1
only==2018.11.20
psutil==5.4.8
public==2018.11.20
pycodestyle==2.4.0
pyflakes==2.0.0
pylint==2.2.1
pytz==2018.7
runcmd==2018.11.20
selenium==3.141.0
six==1.11.0
splinter==0.10.0
temp==2018.11.20
urllib3==1.24.1
wrapt==1.10.11
.gitlab-ci.yml
image: python:latest
before_script:
- pip install virtualenv
- virtualenv --python=python3 venv/
- source venv/bin/activate
- pip install -r requirements.txt
- cd src/
- python manage.py migrate
stages:
- quality
- tests
flake8:
stage: quality
script:
- flake8 ./
test:
stage: tests
script:
- python manage.py test
test_functional.py
def setUp(self):
# LINUX x64
executable_path = {'executable_path': settings.CHROMEDRIVER_PATH_LINUX64}
# chrome
self.browser_chrome = Browser('chrome', **executable_path)
[..]
With this, a chrome browser has been installed, but now I get this error:
selenium.common.exceptions.WebDriverException:
Message: Service /builds/mitfahrzentrale/mitfahrzentrale/venv/chromedriver unexpectedly exited.
Status code was: 127
What do I need to modify in order to use chromedriver for gitlab?

I don't think the google-chrome package does what you think it does. Looking at its source code, it's a Python wrapper for a set of AppleScript commands around the Chrome browser on MacOS and will certainly not install the browser on Linux.
For reference, here is the (stripped) Gitlab CI pipeline we're using with Django and Selenium to run tests with Firefox and Chrome:
stages:
- test
.test:
coverage: '/TOTAL.*\s+(\d+%)$/'
test-linux_x86_64:
extends: .test
image: python:3.7.1-stretch
stage: test
tags:
- linux_x86_64
script:
- apt -qq update
- DEBIAN_FRONTEND=noninteractive apt -qq -y install xvfb firefox-esr chromium chromedriver
# download geckodriver as no distro offers a package
- apt install -qq -y jq # I don't want to parse JSON with regexes
- curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest | jq -r '.assets[].browser_download_url | select(contains("linux64"))' | xargs -n1 curl -sL | tar -xz -C /usr/local/bin
- chmod +x /usr/local/bin/geckodriver
# prepare Django installation
- python -m venv /opt/testing
# bundled pip and setuptools are outdated
- /opt/testing/bin/pip install --quiet --upgrade pip setuptools
- /opt/testing/bin/pip install --quiet -r requirements.txt
- xvfb-run /opt/testing/bin/python manage.py test
Some notes:
taking a closer look at the job, all the steps besides the last two are preparation steps; moving them to a custom Docker image will reduce the test running time and the amount of boilerplate in your pipeline.
here, xvfb is used to run the browser in a virtual display; the modern browsers are able to run in headless mode (add --headless to chromedriver options), making the virtual display unnecessary. If you don't need to support old browser versions, you can omit the xvfb installation and xvfb-run usage.
The tests will run as root in container; at first, we got the error
E selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
E (unknown error: DevToolsActivePort file doesn't exist)
E (The process started from chrome location /usr/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
E (Driver info: chromedriver=2.41,platform=Linux 4.15.10-300.fc27.x86_64 x86_64)
If you face this, you need to pass the additional flag --no-sandbox to Chrome because it refuses to run as root without it:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
ds = DesiredCapabilities.CHROME
ds['loggingPrefs'] = {'browser': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=ds, options=chrome_options)

Related

Pyinstaller not working in Gitlab CI file

I have created a python application and I would to deploy it via Gitlab. To achieve this, I create the following gitlab-ci.yml file:
# This file is a template, and might need editing before it works on your project.
# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/
image: "python:3.10"
#commands to run in the Docker container before starting each job.
before_script:
- python --version
- pip install -r requirements.txt
# different stages in the pipeline
stages:
- Static Analysis
- Test
- Deploy
#defines the job in Static Analysis
pylint:
stage: Static Analysis
script:
- pylint -d C0301 src/*.py
#tests the code
pytest:
stage: Test
script:
- cd test/;pytest -v
#deploy
deploy:
stage: Deploy
script:
- echo "test ms deploy"
- cd src/
- pyinstaller -F gui.py --noconsole
tags:
- macos
It runs fine through the Static Analysis and Test phases, but in Deploy I get the following error:
OSError: Python library not found: .Python, libpython3.10.dylib, Python3, Python, libpython3.10m.dylib
This means your Python installation does not come with proper shared library files.
This usually happens due to missing development package, or unsuitable build parameters of the Python installation.
* On Debian/Ubuntu, you need to install Python development packages:
* apt-get install python3-dev
* apt-get install python-dev
* If you are building Python by yourself, rebuild with `--enable-shared` (or, `--enable-framework` on macOS).
As I am working on a Macbook I tried with the following addition - env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.10.5 but then I get an error that python 3.10.5 already exists.
I tried some other things, but I am a bit stuck. Any advice or suggestions?

Configure selenium webdriver in docker container

I have the following code:
fox = webdriver.Firefox(executable_path=GeckoDriverManager().install())
try:
fox.get(url+search+id)
image = BytesIO(fox.find_element_by_tag_name('table').screenshot_as_png)
image.name = id + '.png'
except:
fox.close()
fox.close()
return image
Its perfectly just works in windows 10 with env, but this code is just a part of one project, and i use docker to deploy it. The Dockerfile:
FROM python:slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD [ "python", "./main.py" ]
All i have after execution of the python code above is:
====== WebDriver manager ======
Current firefox version is 91.5
Get LATEST geckodriver version for 91.5 firefox
Driver [/root/.wdm/drivers/geckodriver/linux64/v0.30.0/geckodriver] found in cache
Make it work with this configuration:
opts = FirefoxOptions()
opts.add_argument("--headless")
fox = webdriver.Firefox(executable_path=GeckoDriverManager().install(), options=opts)
Also added this line to the Dockerfile:
RUN apt-get update && apt-get install firefox-esr -y

Save a file from gitlab-ci to a gitlab repository

I made this citlab-ci.yml file but i can find the html report in my repo at the end.
Can you tell me why ?
image: python
services:
- selenium/standalone-chrome:latest
variables:
selenium_remote_url: "http://selenium__standalone-chrome:4444/wd/hub"
cucumber:
script:
- python --version
- pwd
- ls
- pip install pytest
- pip install pytest_bdd
- pip install selenium
- pip install chromedriver
- pip install pytest-html
- cd test_pytest
- ls
- python -m pytest step_defs/test_web_steps.py --html=report.html
tx
Hadrien
You can actually generate test reports in gitlab. For this, generate an XML report from Pytest that would be stored in GitLab as an artifact. On your .gitlab-ci.yml file
image: python:3.6
stages:
- test
testing:
stage: test
when: manual
script:
...
- pytest --junitxml=report.xml
artifacts:
when: always
reports:
junit: report.xml
Then, you can download this report
or visualize it under the Tests tag of your pipeline.

Bitbucket pipleline python3.7 not found! Try the pythonBin option

I have below bitbucket pipeline
image: node:11.13.0-alpine
pipelines:
branches:
master:
- step:
caches:
- node
script:
- apk add python py-pip python3
- npm install -g serverless
- serverless config credentials --stage dev --provider aws --key $AWS_ACCESS_KEY_ID --secret $AWS_SECRET_ACCESS_KEY
- cd src/rsc_user
- pip install -r requirements.txt
- sls plugin install -n serverless-python-requirements
- sls plugin install -n serverless-wsgi
- npm i serverless-package-external --save-dev
- npm install serverless-domain-manager --save-dev
- serverless deploy --stage dev
Throwing error
Error --------------------------------------------------
Error: python3.7 not found! Try the pythonBin option.
at pipAcceptsSystem (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/serverless-python-requirements/lib/pip.js:100:13)
at installRequirements (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/serverless-python-requirements/lib/pip.js:173:9)
at installRequirementsIfNeeded (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/serverless-python-requirements/lib/pip.js:556:3)
at ServerlessPythonRequirements.installAllRequirements (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/serverless-python-requirements/lib/pip.js:635:29)
at ServerlessPythonRequirements.tryCatcher (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/promise.js:729:18)
at _drainQueueStep (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/async.js:93:12)
at _drainQueue (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (/opt/atlassian/pipelines/agent/build/src/rsc_user/node_modules/bluebird/js/release/async.js:15:14)
at processImmediate (internal/timers.js:443:21)
at process.topLevelDomainCallback (domain.js:136:23)
For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Issues: forum.serverless.com
Your Environment Information ---------------------------
Operating System: linux
Node Version: 11.13.0
Framework Version: 2.1.1
Plugin Version: 4.0.4
SDK Version: 2.3.2
Components Version: 3.1.4
I am not able to get this error as I am new in python.
any help highly appreciated
Thanks
This error basically means you do not have the right installation of python. Some application needs python3.7 and you do not specify a version with apk add python3. Hence, the latest is probably installed (3.8).
This article deals with how to select a given python version for an agent in a bitbucket pipeline. It basically boils down to:
image: python:3.7
pipelines:
default:
- step:
script:
- python --version
Is there a reason you have to use Alpine? Otherwise I'd go for the pragmatic image above.
he solve with
pythonRequirements:
pythonBin: python3
Similar problem

WebDriverException at / Service /usr/bin/chromium-browser unexpectedly exited. Status code was: 127 [duplicate]

I'd like to construct my crawler using selenium on my server.
Thus I had installed/download required dependencies- such as chromedriver, chromium-browser etc on my Ubuntu17.10 server
However, when I run following code:
driver = webdriver.Chrome()
It returns following error:
---------------------------------------------------------------------------
WebDriverException Traceback (most recent call last)
<ipython-input-14-2cdab8938403> in <module>()
----> 1 driver = webdriver.Chrome()
/home/zachary/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options)
66 service_args=service_args,
67 log_path=service_log_path)
---> 68 self.service.start()
69
70 try:
/home/zachary/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py in start(self)
96 count = 0
97 while True:
---> 98 self.assert_process_still_running()
99 if self.is_connectable():
100 break
/home/zachary/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py in assert_process_still_running(self)
109 raise WebDriverException(
110 'Service %s unexpectedly exited. Status code was: %s'
--> 111 % (self.path, return_code)
112 )
113
WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127
What does it mean that it's excited..?
I can't get what the original intention of that error code and where to start to fix it.
It looks very rare case.
Maybe relevant:
I had install ubuntu desktop 17.10 on my desktop but failed to get GUI boot. Thus I am just using terminal only, but it well works so far.
I had installed ssh and remote controlling jupyter notebook from my mac to server desktop, and those errors comes from it.
Hope this info is relevant to solve this error, otherwise will abort it.
It seems chromedriver needs some extra libraries. This solved the issue for me:
apt-get install -y libglib2.0-0=2.50.3-2 \
libnss3=2:3.26.2-1.1+deb9u1 \
libgconf-2-4=3.2.6-4+b1 \
libfontconfig1=2.11.0-6.7+b1
I was working on a similar setup using a docker container instead of a server/VM without X / GUI.
To figure out which dependencies are required I tried iteratively to run it from the command line like this: /opt/chromedriver/2.33/chromedriver --version over and over again.
Then at eache time I used commands like apt-cache search <STUFF> and apt-cache madison <STUFF> to figure out the exact version of the deb package needed by chromedriver 2.33 (in my case, but I guess something similar would work for any version of chromedriver).
Edit
As suggested in the comments, using the ldd command to print shared object dependencies may be another option. As of today my chromedriver version after a few years from the original answer is 83.0.4103.14 - the dependencies are different as well, but see below to get an idea of what could be missing:
$ /usr/local/bin/chromedriver --version
ChromeDriver 83.0.4103.14 (be04594a2b8411758b860104bc0a1033417178be-refs/branch-heads/4103#{#119})
$ ldd /usr/local/bin/chromedriver
linux-vdso.so.1 (0x00007fffff7f0000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f414739d000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f414737a000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f414736f000)
libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f4147246000)
libnss3.so => /usr/lib/x86_64-linux-gnu/libnss3.so (0x00007f41470f7000)
libnssutil3.so => /usr/lib/x86_64-linux-gnu/libnssutil3.so (0x00007f41470c4000)
libnspr4.so => /usr/lib/x86_64-linux-gnu/libnspr4.so (0x00007f4147082000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f4146f45000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4146df6000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4146ddb000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4146be9000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4147e56000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f4146b76000)
libplc4.so => /usr/lib/x86_64-linux-gnu/libplc4.so (0x00007f4146b6d000)
libplds4.so => /usr/lib/x86_64-linux-gnu/libplds4.so (0x00007f4146b68000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f4146b3e000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f4146b38000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f4146b30000)
libbsd.so.0 => /usr/lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f4146b14000)
From man ldd:
ldd prints the shared objects (shared libraries) required by each program or shared object specified on the command line.
...
In the usual case, ldd invokes the standard dynamic linker (see ld.so(8))
with the LD_TRACE_LOADED_OBJECTS environment variable set to 1. This
causes the dynamic linker to inspect the program's dynamic
dependencies, and find (according to the rules described in ld.so(8))
and load the objects that satisfy those dependencies. For each
dependency, ldd displays the location of the matching object and the
(hexadecimal) address at which it is loaded.
I encountered the same error when using selenium/chromedriver on my VPS. I installed chromium-browser and the problem was gone.
sudo apt-get install -y chromium-browser
Maybe it's not the chromium-browser is needed, but the packages were installed along with it. However, that was a quick fix.
Run this command to troubleshoot: ./chromedriver (where your chrome driver binary is).
You might see an error like this:
./chromedriver: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
To solve this error, simply run: sudo apt-get install libnss3.
Then check again and see if it works this time: ./chromedriver.
Some other packages might be missing as well. Here is an exhaustive list:
gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
You probably don't need all of them but the ones you need are likely to be listed above.
I had a similar issue but it turned out that my problem was incorrectly set service_log_path which was pointing to a deleted folder.
webdriver.Chrome(executable_path='/path/to/chromedriver', service_log_path='/path/to/existing/folder')
While working with Selenium v3.11.0, ChromeDriver v2.36 and Chrome v64.x you have to download the latest ChromeDriver from the ChromeDriver - WebDriver for Chrome and place it within your system. Next while initializing the WebDriver and the WebBrowser you have to pass the argument executable_path along with the absolute path of the ChromeDriver as follows :
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
driver.get("http://www.python.org")
I used such a script to install Chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list
apt update -y
apt install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
apt install -y google-chrome-stable
Solved by carefully removing existing chromedriver and updating it to a newer version:
Delete all existing chromedriver files
Download wget https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip (replace 2.46 bit to the newer one if needed, see compatible versions here: http://chromedriver.chromium.org/downloads)
Unzip, convert to executable by running chmod +x chromedriver
Move it to mv -f chromedriver /usr/local/bin/chromedriver so it appears in PATH
This should solve an issue. I thought updating doesn't work because when I first tried it, I didn't remove the older version and I was still using it accidentally.
I was receiving the same selenium trace error:
WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127
My issue was due to using a different version of chromedriver (version 78) than browser (version 79) when trying to manually run the chromedriver I would see
Segmentation fault (core dumped)
Once I updated my chromedriver to match the browser it was able to start successfully
Starting ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945#{#614}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
#TPPZ 's answer need be updated
$ apt-get update -y
$ apt-get install -y libglib2.0-0 libnss3 libgconf-2-4 libfontconfig1
I had this same issue, and the problem was due to chromedriver version.
Please ensure You are using latest Chrome Browser along with latest chromedriver.
Reverting to older versions might also be a solution...
I am using Ubuntu 18.10 and installed the latest Selenium (3.141.0) and ChromeDriver (75.0.3770.8), but also had the same permission problems, and status code 127 afterwards.
I tried installing Chromium and noticed that Ubuntu was using Version 73. So I reverted from the latest version of Chromedriver (75 at this time), back to Version 73 and that worked for me.
This error can occur if you're running a IDE in the cloud. GITPOD etc. Try creating a local repo and VSCode (or your chosen IDE) and selenium should work fine.

Categories