Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Testing form with Selenium Code Sample Java

 

package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SeleniumTestForm {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://www.seleniummaster.com/seleniumformtest/registrationform.aspx";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testSeleniumTestForm() throws Exception {
        driver.get(baseUrl + "/seleniumformtest/registrationform.aspx");
        driver.findElement(By.id("firstNameTextBox")).clear();
        driver.findElement(By.id("firstNameTextBox")).sendKeys("abcd");
        driver.findElement(By.id("lastNameTextBox")).clear();
        driver.findElement(By.id("lastNameTextBox")).sendKeys("abcd");
        driver.findElement(By.id("emailTextBox")).clear();
        driver.findElement(By.id("emailTextBox")).sendKeys(" This email address is being protected from spambots. You need JavaScript enabled to view it. ");
        driver.findElement(By.id("phoneTextBox")).clear();
        driver.findElement(By.id("phoneTextBox")).sendKeys("555-123-4567");
        new Select(driver.findElement(By.id("booksDropDownList"))).selectByVisibleText("Selenium RC");
        driver.findElement(By.id("osRadioButtonList_2")).click();
        driver.findElement(By.id("registerButton")).click();
        Assert.AreEqual("Registration Successful",driver.FindElement(By.Id("registrationconfirmation")).Text);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        catch (NoSuchElementException e) {
            return false;
        }
    }
}