Debugging Selenium Tests

Lev Eliezer Israel edited this page May 17, 2016 · 1 revision

Debugging a front end test

Two of the front end tests are failing:

$ python run_local.py 
RecentInToc / chrome - Starting
RecentInToc / chrome - Passed
LoadRefAndClickSegment / chrome - Starting
LoadRefAndClickSegment / chrome - Passed
LoadRefWithCommentaryAndClickOnCommentator / chrome - Starting
LoadRefWithCommentaryAndClickOnCommentator / chrome - Failed
no such element: Unable to locate element: {"method":"css selector","selector":".textFilter[data-name="Rashi"]"}
  (Session info: chrome=50.0.2661.102)
  (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64)
ClickVersionedSearchResultDesktop / chrome - Starting
ClickVersionedSearchResultDesktop / chrome - Failed
timeout: Timed out receiving message from renderer: 300.000
  (Session info: chrome=50.0.2661.102)
  (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64)
                                           chrome
LoadRefWithCommentaryAndClickOnCommentator Fail  
RecentInToc                                Pass  
ClickVersionedSearchResultDesktop          Fail  
LoadRefAndClickSegment                     Pass  

First, let's isolate a failing test, so that we don't have to wait so long, and watch what happens in the browser.

LoadRefWithCommentaryAndClickOnCommentator

$ python run_one_local.py LoadRefWithCommentaryAndClickOnCommentator
LoadRefWithCommentaryAndClickOnCommentator / chrome - Starting
LoadRefWithCommentaryAndClickOnCommentator / chrome - Failed
no such element: Unable to locate element: {"method":"css selector","selector":".textFilter[data-name="Rashi"]"}
  (Session info: chrome=50.0.2661.102)
  (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64)

Watching the browser, it was clear that the test was failing to locate Rashi, before the commentator panel had finished rendering.

Here's the test code:

    def load_ref(self, ref, filter=None):
        """
        takes string ref or object Ref
        :param ref:
        :param filter: "all", "Rashi", etc
        :return:
        """
        if isinstance(ref, basestring):
            ref = Ref(ref)
        assert isinstance(ref, Ref)
        url = self.base_url + "/" + ref.url()
        if filter is not None:
            url += "?with={}".format(filter)
        self.driver.get(url)
        WebDriverWait(self.driver, TEMPER).until(title_contains(ref.normal()))
        return self

The problem is the WebDriverWait line. Turns out, the title is updated before the content is loaded. If filters are specified, let's make it look for the presence of the filters in the DOM.

        if filter is not None:
            # Filters load slower than the main page
            WebDriverWait(self.driver, TEMPER).until(presence_of_element_located((By.CSS_SELECTOR, ".filterSet")))
        else:
            WebDriverWait(self.driver, TEMPER).until(title_contains(ref.normal()))

And now...

eliezer-israels-macbook-pro:browsertest levisrael$ python run_one_local.py LoadRefWithCommentaryAndClickOnCommentator
LoadRefWithCommentaryAndClickOnCommentator / chrome - Starting
LoadRefWithCommentaryAndClickOnCommentator / chrome - Passed
Pass - LoadRefWithCommentaryAndClickOnCommentator on chrome

Success!

ClickVersionedSearchResultDesktop

What about ClickVersionedSearchResultDesktop?

It's erring with:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"a[href="/Psalms.59.7/en/The_Rashi_Ketuvim_by_Rabbi_Shraga_Silverstein?qh=Dogs"]"}

The code of the test:

    # Search
    def search_for(self, query):
        elem = self.driver.find_element_by_css_selector("input.search")
        elem.send_keys(query)
        elem.send_keys(Keys.RETURN)
        WebDriverWait(self.driver, TEMPER).until(title_contains(query))
        return self

Again, the wait condition needs to be changed, in order to make sure that the search results are there.

WebDriverWait(self.driver, TEMPER).until(presence_of_element_located((By.CSS_SELECTOR, ".result")))

and...

$ python run_one_local.py ClickVersionedSearchResultDesktop
ClickVersionedSearchResultDesktop / chrome - Starting
ClickVersionedSearchResultDesktop / chrome - Passed
Pass - ClickVersionedSearchResultDesktop on chrome