Monday, January 4, 2016

Selenium script to login Facebook

In the following post I will go step by step and explain how to write script using Selenium Webdriver for auto facebook login  and execute it .
1. Right click on the src folder ->New -> Package.
2. Provide package name something like com.stm.test and click “Finish”.
3. Right click on the newly created package – > New -> Class.
4. Provide class name as “RegistrationTest” and click Finish.
5. Write the code given below for your first test.

The code for the first test is as follows:

package com.stm.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
public class RegistrationTest
{
@Test 
public void testRegister()
{
WebDriver driver = new FirefoxDriver();
driver.get(“http://www.facebook.com/”);
driver.findElement(By.name(“email”)).sendKeys(“User1”);
driver.findElement(By.name(“pass”)).sendKeys(“Surname1”);

driver.findElement(By.Xpath(“//*[@id="u_0_v"]”)).click();
driver.close();
driver.quit();
}
}

6. After finishing the test right click on the test and click on RunAs – >TestNG Test
7. After executing the test select the project and press F5 to refresh the project. A new folder “test-results” will get created which will show you the results for the execution. Right click on index.html->open with->web browser to see the execution report.