if you have a jQuery Ajax request in your test web page, you should wait to request to complete. Otherwise, unexpected behaviours can occur. in Selenium Webdriver, we can perform by Javascript running function: execute_script()
jQuery provide a parameter to check request status. “active” is one of jQuery library. if value is 1 it means request still in progress, if it is 0(zero) it means that it is done.
so we check by this code: jQuery.active == 0
you can see complete code at below in Python:
# -*- coding: UTF-8 -*- import unittest import datetime from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait class TestClass(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.set_page_load_timeout(30) self.driver.maximize_window() def test_MahsumAkbasNet_Pass(self): wait = WebDriverWait(self.driver, 30) self.driver.get("http://www.mahsumakbas.net/selenium") self.driver.find_element_by_id("submitJquery").click() print "Button is clicked at time: " + str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")) wait.until(lambda driver: self.driver.execute_script("return jQuery.active == 0")) print "Ajax request is completed at time: " + str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")) def tearDown(self): self.driver.close() if __name__ == '__main__': unittest.main()
you can see output, it waits about 11 seconds(requested page coded to sleep 11 seconds):
Button is clicked at time: 20150915212337
Ajax request is completed at time: 20150915212348
.
———————————————————————-
Ran 1 test in 22.413sOK