com.gargoylesoftware.htmlunit.html.HtmlButtonInput2Test.java Source code

Java tutorial

Introduction

Here is the source code for com.gargoylesoftware.htmlunit.html.HtmlButtonInput2Test.java

Source

/*
 * Copyright (c) 2002-2017 Gargoyle Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gargoylesoftware.htmlunit.html;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.gargoylesoftware.htmlunit.BrowserRunner;
import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts;
import com.gargoylesoftware.htmlunit.WebDriverTestCase;

/**
 * Tests for {@link HtmlButtonInput}.
 *
 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
 * @author Ronald Brill
 * @author Frank Danek
 */
@RunWith(BrowserRunner.class)
public class HtmlButtonInput2Test extends WebDriverTestCase {

    /**
     * @throws Exception if the test fails
     */
    @Test
    @Alerts({ "-", "-", "-" })
    public void defaultValues() throws Exception {
        final String html = "<html><head><title>foo</title>\n" + "<script>\n" + "  function test() {\n"
                + "    var input = document.getElementById('button1');\n"
                + "    alert(input.value + '-' + input.defaultValue);\n"

                + "    input = document.createElement('input');\n" + "    input.type = 'button';\n"
                + "    alert(input.value + '-' + input.defaultValue);\n"

                + "    var builder = document.createElement('div');\n"
                + "    builder.innerHTML = '<input type=\"button\">';\n" + "    input = builder.firstChild;\n"
                + "    alert(input.value + '-' + input.defaultValue);\n" + "  }\n" + "</script>\n"
                + "</head><body onload='test()'>\n" + "<form>\n" + "  <input type='button' id='button1'>\n"
                + "</form>\n" + "</body></html>";

        loadPageWithAlerts2(html);
    }

    /**
     * @throws Exception if the test fails
     */
    @Test
    @Alerts({ "-", "-", "-" })
    public void defaultValuesAfterClone() throws Exception {
        final String html = "<html><head><title>foo</title>\n" + "<script>\n" + "  function test() {\n"
                + "    var input = document.getElementById('button1');\n" + "    input = input.cloneNode(false);\n"
                + "    alert(input.value + '-' + input.defaultValue);\n"

                + "    input = document.createElement('input');\n" + "    input.type = 'button';\n"
                + "    input = input.cloneNode(false);\n" + "    alert(input.value + '-' + input.defaultValue);\n"

                + "    var builder = document.createElement('div');\n"
                + "    builder.innerHTML = '<input type=\"button\">';\n" + "    input = builder.firstChild;\n"
                + "    input = input.cloneNode(false);\n" + "    alert(input.value + '-' + input.defaultValue);\n"
                + "  }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "<form>\n"
                + "  <input type='button' id='button1'>\n" + "</form>\n" + "</body></html>";

        loadPageWithAlerts2(html);
    }

    /**
     * @throws Exception if the test fails
     */
    @Test
    @Alerts({ "initial-initial", "initial-initial", "newValue-newValue", "newValue-newValue",
            "newDefault-newDefault", "newDefault-newDefault" })
    public void resetByClick() throws Exception {
        final String html = "<html><head><title>foo</title>\n" + "<script>\n" + "  function test() {\n"
                + "    var button = document.getElementById('testId');\n"
                + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    document.getElementById('testReset').click;\n"
                + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    button.value = 'newValue';\n" + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    document.getElementById('testReset').click;\n"
                + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    button.defaultValue = 'newDefault';\n"
                + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    document.forms[0].reset;\n" + "    alert(button.value + '-' + button.defaultValue);\n"
                + "  }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "<form>\n"
                + "  <input type='button' id='testId' value='initial'>\n"
                + "  <input type='reset' id='testReset'>\n" + "</form>\n" + "</body></html>";

        loadPageWithAlerts2(html);
    }

    /**
     * @throws Exception if the test fails
     */
    @Test
    @Alerts({ "initial-initial", "initial-initial", "newValue-newValue", "newValue-newValue",
            "newDefault-newDefault", "newDefault-newDefault" })
    public void resetByJS() throws Exception {
        final String html = "<html><head><title>foo</title>\n" + "<script>\n" + "  function test() {\n"
                + "    var button = document.getElementById('testId');\n"
                + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    document.forms[0].reset;\n" + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    button.value = 'newValue';\n" + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    document.forms[0].reset;\n" + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    button.defaultValue = 'newDefault';\n"
                + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    document.forms[0].reset;\n" + "    alert(button.value + '-' + button.defaultValue);\n"
                + "  }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "<form>\n"
                + "  <input type='button' id='testId' value='initial'>\n" + "</form>\n" + "</body></html>";

        loadPageWithAlerts2(html);
    }

    /**
     * @throws Exception if the test fails
     */
    @Test
    @Alerts({ "initial-initial", "default-default", "newValue-newValue", "newDefault-newDefault" })
    public void defaultValue() throws Exception {
        final String html = "<html><head><title>foo</title>\n" + "<script>\n" + "  function test() {\n"
                + "    var button = document.getElementById('testId');\n"
                + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    button.defaultValue = 'default';\n"
                + "    alert(button.value + '-' + button.defaultValue);\n"

                + "    button.value = 'newValue';\n" + "    alert(button.value + '-' + button.defaultValue);\n"
                + "    button.defaultValue = 'newDefault';\n"
                + "    alert(button.value + '-' + button.defaultValue);\n" + "  }\n" + "</script>\n"
                + "</head><body onload='test()'>\n" + "<form>\n"
                + "  <input type='button' id='testId' value='initial'>\n" + "</form>\n" + "</body></html>";

        loadPageWithAlerts2(html);
    }

    /**
     * @throws Exception if the test fails
     */
    @Test
    @Alerts("foo")
    public void click_onClick() throws Exception {
        final String html = "<html><head><title>foo</title></head><body>\n"
                + "<form id='form1' onSubmit='alert(\"bar\")'>\n"
                + "  <input type='button' name='button' id='button' "
                + "onClick='alert(\"foo\")'>Push me</button>\n" + "</form></body></html>";

        final WebDriver driver = loadPage2(html);
        driver.findElement(By.id("button")).click();

        verifyAlerts(driver, getExpectedAlerts());
    }

    /**
     * @throws Exception if the test fails
     */
    @Test
    @Alerts("foo")
    public void click_onClickIgnoreCase() throws Exception {
        final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
                + "  <input type='button' name='button' id='button' "
                + "oNclICK='alert(\"foo\")'>Push me</button>\n" + "</form></body></html>";

        final WebDriver driver = loadPage2(html);
        driver.findElement(By.id("button")).click();

        verifyAlerts(driver, getExpectedAlerts());
    }
}