How to check jquery ajax tabs with selenium?

I am testing a django application with selenium, and one of my pages uses the jquery ui tabs element. One of the tabs contains a simple table listing some users and is loaded via ajax. When using the application, the tab works fine, but when automating the test with selenium, the tab does not load the contents! I write tests myself in python. Firstly, I used the click selenium RC method, but since I usually learned from the previous test, this is pretty bad when it comes to anchored tags, so I resorted to the solution I used before: the wait_for_condition method and explicitly called the event click tab (and even loading event!), and yet the tab still doesn't work!

I am desperate, most of my tests depend on this page, and almost half of them are on this table, but, alas, it seems that selenium twists javascript! (I have other tests in the class, and they work just fine, so nothing strange happens at the server level, it seems to be a problem caused by client-side selenium) My test code looks like this:

class TestMyApp(TransactionTestCase): urls = 'myapp.test_urls' def setUp(self): self.verificationErrors = [] self.selenium = selenium("localhost", 4444, "*chrome", "http://localhost:8000/") self.selenium.start() #self.selenium.set_speed(2000) self.selenium.window_maximize() def test_users_list(self): """Test that an app users are correctly listed""" sel = self.selenium users = [] for u in range(settings.FREE_USER_LIMIT/2): users.append(self.app.users.create(name="testUser_%s"%uuid4())) sel.open("/") sel.wait_for_page_to_load("30000") sel.wait_for_condition('selenium.browserbot.getCurrentWindow().jQuery("#tabs").tabs("select",1);\ selenium.browserbot.getCurrentWindow().jQuery("#tabs").tabs("load",1);', 3000) for user in users: try: self.failUnless(sel.is_text_present(user.name)) except AssertionError, e: self.verificationErrors.append(str(e)) try: self.failUnless(sel.is_text_present(str(user.added.date()))) except AssertionError, e: self.verificationErrors.append(str(e)) def tearDown(self): self.selenium.stop() self.assertEqual([], self.verificationErrors) 
+4
source share
1 answer

This may be a few things. It may be that Selena is having trouble pushing the anchor, but I really haven't heard about this problem, and it sounds less likely. It looks like the click () method returns OK, it does not give you an "item not found", right? When you click on the jquery tab, javascript just doesn't do what is expected. In my experience, this usually comes down to the same problem - since Selenium runs very fast when javascript periodically displays parts of the page and periodically executes the DOM when Selenium goes into interacting with dynamically generated parts of the page (for example, to click this tab), the part with which it interacts depends on some other part that is actually not yet fully loaded. This is probably in microseconds from a full load in fact, but selenium is too fast. You, of course, understand this, you have the right idea with the wait_for condition, which is looking for loadable tabs. My guess would probably not be long. You have to find some kind of rating to say that all user interface tabs are loading and displaying. Do the tab APIs have some callbacks that you can add to set the "done load" variable, or does it expose such a variable? It’s forbidden to find out what the correct expression is to find the point in time when the tabs on the user interface are actually ready to click, which can be difficult, you can resort to direct pauses to make sure that part of the page is ready to work before you interact with him. I do not see problems in a dream (2) or even sleep (5), etc. In the code, if necessary for its operation. One way to verify that this is really what is happening is to run the script in an interactive interpreter (you need to love Python, hit your pants with this in Java). Paste the code line by line to go to the point of failure, or comment out the call to selenium.stop () in the tracking method and any test code after the failure, so it leaves the selenium window open and exits. Then create an instance of the selenium object in the interactive interpreter and capture an open session:

 selenium = selenium("localhost", 4444, "*chrome", "http://localhost:8000/") selenium.sessionId = "0asdd234234023424foo" #Get this from the Se window 

... for interactive window control. You can then see how to make selenium.click() calls or make selenium.get_eval('...js...') to explore the javascript landscape at that point in time. I assume that when you do this, click() will work fine when you enter it, because by the time you load the session and bypass its input in selenium.click('blah_tab_locator') , all brushes will be loaded and ready to go. It's easy when Python makes calls, it does it too fast for the browser when these dynamic renderings occur. If the click works fine, when you do it manually through selenium like this, then you know that it is a matter of time. Find this proper wait_for_condition or strip with python sleep() . Otherwise, if the click continues to work when you do this, this is probably the problem with the locator used. In the user interface of the tab, there is a click or mouse or focus or some kind of event handler in some part of the structure of the tab, perhaps it is just a search for the right part of the tab for a click or the correct event to trigger. If this is not the case, perhaps it may be when you mention some strange interaction between Selenium and the JQuery UI, but it will surprise me and I would be interested to know. Knock with get_eval () to see what happens in javascript, if so. It sounds like a matter of time to me, though.

+5
source

Source: https://habr.com/ru/post/1313814/


All Articles