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


No comments:

Post a Comment