2

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)
pbaranski
  • 22,778
  • 19
  • 100
  • 117
  • 1
    What does the stacktrace / error trace log says? – undetected Selenium Dec 26 '21 at 13:14
  • 1
    Is the site you're testing literally `google.com`? It could just be a matter of a race condition in the page loading. Your tests don't seem to have any waits on expected conditions, so it's possible that a page load event will complete (meaning `driver.get` stops blocking) and you try to act on the element too soon. Instead using `WebDriverWait(driver, timeout=5).until(...)` will block until the element is available (or the timeout occurs). If the page itself truly fails to load, you may consider retry logic for the flakey tests. – sytech Dec 30 '21 at 18:21
  • @DebanjanB added logs and some clue for fix - but this logs indicates an empty page what is tested on CI machine (I managed to get screenshot during fail) – pbaranski Jan 02 '22 at 04:10
  • @sytech I'm not testing google.com directly - I'm testing many sites deployed to different servers. This problem was observed for different deployments so it is not related tightly to my environments. Some scripts are as simple as this inserted in question. Maybe this is coincidence but I think that this is problem with shared runners and memory. Adding timeout can be an option but I really need to know how to debug this problem on GitLab. I added logs and some clue for fix. – pbaranski Jan 02 '22 at 04:16

1 Answers1

0

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for answer but I tested wait before and white page is persistent state. I'm looking more for solution about debugging GitLab runner, browser state problem debugging to find out what is the root of those random problem. – pbaranski Jan 11 '22 at 04:30