Automating Facebook Login with Selenium Webdriver

2020-06-29 14:23发布

问题:

I am looking to introduce Facebook login/sign up tests into a Webdriver suite for a C# application.

Facebook Developers has an article on manual verification, but does not seem to reference any procedure for automating these tests. I could simply write the steps indicated but I would like to avoid running scripts on the Facebook UI, especially when it involves creating test users.

Looking through a few previous answers, it appears that Facebook had an automation solution a few years ago, but I can no longer find any reference to it anywhere.

Has anyone had any experience automating Facebook Connect login or sign up? Any tips or strategies you can share would be greatly appreciated.

回答1:

The first thing you probably want to do is create test users.

The Facebook API lets you do this easily;

https://developers.facebook.com/docs/graph-api/reference/v2.0/app/accounts/test-users

This will give you a login url which should result in automatic login and as result drop a cookie which should help you avoid manual login within your application.

The api also allows you to create relationships and publish events



回答2:

You can connect to Facebook by logging in automatically using Selenium, I wrote it in Java.

It roughly looks like this:

void login()
{
    if(isElementFound(EMAIL_FIELD))
        driver.findElement(EMAIL_FIELD).sendKeys(username);
    if(isElementFound(PWD_FIELD))
        driver.findElement(PWD_FIELD).sendKeys(password);
    if(isElementFound(LOGIN_BUTTON))
        driver.findElement(LOGIN_BUTTON).click();
    if(waitForElement(MENU, 30))
        System.out.println("login Successful");
    else
        System.out.println("login un-successful");       
}


回答3:

@Test
public void facebookLogin() throws Exception {

    WebDriver driver = getDriver();
    driver.get("http://www.facebook.com");

    WebElement email = driver.findElement(By.name("email"));
    email.clear();
    email.sendKeys("myuser");

    WebElement password = driver.findElement(By.name("pass"));
    password.clear();
    password.sendKeys("mypass");

    WebElement loginbutton = driver.findElement(By.id("loginbutton"));
    loginbutton.click();

    System.out.println("done!");
}


回答4:

When I needed to do this, the option of creating test users was not sufficient - they were too short lived and we needed time to populate them with a particular set of attributes.

We ended up setting up servers that mocked the parts of the Facebook APIs that were exercised in our tests. We then redirected the test environment to resolve the facebook domain names to our those servers. It was not easy, but this approach had several advantages

  1. We could populate the users as we pleased without worrying about them disappearing
  2. We could have as many as we wanted
  3. We could control the performance of the FB APIs - and capture metrics on that performance during the test. If something was slow, we could be sure it wasn't a FB hiccup. If we wanted to test what happened to the target app if FB got slow, we could do that, too.