Skip to content

Facebook message send automation using Watir-Webdriver

In my previous post I showed how to login and share a post in Facebook. This post will demonstrate how to send a message to friends in Facebook following some very simple steps.

First, we need to list down who we want to send the message to. If it’s a single person then we can set his/her name in the program. But if it’s a general message that we want to send to all our friends then typing all their names is not an option. So the friend list needs to be retrieved first.

Below code gets the name of the  friends from ‘Friends’ tab of Facebook and save that into a simple text file-

fbFriends = File.new("fbFriendList.txt", "w")

divs = browser.divs(:class, "fsl")

divs.each do |div|
 link = div.link(:index, 0)
 friends= link.text
 fbFriends.puts friends
end
fbFriends.close

This will create a text file named ‘fbFriendList’ into the same path where we ran the program from. Opening this text file will show all the person names who are friend with us in Facebook.

Now that we have got our friend list we can send message to them. Following code reads the friends name from ‘fbFriendList.txt’, set them as the recipient and send a general message to them.


browser.button(:value,"New Message").click
browser.text_field(:class, "inputtext textInput").flash

fbFriends = File.open("sampleFriend.txt")

while !fbFriends.eof?
 friends=fbFriends.readline
 browser.text_field(:class,"inputtext textInput").send_keys(friends)
 browser.text_field(:class,"inputtext textInput").send_keys :enter
end
fbFriends.close

browser.textarea(:index, 0).set("Please visit http://www.wateraid.org/uk and donate. Your £2 a month could help stop children dying from drinking dirty water.")
browser.button(:value,"Send").click

In case of sending a private message to a particular person we need to type the name of that friend instead of reading from the text file where all friends names are saved.


browser.button(:value,"New Message").click
browser.text_field(:class, "inputtext textInput").flash

browser.text_field(:class,"inputtext textInput").set("")  # Name of the friend goes here

browser.textarea(:index, 0).set("Please visit http://www.wateraid.org/uk and donate. Your £2 a month could help stop children dying from drinking dirty water.")
browser.button(:value,"Send").click

So the whole program from login into Facebook to message send and logout looks like this:


#****Sending message to all friends in Facebook

require "watir-webdriver"

signin={
 email: "", #The email that is used to sign in to Facebook
 password: "" #The password that is used to sign in to facebook
 }
event_URL_Friends="" #'Friends' tab's URL goes here
event_URL_Message="https://www.facebook.com/messages/"

browser = Watir::Browser.new
browser.goto "https://www.facebook.com/"

browser.text_field(:id, "email").set signin[:email]
browser.text_field(:id, "pass").set signin[:password]
browser.button(:type,"submit").click
browser.goto event_URL_Friends

fbFriends = File.new("fbFriendList.txt", "w")

divs = browser.divs(:class, "fsl")

divs.each do |div|
 link = div.link(:index, 0)
 friends= link.text
 fbFriends.puts friends
end
fbFriends.close

browser.goto event_URL_Message

browser.button(:value,"New Message").click
browser.text_field(:class, "inputtext textInput").flash

fbFriends = File.open("sampleFriend.txt")

while !fbFriends.eof?
 friends=fbFriends.readline
 browser.text_field(:class,"inputtext textInput").send_keys(friends)
 browser.text_field(:class,"inputtext textInput").send_keys :enter
end
fbFriends.close

browser.textarea(:index, 0).set("Please visit http://www.wateraid.org/uk and donate. Your £2 a month could help stop children dying from drinking dirty water.")
browser.button(:value,"Send").click
browser.link(:id, "navAccountLink").click
browser.button(:value,"Log out").click
browser.close

Advertisements

Text Editor or IDE

There are many Text Editor/IDE available where you can script for Ruby-Watir. Try using a few editor/IDE by yourself and seeing which one you like the best because what any particular person wants from an editor varies broadly. I recommend not to use simple notepad, there are much better free source code Editors/IDE available out there. Scripting in notepad and run from the command prompt is not something convenient.

If you are a beginner then SCiTE would be your favorite.  It is a good, light-weight text editor and supports both windows and Linux. At previous it was bundled along with the Ruby oneclick installer but later it has been removed not to dictate the editor’s choice for the user. You can download it from here

Another easy-to-use text editor is sublime. Though it has a very simple and clean look, it has some killer features that makes scripting easier and attractive. It remembers the words you have already typed, suggests and auto-completes them. It also provides syntax highlighting(though that is not very rich but better than the other text editors). Visit sublimetext.com to know more about this editor and download it.

Image

Alternatively you can use TestWise IDE which is a very good functional testing tool. One of its cool features is that it provides the support of running a test case as well as the entire test file. It has syntax validation and code auto-complete suggestion features. Though it comes up with a recorder but I would not encourage to record tests as that is hard to maintain and generate later. 

These are only some guidelines of Editors/IDEs that a user could start with. You will find more Text Editors/IDEs upon searching on Internet. Choose yours and start writing code to automate your web application!

Facebook Login and post share automation using Watir-Webdriver

Watir (pronounced as ‘water’) is an open source group of Ruby libraries that allows you to automate browser-based tests of web applications. It is an amazing browser driver, very light weight and simple to use. Any web applications written in any language can be automated with Watir and it simulates browser interactions the same way an end user would.

Unlike most of the other testing tools, it is very easy to setup in your machine, you don’t have to run a server or configure much of anything. You need to run gem install water-webdriver in command prompt and there you have it! Obviously you first need the Ruby installed in your system and then set up the watir-webdriver, but I am not going to detail that here.  Check out watir.com for the instructions of installation.

Write your first test script using Watir-Webdribver 

As Watir is built on Ruby scripting language, it’s easier to get into it if you have some knowledge in Ruby. But if you don’t, then too relax! you will love Ruby for its simplicity. Below is an example of  Facebook login and post share automation using Watir-Webdriver and Ruby that followed a very few steps. You will fall in love with Watir when you will see how straight forward it is.

Basically, here is what we are going to do:

Step 1: Open the browser and Navigate to https://facebook.com

Step 2: Enter username, password and Login

Step 3: Evaluate Login

Step 4: Share a post

Step 6: Logout

 require "watir-webdriver"

 signIn={
  email: "", # The email you use to sign in to Facebook
  password: "" # The password you use to sign in to Facebook
  }

 test_site="https://www.facebook.com/"
 browser=Watir::Browser.new
 browser.goto test_site

 browser.text_field(:id, "email").set signIn[:email]
 browser.text_field(:id, "pass").set signIn[:password]
 browser.button(:type,"submit").click

 if browser.text.include? "" #name of the logged in person goes inside the quotation
  puts "***Login successfull"
 else
  puts "***Error in Login"
 end

 browser.link(:class, "navLink").click
 browser.textarea(:class, "DOMControl_placeholder uiTextareaAutogrow input mentionsTextarea textInput").set("Testing using Watir, its amazing!")
 browser.button(:class, "_42ft _4jy0 _11b _4jy3 _4jy1 selected").click
 browser.link(:id, "navAccountLink").click
 browser.button(:value,"Log out").click
 browser.close
 
%d bloggers like this: