Sunday, November 22, 2015

Facebook Login using Selenium WebDriver


Can selenium automate Facebook? 

    Lets see how selenium handle our routine ativities with Facebook. Selenium can help you with logging in to your facebook account, read posts and respond on you behalf with Likes and Comments.

Lets start with login for now,


The basic steps we need to login are......

driver.findElement(By.id("email")).sendKeys(username);
driver.findElement(By.id("pass")).sendKeys(password);
driver.findElement(By.id("loginbutton")).click();

But this not how we do in our real time projects. We have to put them in a better way.
I have created a custom method to check the presence of an element. it is "isElementFound()".
This is to make sure you have the element loaded/visible on the page before acting on the element. If you direct click/act on an element and if it doesn't exists on webpage you will end up seeing exceptions. 

There are N number of ways this method is written. i have just written it as below as I liked this way.
It gets the element count and if its >0 then returns TRUE else FALSE.
So, you cal call this method and act on elements if it returns true. 


The Complete Code file (you can import it to your eclise and run as Java file)

====================================
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FaceBook {
   
    private static WebDriver driver;
    static String username;
    static String password;
   
    final By EMAIL_FIELD = By.id("email");
    final By PWD_FIELD = By.id("pass");
    final By LOGIN_BUTTON = By.id("loginbutton");
   
    public FaceBook(String uname, String pwd){
        username = uname;
        password = pwd;
        driver = new FirefoxDriver();
        driver.get("http://www.facebook.com");
    }
   
    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();
    }
    public static void main(String[] args)
    {      
        FaceBook actions = new FaceBook("yourname@gmail.com", "XXXXXXXX");
        actions.login();
        driver.quit();
    }
   
    public static boolean isElementFound(By element){
        if(driver.findElements(element).size()>0)
            return true;
        else
            return false;
    }

}
===============================

    Login is not just this,, you need to make sure the login went successful. That means on clicking the LOGIN button, we will make sure a definite object something like "Menu"/"Profile PIC"/"Home Tab" is loaded fully on the page that conforms you are indeed logged in successfully.

In real time automation this is very important and is referred to as "Verification" (or may be "Validation", as I never digged them too much).
So, you should not say you have logged in successfully unless you verify something like that.

Now, to do this we will check if some element (Lets take it as MENU) is visible after login. So i would add another like to my login() method at the bottom.

It is like....

    ..........
    ..........
    final By MENU = By.id("logoutMenu");

    ..........
    ..........

        if(isElementFound(MENU, 30))
            System.out.println("login Successful");
        else
            System.out.println("login un-successful");     


Complete method 

    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");       
    }

 

Now, what if the MENU is not loaded/visible immediately after clicking LOGIN button??
WebDriver doesn't wait for MENU after clicking on LOGIN, it directly tries to find MENu and retuen false and thus JAVA prints the message saying "login un-successful".

How to handle this?
The answer is waiting for an element....... There are N ways to wait for an element. Most of the time people write their own wait methods (customized) in their frameworks.

See my WebDriver tips page to use one of them.

No comments:

Post a Comment