Sunday, December 6, 2015

Facebook and Selenium - Friend Requests Automated.

    Last post was about notification and messages. No, we will explore something on Friend Requests.

    I am writing the direct code below. to understand the context and the link between my code across realated posts, you are requested to read my first post on FB (login)
Here you go ...

You may also want to read other facebook related posts, then on....

So, how do you find out the new friend requets? We will try to know the count of new friends requests.

1) New Friends Requests:


We will initialize our web elements:

    final By FRIENDREQUEST_COUNT ="requestsCountValue");

The method looks something like this:

    void printFriendRequests()
        String request_count = "";
            request_count = driver.findElement(FRIENDREQUEST_COUNT).getText();

            System.out.println("YOU HAVE NEW FRIEND REQUESTS: "+request_count);
            System.out.println("YOU HAVE NO NEW FRIEND REQUESTS");  



2) Latest Friend Request From:

Lets see how to print the latest friend request from.

We will initialize our web elements:

    final By FRIENDREQUEST_ICON ="fbRequestsJewel");
    final By FRIENDNAME = By.xpath("//div[@class='fbRequestList']/ul/li/div/div/div/div[2]/div[2]/div/span/a");

The method looks something like this:

    void printFirstPendingFriendRequestor()
        String friend = "";      
        if(waitForElement(FRIENDREQUEST_ICON, 5))
        if(waitForElement(FRIENDNAME, 5))
            friend = driver.findElement(FRIENDNAME).getText();

            System.out.println("Latest Pending Friends request is from: "+friend);
            System.out.println("YOU HAVE NO NEW FRIEND REQUESTS");  


This should print the name "David Math".

Wondering how this new "waitForElement" crept in?? See the implemenation at this page ""

3) All Pending Friend Requests From:

Lets see how to print all the pending friend requests.

We will initialize our web elements:

    final By SEE_ALL_FRIENDREQUESTS = By.xpath("//div[@class='jewelFooter']/a");
    final By SEE_ALL_TOTAL_REQUEST = By.xpath("//div[@class='phl']/div");
    final By SEE_ALL_FRIENDNAME = By.xpath("div/div[2]/div/a");

The method looks something like this:
 void findAllPendingFriendRequests(){      
        try {
            List<WebElement> allFriendsNodes = null;
            if(waitForElement(FRIENDREQUEST_ICON, 10))
            if(waitForElement(SEE_ALL_FRIENDREQUESTS, 15))
            if(waitForElement(SEE_ALL_TOTAL_REQUEST, 15))
                allFriendsNodes = driver.findElements(SEE_ALL_TOTAL_REQUEST);
            if(allFriendsNodes != null){
                System.out.println("BELOW ARE THE FRIEND NAMES:");
                for(int i=0; i<allFriendsNodes.size(); i++){
                    List<WebElement> myFriend = null;
                        System.out.println("Issue with reading friend name at the index: "+i);
                System.out.println("Issue in See All Friend requests Page, Probably page didnt load in given time");
        } catch (Exception e) {


This is very tricky part of the entire flow for a new starter.
See the Initialization. The element SEE_ALL_TOTAL_REQUEST refers to all DIV elements that represent each friend.

So, I would like to navigate to that each div and iterate through them to find the friend SEE_ALL_FRIENDNAME by using nested findElements method.
//    allFriendsNodes = driver.findElements(SEE_ALL_TOTAL_REQUEST);

I will capture all those friend DIV elements in "allFriendsNodes"
Then i will make sure those are >0 (not NULL)

//    if(allFriendsNodes != null){

I will then iterate through each node

//    for(int i=0; i<allFriendsNodes.size(); i++){

 I will then find the name of the each friend under the main DIV by using nested findElements method.

 //   if((myFriend=allFriendsNodes.get(i).findElements(SEE_ALL_FRIENDNAME)).size()>0){

Remember the xpath for SEE_ALL_FRIENDNAME doesnt start with double forward slash (not from top most node), it is because the relative element shouldn't use this convetion. It is similar to the paths convention.
We want driver to find the name of the friend inside "SEE_ALL_TOTAL_REQUEST"
its like "Finding SEE_ALL_FRIENDNAME element inside SEE_ALL_TOTAL_REQUEST element"


More scenarios to follow.....

No comments:

Post a Comment