2

Using CSS/JS one can change the cursor that the user sees for their mouse pointer. For instance, you can set cursor: wait to change the cursor in to a spinner.

What I would like to do is have a Selenium test which waits until the cursor changes to/from wait. However, I can't find any "expected condition" (EC) method for watching the CSS value of the cursor.

Is there any way to wait for a cursor change (short of just sleeping and periodically checking its value)?

machineghost
  • 33,529
  • 30
  • 159
  • 234

3 Answers3

3

Theoretically, I believe you can wait until element.GetCssValue("cursor") == "wait". It would be great that you can provide a demo page to test it.

Expected conditions (EC) were created for common wait.Until usages, while waiting for cursor seems rare.

Sample code in C#:

// depends on which element you want to wait, here take <body> as an example
var wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(5000));
wait.Until(d => d.FindElement(By.TagName("body")).GetCssValue("cursor") == "wait");
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • That worked great, thanks. I'll post a separate answer with the Python version for anyone who wants it (as it's too hard to format the code in a comment). – machineghost Jul 17 '14 at 19:29
1

@Yi Zheng's answer was perfect, but for anyone (like myself) who is using the Python version of Selenium, what I wound up doing was creating a class like so:

class CursorChangesFromWaiting():
    """Helper class for waiting until the cursor changes back to normal)"""
    def __init__(self, browser):
        self.browser = browser

    def __call__(self, ignored):
        """This will be called every 500ms by Selenium until it returns true (or we time out)"""
        cursor = self.browser.find_element_by_tag_name("body").value_of_css_property("cursor")
        return cursor != "wait"

Then I was just able to do the following in my test:

WebDriverWait(browser, 20).until(CursorChangesFromWaiting(browser))
machineghost
  • 33,529
  • 30
  • 159
  • 234
0

@machineghost's answer works for Python but browser.find_element_by_tag_name is deprecated.

Use browser.find_element( By.TAG_NAME, "body")

class CursorChangesFromWaiting():
"""Helper class for waiting until the cursor changes back to normal)"""
def __init__(self, browser):
    self.browser = browser

def __call__(self, ignored):
    """This will be called every 500ms by Selenium until it returns true (or we time out)"""
    cursor = self.browser.find_element(By.TAG_NAME, "body").value_of_css_property("cursor")
    return cursor != "wait"