Friday 11 November 2016

Working with forms in selenium webdriver

Hi,

Today we will be learning how to fill a form or how to enter data in a form using a script.We will be writing a automated script to enter the data in the form.


Script for filling a form


import java.util.*;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class WorkingWithforms
{

public static void main(String[] args)
{


WebDriver driver = new FirefoxDriver();

driver.get("any website with a form");

//Find Username textbox and enter value in it
driver.findElement(By.id("textUserName")).sendKeys("Manish");

//Find Password textbox and enter value in it
driver.findElement(By.name("txtPwd")).sendKeys("hello");


//Find Confirm Password textbox and enter value in it
driver.findElement(By.className("txtcnfrm")).sendKeys("hello");

//Find First Name textbox and enter value in it
driver.findElement(By.cssSelector("input.Format1")).sendKeys("Manish");

//Find Last Name textbox and enter value in it
driver.findElement(By.name("txtl")).sendKeys("Dangwal");

//Find Gender radio button and enter value in it
driver.findElement(By.cssSelector("input[value='male']")).click();

//Find Date Of Birth textbox and enter value
driver.findElement(By.name("dob")).sendKeys("01/05/1994");

//Find Email textbox and enter value in it
driver.findElement(By.name("Email")).sendKeys("manishdangwal8@gmail.com");

//Find Address textbox and enter value
driver.findElement(By.name("add")).sendKeys("villa");

//Select a city
Select city= new Select(driver.findElement(By.name("city")));

city.selectByIndex(1);

driver.close();
}

}

0 comments: