GitLab job randomly failing with Selenium showing white screen

Viewed 438

I'm running simple selenium test on GitLab CI (free tier).

Sometimes I get failing job because elements where not located (after taking screenshot it appears that page is displayed as white screen).

After running failed job couple of times it starts working. It is very hard to reproduce because it is quite rare.

How can I get more info what kind of issue it is? Some memory problems with runners or other?

Example selenium script:

import unittest
from selenium import webdriver


class SmokeTests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        self.driver = webdriver.Chrome(options=chrome_options)

        base_url = 'https://google.com'
        self.driver.get(base_url)

    @classmethod
    def tearDownClass(self):
        self.driver.quit()

    def test_name(self):
        expected_title = 'Google'
        observed_title = self.driver.title
        self.assertEqual(expected_title, observed_title)

EDIT

How it looks like when running job couple times enter image description here

Logs contain standard info when title will be not found:

AssertionError: 'Google' != ''
- Google
+ 
----------------------------------------------------------------------
Ran 1 test in 1.193s
FAILED (failures=1)
tearDown
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1

I experienced this problem on different pages/servers/repositories. Possible issue is lack of memory on shared runners so maybe usage of below option can help:

options.add_argument('--disable-dev-shm-usage')

but is not possible to test it due to randomness of this fail.

CI script:

test staging ui:
  image: jaktestowac/python-chromedriver:3.9-selenium
  before_script:
    - pip install -r tests/requirements.txt
  script:
    - python -m unittest tests/smoke_tests.py

Image used for testing: https://hub.docker.com/layers/jaktestowac/python-chromedriver/3.9-selenium/images/sha256-5feb585b9ebdac3f5191e7c24f29e8e78beb4f69c9bc734d7050157544f4653f?context=explore

EDIT2

Using wait is not a case here (tested for 20sec). With this random bug the Page is not loaded at all (title element is not available).

Currently I'm trying find some clues with logging :

    d = DesiredCapabilities.CHROME
    d['goog:loggingPrefs'] = { 'browser':'ALL' }
    self.driver = webdriver.Chrome(desired_capabilities=d, options=self.chrome_options)
1 Answers

After taking screenshot it appears that page is displayed as white screen can be interpreted as the webpage didn't render completely when taking of the screenshot was attempted.

Ideally before validating the Page Title you need to induce WebDriverWait for either:

  • visibility_of_element_located() for any of the visible element within the DOM Tree. As an example, waiting for the Search Box to be visible as follows:

    def test_name(self):
        WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.NAME, "q")))
        expected_title = 'Google'
        observed_title = self.driver.title
        self.assertEqual(expected_title, observed_title)
    
  • element_to_be_clickable() for any of the clickable element within the HTML DOM. As an example, waiting for the Search Box to be clickable as follows:

    def test_name(self):
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
        expected_title = 'Google'
        observed_title = self.driver.title
        self.assertEqual(expected_title, observed_title)
    

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Related