How to validate if an element is enabled only after 60 seconds

482 views Asked by At

I have a link in my application which gets enabled after 60 seconds. I have to verify , that the link only gets enabled after 60 seconds not before that. Have tried below ways:

  1. I have tried element to be clickable() with fluent wait/webdriver wait/thread.sleep and all are returning that element is enabled where as it's actually disabled till 60 seconds.

  2. i have tried getAttribute("disabled")also , it also returns false.

The only difference i can see in the html is the class attribute. When it is disabled , class attribute value has additional text (disabled) added to it.

2

There are 2 answers

0
Mate Mrše On

Try with this (and tweek as needed):

long ts1 = System.currentTimeMillis()/1000;
new WebDriverWait(driver, 60).until(ExpectedConditions.attributeToBe(element, "disabled", null);
long ts2 = System.currentTimeMillis()/1000;
Assert.assertTrue((ts2-ts1)>=60);

If the element becomes not disabled before 60 seconds is up, assert will fail.

0
undetected Selenium On

As the link within the application gets enabled after 60 seconds and till then (being disabled) the class attribute contains the additional value disabled, to validate this usecase you can use a try-catch{} block and you can use either of the following Python based Locator Strategies:

  • CSS_SELECTOR:

    try:
        WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "tag_name[some_attribute='value_of_some_attribute']:not(.disabled)")))
        print("Class attribute failed to contain the value disabled for 60 seconds")
    except TimeoutException:
        print("Class attribute successfully contained the value disabled for 60 seconds")
    
  • XPATH:

    try:
        WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//tag_name[@some_attribute='value_of_some_attribute' and not(@class='disabled')]")))
        print("Class attribute failed to contain the value disabled for 60 seconds")
    except TimeoutException:
        print("Class attribute successfully contained the value disabled for 60 seconds")