how to use assert in selenium python to check my page load time is less than 5 sec?



  • start2 = datetime.datetime.now()
    
    button8 = driver.find_element_by_xpath("//span[contains(text(),'Last Month')]").click()
    
    timeout = time.time() + 60 * 2
    while len(driver.find_elements_by_css_selector("path[class='area areaChart']")) != 16:
        time.sleep(0.1)
        if time.time() > timeout:
             break
    print('Last month option selected')
    elapsed2 = datetime.datetime.now()
    x = elapsed2 - start2
    print(x)
    #print("Elapsed Time2 = {0}".format(elapsed2 - start2))
    #if x <= 5:
    assert (x <= 5),"VM4 is working good!"
    


  • You can go for WebDriver.set_page_load_timeout() function, if page loading time will be exceeded - Selenium will throw an error automatically.

    driver.set_page_load_timeout(5)
    

    You can also use Explicit Wait instead of your construction:

    wait(driver, 5).until(lambda driver: len(driver.find_elements_by_css_selector("path[class='area areaChart']")) != 16)
    

    More information: How to use Selenium to test web applications using AJAX technology



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2