To entering data into html form we can use this sample piece of code.
It open the desired webpage with browser and will enter data into webpage.
package com.testing_02;
import static org.junit.Assert.assertEquals;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class HtmlElement {
private Selenium selenium;
public static void main(String[] args) throws Exception{
HtmlElement he = new HtmlElement();
he.launch_browser();
he.my_test();
he.stop_selenium();
}
public void launch_browser() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://demo.tanmaysarkar.com/sample_01.html");
selenium.start();
selenium.windowFocus();
selenium.windowMaximize();
}
public void my_test() throws Exception {
selenium.open("http://demo.tanmaysarkar.com/sample_01.html");
System.out.println("Open url");
selenium.type("xpath=//input[contains(@id,'ts_first_name')]", "tanmay");
System.out.println("Provide First name");
selenium.type("xpath=//input[contains(@id,'ts_last_name')]", "sarkar");
System.out.println("Provide Last name");
selenium.type("xpath=//textarea[contains(@id,'ts_address')]", "my home address");
System.out.println("Provide first Address");
selenium.select("//select[contains(@id,'ts_country')]", "label=India");
System.out.println("Select country as India");
selenium.click("xpath=html/body/form/table/tbody/tr[5]/td[2]/p/label[1]/input");
System.out.println("Select Gender as Male");
selenium.click("xpath=.//*[@id='ts_checkbox2']");
System.out.println("Checked Footbal");
selenium.click("xpath=.//*[@id='ts_checkbox3']");
System.out.println("Checked hockey");
}
public void stop_selenium() throws Exception {
selenium.stop();
System.out.println("Test execution complate");
}
}
