Automate Login Page In Selenium

In this post we will explain how to automate login page in selenium, We will take an example of social networking website Facebook, this post will contain code to automate textbox and button click. Before starting with our code it’s important to setup testing environment which will require a tool called eclipse. No problem if you are not aware to how to download eclipse or jar installation, CLICK HERE and learn to install the required software and Jar files.

 

How to automate login page in selenium

After a successful installation, please follow below steps:

1.  Create new project with name “Testing” in eclipse
     (File -> New -> Project -> Select Java Project -> Enter project name “Testing”)
2. Under project create new package with name “facebook” in eclipse
     (Right click on project -> New -> Package -> Enter package name as “facebook”)
3. Under package name create class with name “Login”
     (Right click on package -> New -> Class -> Enter class name as “Login”-> OK)
4.  Import jar files in your project, click on download to download Jar files.
5. Open Login class and paste the below code inside the login class.

Code For Automating Login Page:

[php light=”true”]
</strong><strong>package</strong> facebook;

<strong>import</strong> org.openqa.selenium.By;
<strong>import</strong> org.openqa.selenium.WebDriver;
<strong>import</strong> org.openqa.selenium.firefox.FirefoxDriver;

<strong>public</strong> <strong>class</strong> Login {

<strong>public</strong> <strong>static</strong> <strong>void</strong> main(String[] args) <strong>throws</strong> InterruptedException {

&nbsp;// Create an object driver for accessing driver method’s
WebDriver driver = <strong>new</strong> FirefoxDriver();

&nbsp;// navigate() will open URL in <u>Firefox
</u>driver.navigate().to("https://www.facebook.com/");

System.<em>out</em>.println("Launching Browser");

// Maximize is used to maximize the window.
&nbsp;driver.manage().window().maximize();

// using ID of an element to identify element, <u>sendkeys</u> is used to send&nbsp;text in field
&nbsp;driver.findElement(By.<em>id</em>("email")).sendKeys("username");
driver.findElement(By.<em>id</em>("pass")).sendKeys("password");

// click method will click on Login button
driver.findElement(By.<em>id</em>("u_0_r")).click();

Thread.<em>sleep</em>(3000);

// This will close <u>firefox</u> window
&nbsp;driver.close();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
}

[/php]

To Run Programme:

— Right click on class “Login” -> select run as option -> Java Application

After clicking on java application, Automation will take place like this:
1. Firefox browser launching will take place.
2. Face book login page will open; Browser will enter email in email field
3. System will enter password in password field
4. Click on login button.
5. Home page will open.

Watch below video to understand how the execution will take place after running this code.

Leave a Reply

Your email address will not be published. Required fields are marked *