Wednesday, December 9, 2015

Facebook and Selenium - Friend Requests Automated - Part2




.... In continuation to out facebook friend request automation (see part1), we are going to to play around confirming the firiend requets now.

1) Confirm First Friends Request:


We will initialize our web elements:

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


 The method looks something like this:


    void confirmFirstFriendRequest()
{
        String friend = "";      
        if(waitForElement(FRIENDREQUEST_ICON, 15))
            driver.findElement(FRIENDREQUEST_ICON).click();
       
        if(waitForElement(FRIENDNAME, 15))
            friend = driver.findElement(FRIENDNAME).getText();

        if(!friend.isEmpty()){
            System.out.println("Latest Pending Friends request is from: "+friend);
            if(isElementFound(FRIENDNAME_CONFIRM)){
                driver.findElement(FRIENDNAME_CONFIRM).click();
                System.out.println("Confirmed the friend request: "+friend);
            }
        }
        else
            System.out.println("YOU HAVE NO NEW FRIEND REQUESTS");  
       
        driver.findElement(FRIENDREQUEST_ICON).click();
    }


2) Confirm All Friends 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");
    final By SEE_ALL_FRIEND_CONFIRM = By.xpath("div/div/div[2]/div/div/button");

 The method looks something like this:


void confirmAllPendingFriendRequests()
{      
        try {
            List<WebElement> allFriendsNodes = null;
          
            if(waitForElement(FRIENDREQUEST_ICON, 10))
                driver.findElement(FRIENDREQUEST_ICON).click();
          
            if(waitForElement(SEE_ALL_FRIENDREQUESTS, 15))
                driver.findElement(SEE_ALL_FRIENDREQUESTS).click();
          
            if(waitForElement(SEE_ALL_TOTAL_REQUEST, 15))
                allFriendsNodes = driver.findElements(SEE_ALL_TOTAL_REQUEST);
          
            if(allFriendsNodes != null){
                System.out.println("BELOW ARE THE CONFIRMED FRIEND NAMES:");
                System.out.println("--------------------------");
                for(int i=0; i<2; i++){
                //for(int i=0; i<allFriendsNodes.size(); i++){
                    List<WebElement> myFriend = null, myFriendConfirmButton = null;
                    if((myFriend=allFriendsNodes.get(i).findElements(SEE_ALL_FRIENDNAME)).size()>0){
                        if((myFriendConfirmButton=allFriendsNodes.get(i).findElements(SEE_ALL_FRIEND_CONFIRM)).size()>0){
                            myFriendConfirmButton.get(0).click();
                            System.out.println("CONFIRMED "+myFriend.get(0).getText());
                            Thread.sleep(1000);
                        }
                    }
                    else
                        System.out.println("Issue with reading friend name at the index: "+i);
                }
            }
            else
                System.out.println("Issue in See All Friend requests Page, Probably page didnt load in given time");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


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 ...   http://www.seleniumhq.in/2015/11/facebook-login-using-selenium-webdriver.html

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 = By.id("requestsCountValue");

The method looks something like this:

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

        if(!request_count.isEmpty())
            System.out.println("YOU HAVE NEW FRIEND REQUESTS: "+request_count);
        else
            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 = By.id("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))
            driver.findElement(FRIENDREQUEST_ICON).click();
      
        if(waitForElement(FRIENDNAME, 5))
            friend = driver.findElement(FRIENDNAME).getText();

        if(!friend.isEmpty())
            System.out.println("Latest Pending Friends request is from: "+friend);
        else
            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 "http://www.seleniumhq.in/p/blog-page_13.html"







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))
                driver.findElement(FRIENDREQUEST_ICON).click();
          
            if(waitForElement(SEE_ALL_FRIENDREQUESTS, 15))
                driver.findElement(SEE_ALL_FRIENDREQUESTS).click();
          
            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:");
                System.out.println("--------------------------");
                for(int i=0; i<allFriendsNodes.size(); i++){
                    List<WebElement> myFriend = null;
                    if((myFriend=allFriendsNodes.get(i).findElements(SEE_ALL_FRIENDNAME)).size()>0){
                        System.out.println(myFriend.get(0).getText());
                    }
                    else
                        System.out.println("Issue with reading friend name at the index: "+i);
                }
            }
            else
                System.out.println("Issue in See All Friend requests Page, Probably page didnt load in given time");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

----------------------------------------------

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"

<eop>

More scenarios to follow.....