Automate Facebook Login using Selenium WebDriver & Java

Senuri Samindi
5 min readJun 20, 2022

Create a project

  1. Open Eclipse
  2. Click on files

3. Select new and click on the project

4. Select java Project to create a basic java project and click next

5. Give any name (I gave “test1” as the name). also, check out the create module-info .java file since we create packages before creating class. Then click finish to create a project

6. Then Project will be created

Add the selenium jar file to the project

  1. Right-click on the project you created and select properties

2. Navigate to the Libraries

You’ll see two build paths as Modulepath and Classpath.

  • CLASSPATH is the location of user-defined classes or packages.
  • MODULEPATH is the location of user-defined modules.

3. To add the selenium .jar file, Since we use selenium for this project click on Classpath, then can access the ‘Add External JARs.

4. Go to the place you downloaded the selenium Jar file, select it and click on open

5. Click apply and close

6. Then the installation and configuration will be done. You can see the added library in the reference library section

Start Scripting

  1. Create a Package
  • Right-click on the src folder, select new, and click on the package (The package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts and to write a better maintainable code)
  • Give a name to the package (I gave facebook as the name)

2. Create a class

  • Right-click on the package, select new, and click on the class
  • Give a name to the class and since it is a simple java class enable public static void main and finish it

3. Then set the path for the browser

  • Call the setProperty method for it
  • setProperty method enables to set of the properties for the desired browser to be used in test automation.
  • The setProperty method has two attributes — “propertyName” and “value.”
  • The propertyName represents the name of the browser-specific driver, and the value points to the path of that browser driver.

4. Initiate browser driver and open it

  • We can initiate it using WebDriver
  • We have a separate interface called webdriver interface and have to create the reference of webdriver interface. So, create an object

5. Import the relevant packages

  • press ctrl + space and click on the relevant package to import it in selenium WebDriver
  • It will import the relevant two packages

6. We have to path the URL to navigate to the web page after opening the browser

  • Use driver.get method to navigate to a specific page using its URL.
  • It opens an URL and it will wait till the whole page gets loaded
  • The Facebook login page will be open while running the test

7. Send values to the User email and password input fields

  • Use Selenium locators to find the element on the page and also import the relevant packages. Then use send method to send values to the specific element.
select the marked word and click ctrl+space

8. Click the sign-in Button

  • Use locators to find the button element and click it using click method

9. Run the test using the execute button on the eclipse

--

--

Senuri Samindi

I am an unrelenting person who has marshaled all the knowledge, skills and abilities to win over audacious goals by constantly honing latent abilities.