Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Selenium Guest Book Test Case Java Code

 

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 Seleniumguestbooktest {
    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/seleniumguestbook/Guestbook.aspx";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    @Test
    public void testSeleniumguestbook() throws Exception {
        driver.get(baseUrl + "/seleniumguestbook/Guestbook.aspx");
        for (int second = 0;; second++) {
            if (second >= 60fail("timeout");
            try if (isElementPresent(By.id("submitButton"))) breakcatch (Exception e) {}
            Thread.sleep(1000);
        }

        assertTrue(isElementPresent(By.id("submitButton")));
        driver.findElement(By.id("nameTextBox")).clear();
        driver.findElement(By.id("nameTextBox")).sendKeys("seleniumtest");
        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("messageTextBox")).clear();
        driver.findElement(By.id("messageTextBox")).sendKeys("This is a test!!!");
        driver.findElement(By.id("submitButton")).click();
        for (int second = 0;; second++) {
            if (second >= 60fail("timeout");
            try if (isElementPresent(By.id("messageConfirmation"))) breakcatch (Exception e) {}
            Thread.sleep(1000);
        }

        // ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
    }

    @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;
        }
    }
}