javascriptdjangofacebookseleniumdjango-facebook

Facebook Login in Selenium Tests


I'm working on a django project, using django_facebook for Facebook interaction, and Selenium for automated system testing.

In the tests, when it comes to logging to facebook from my application, I do this:

        self.browser.get(self.live_server_url)
        self.browser.find_element_by_xpath('//*[@id="facebook_login_js"]/input').click()

        self.browser.switch_to_window(self.browser.window_handles[1])
        self.login_to_facebook()

but it fails because Facebook's JS SDK hasn't finished its asynchronous initialization process. My tests used to work in the past, don't know if something changed now (like slower initialization).

I've thought of a solution like using fbAsyncInit, but that code is provided by django_facebook in a Javascript file and I don't want to touch it.

Have any idea how to wait until Facebook JS SDK is completely load so I can use the login functionality?


Solution

  • I finally had to override Facebook initialization, so there was no problem afterwards. I simply did

    function facebookJSLoaded(){
        FB.init({appId: facebookAppId, status: false, cookie: true, xfbml: true, oauth: 
    
        $('#fb-root').attr("data-sdk-loaded", 1);
    };
    
    window.fbAsyncInit = facebookJSLoaded;
    F = new facebookClass(facebookAppId);
    F.load();
    

    (which is a solution very similar to How to detect when facebook's FB.init is complete). In my selenium test, I wait for this attribute to appear in fb-root to click the login button.