Although a very basic thing, but when I actually started writing the code and made dry runs, I faced two basic challenges: identifying appropriate locators and disabling browser level notifications. But after numerous trials and errors, I was finally able to strike the right chord.

Steps covered:
1. Launch Facebook
2. Login using valid Credentials
3. In the News Feed Section, click on the status text box
4. Write the Status and Post-it
5. Take a Screenshot
6. Logout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.testersdock.testcase;

import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.apache.commons.io.FileUtils;

public class FBLogin {

public static void main(String[] args) throws InterruptedException, IOException {

//Creating reference of Webdriver Interface
WebDriver driver;

//Declare Facebook Credentials
String user="Enter your Username";
String pass="Enter your Password";

//Creating an instance of chrome level class to disable browser level notifications
ChromeOptions coptions = new ChromeOptions();
coptions.addArguments("--disable-notifications");

// Telling Selenium to find Chrome Driver
System.setProperty("webdriver.chrome.driver", "/Users/alapan/Desktop/chromedriver");

// Initialize browser
driver = new ChromeDriver(coptions);

// Launch Facebook
driver.get("http://facebook.com/");

//Wait
Thread.sleep(1000);

//Maximize Window
driver.manage().window().maximize();

//Wait
Thread.sleep(2000);

//Enter Username
WebElement userTextField = driver.findElement(By.id("email"));
userTextField.sendKeys(user);

//Wait
Thread.sleep(2000);

//Enter Password
WebElement PassTextField = driver.findElement(By.id("pass"));
PassTextField.sendKeys(pass);

//Wait
Thread.sleep(2000);

//Click on Login button
driver.findElement(By.name("login")).click();

//Wait
Thread.sleep(10000);

//Click on Accept All Cookies button
WebElement webElement = driver.findElement(By.cssSelector("div[aria-label="Accept All"]"));
webElement.click();

//In case if the browser shows - Click on Not Now for Remember Password button
//WebElement element = driver.findElement(By.cssSelector("div[aria-label="Not Now"]:nth-child(2)"));
//element.click();

//Click on What's on Your Mind?
WebElement TextArea = driver.findElement(By.cssSelector("div[role="button"] > div > span[style*="webkit-box-orient"]"));
TextArea.click();
Thread.sleep(3000);

//Click on the expanded text area
WebElement TextAreaExpanded = driver.findElement(By.cssSelector("div[aria-describedby*="placeholder"]"));
TextAreaExpanded.click();
TextAreaExpanded.sendKeys("Hello World");

//Wait
Thread.sleep(3000);

//Click On Post Button
WebElement PostBtn = driver.findElement(By.cssSelector("div[aria-label="Post"]"));
PostBtn.click();

//Wait
Thread.sleep(4000);

// Take Screenshot for Evidence
File srce = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

// Save the screenshot in the given path by the name FbStatus.png
FileUtils.copyFile(srce, new File("/Users/alapan/Desktop/FbStatus.png"));

//Wait
Thread.sleep(2000);

//Click on Account Settings Dropdown
WebElement AccSettings = driver.findElement(By.cssSelector("div[aria-label="Account"]"));
AccSettings.click();

//Click on Log out button
WebDriverWait wait = new WebDriverWait(driver, 8);
WebElement logout = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("i.hu5pjgll.lzf7d6o1.sp_09gpAzWhK7K.sx_2eadd2")));
logout.click();

//Wait
Thread.sleep(2000);

// Close the browser
driver.quit();

}
}

 
Update:

1. The web notifications were not getting disabled for Firefox. To do, that replace:

1
2
3
//Creating an instance of chrome level class to disable browser level notifications
ChromeOptions coptions = new ChromeOptions();
coptions.addArguments("--disable-notifications");

With:

1
2
3
4
5
6
7
//Disable Browser Notifications for Firefox
FirefoxProfile newProfile = new FirefoxProfile();
newProfile.setPreference("dom.webnotifications.enabled", false);
DesiredCapabilities desCap = DesiredCapabilities.firefox();
desCap.setCapability(FirefoxDriver.PROFILE, newProfile);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(desCap);

 

2. Updated the locators for the Login button, Status Text Box, Post button, Account settings, and Logout button. Added the button click for Accept All cookies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Click on Login button
driver.findElement(By.name("login")).click();

//Click on Accept All Cookies button
WebElement webElement = driver.findElement(By.cssSelector("div[aria-label="Accept All"]"));
webElement.click();

//Click on What's on Your Mind?
WebElement TextArea = driver.findElement(By.cssSelector("div[role="button"] > div > span[style*="webkit-box-orient"]"));
TextArea.click();
Thread.sleep(3000);

//Click on the expanded text area
WebElement TextAreaExpanded = driver.findElement(By.cssSelector("div[aria-describedby*="placeholder"]"));
TextAreaExpanded.click();
TextAreaExpanded.sendKeys("Hello World");

//Click On Post Button
WebElement PostBtn = driver.findElement(By.cssSelector("div[aria-label="Post"]"));
PostBtn.click();

//Click on Account Settings Dropdown
WebElement AccSettings = driver.findElement(By.cssSelector("div[aria-label="Account"]"));
AccSettings.click();

//Click on Log out button
WebDriverWait wait = new WebDriverWait(driver, 8);
WebElement logout = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("i.hu5pjgll.lzf7d6o1.sp_09gpAzWhK7K.sx_2eadd2")));
logout.click();