finiteautomata
finiteautomata

Reputation: 3813

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?

Upvotes: 2

Views: 1264

Answers (2)

finiteautomata
finiteautomata

Reputation: 3813

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.

Upvotes: 0

Ryan
Ryan

Reputation: 5682

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]);

window.set_timeout(function(){
    self.login_to_facebook();
}, 1000)

The above will make it wait for one second before it calls the login. You can play with the time by changing the 1000. Set_timeout takes time values in milliseconds, (hence 1000 = 1 second).

Upvotes: 1

Related Questions