Java tutorial
/* * Copyright (c) 2002-2016 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 org.openqa.selenium.WebElement; import com.gargoylesoftware.htmlunit.BrowserRunner; import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented; import com.gargoylesoftware.htmlunit.WebDriverTestCase; /** * Tests for {@link HtmlTextInput}. * * @author Ronald Brill */ @RunWith(BrowserRunner.class) public class HtmlTextInput2Test 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('text1');\n" + " alert(input.value + '-' + input.defaultValue);\n" + " input = document.createElement('input');\n" + " input.type = 'text';\n" + " alert(input.value + '-' + input.defaultValue);\n" + " var builder = document.createElement('div');\n" + " builder.innerHTML = '<input type=\"text\">';\n" + " input = builder.firstChild;\n" + " alert(input.value + '-' + input.defaultValue);\n" + " }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "<form>\n" + " <input type='text' id='text1'>\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('text1');\n" + " input = input.cloneNode(false);\n" + " alert(input.value + '-' + input.defaultValue);\n" + " input = document.createElement('input');\n" + " input.type = 'text';\n" + " input = input.cloneNode(false);\n" + " alert(input.value + '-' + input.defaultValue);\n" + " var builder = document.createElement('div');\n" + " builder.innerHTML = '<input type=\"text\">';\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='text' id='text1'>\n" + "</form>\n" + "</body></html>"; loadPageWithAlerts2(html); } /** * @throws Exception if the test fails */ @Test @Alerts({ "initial-initial", "initial-initial", "newValue-initial", "newValue-initial", "newValue-newDefault", "newValue-newDefault" }) public void resetByClick() throws Exception { final String html = "<html><head><title>foo</title>\n" + "<script>\n" + " function test() {\n" + " var text = document.getElementById('testId');\n" + " alert(text.value + '-' + text.defaultValue);\n" + " document.getElementById('testReset').click;\n" + " alert(text.value + '-' + text.defaultValue);\n" + " text.value = 'newValue';\n" + " alert(text.value + '-' + text.defaultValue);\n" + " document.getElementById('testReset').click;\n" + " alert(text.value + '-' + text.defaultValue);\n" + " text.defaultValue = 'newDefault';\n" + " alert(text.value + '-' + text.defaultValue);\n" + " document.forms[0].reset;\n" + " alert(text.value + '-' + text.defaultValue);\n" + " }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "<form>\n" + " <input type='text' 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-initial", "newValue-initial", "newValue-newDefault", "newValue-newDefault" }) public void resetByJS() throws Exception { final String html = "<html><head><title>foo</title>\n" + "<script>\n" + " function test() {\n" + " var text = document.getElementById('testId');\n" + " alert(text.value + '-' + text.defaultValue);\n" + " document.forms[0].reset;\n" + " alert(text.value + '-' + text.defaultValue);\n" + " text.value = 'newValue';\n" + " alert(text.value + '-' + text.defaultValue);\n" + " document.forms[0].reset;\n" + " alert(text.value + '-' + text.defaultValue);\n" + " text.defaultValue = 'newDefault';\n" + " alert(text.value + '-' + text.defaultValue);\n" + " document.forms[0].reset;\n" + " alert(text.value + '-' + text.defaultValue);\n" + " }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "<form>\n" + " <input type='text' id='testId' value='initial'>\n" + "</form>\n" + "</body></html>"; loadPageWithAlerts2(html); } /** * @throws Exception if the test fails */ @Test @Alerts({ "initial-initial", "default-default", "newValue-default", "newValue-newDefault" }) public void defaultValue() throws Exception { final String html = "<html><head><title>foo</title>\n" + "<script>\n" + " function test() {\n" + " var text = document.getElementById('testId');\n" + " alert(text.value + '-' + text.defaultValue);\n" + " text.defaultValue = 'default';\n" + " alert(text.value + '-' + text.defaultValue);\n" + " text.value = 'newValue';\n" + " alert(text.value + '-' + text.defaultValue);\n" + " text.defaultValue = 'newDefault';\n" + " alert(text.value + '-' + text.defaultValue);\n" + " }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "<form>\n" + " <input type='text' id='testId' value='initial'>\n" + "</form>\n" + "</body></html>"; loadPageWithAlerts2(html); } /** * @throws Exception if the test fails */ @Test @Alerts(DEFAULT = { "textLength not available" }, FF = { "7" }) public void textLength() throws Exception { final String html = "<html><head><title>foo</title>\n" + "<script>\n" + " function test() {\n" + " var text = document.getElementById('testId');\n" + " if(text.textLength) {\n" + " alert(text.textLength);\n" + " } else {\n" + " alert('textLength not available');\n" + " }\n" + " }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "<form>\n" + " <input type='text' id='testId' value='initial'>\n" + "</form>\n" + "</body></html>"; loadPageWithAlerts2(html); } /** * @throws Exception if an error occurs */ @Test @Alerts("0") public void selection() throws Exception { final String html = "<html><head><script>\n" + " function test() {\n" + " alert(getSelection(document.getElementById('text1')).length);\n" + " }\n" + " function getSelection(element) {\n" + " return element.value.substring(element.selectionStart, element.selectionEnd);\n" + " }\n" + "</script></head>\n" + "<body onload='test()'>\n" + " <input type='text' id='text1'/>\n" + "</body></html>"; loadPageWithAlerts2(html); } /** * @throws Exception if test fails */ @Test @Alerts(DEFAULT = { "0,0", "11,11", "3,11", "3,10" }, IE = { "0,0", "0,0", "3,3", "3,10" }) public void selection2_1() throws Exception { selection2(3, 10); } /** * @throws Exception if test fails */ @Test @Alerts(DEFAULT = { "0,0", "11,11", "0,11", "0,11" }, IE = { "0,0", "0,0", "0,0", "0,11" }) public void selection2_2() throws Exception { selection2(-3, 15); } /** * @throws Exception if test fails */ @Test @Alerts(DEFAULT = { "0,0", "11,11", "10,11", "5,5" }, IE = { "0,0", "0,0", "10,10", "5,5" }) public void selection2_3() throws Exception { selection2(10, 5); } private void selection2(final int selectionStart, final int selectionEnd) throws Exception { final String html = "<html>\n" + "<body>\n" + "<input id='myTextInput' value='Bonjour' type='text'>\n" + "<script>\n" + " var input = document.getElementById('myTextInput');\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + " input.value = 'Hello there';\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + " input.selectionStart = " + selectionStart + ";\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + " input.selectionEnd = " + selectionEnd + ";\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + "</script>\n" + "</body>\n" + "</html>"; loadPageWithAlerts2(html); } /** * @throws Exception if test fails */ @Test @Alerts(DEFAULT = { "0,0", "4,5", "10,10", "4,4", "1,1" }, IE = { "0,0", "4,5", "0,0", "0,0", "0,0" }) public void selectionOnUpdate() throws Exception { final String html = "<html>\n" + "<body>\n" + "<input id='myTextInput' value='Hello' type='text'>\n" + "<script>\n" + " var input = document.getElementById('myTextInput');\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + " input.selectionStart = 4;\n" + " input.selectionEnd = 5;\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + " input.value = 'abcdefghif';\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + " input.value = 'abcd';\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + " input.selectionStart = 0;\n" + " input.selectionEnd = 4;\n" + " input.value = 'a';\n" + " alert(input.selectionStart + ',' + input.selectionEnd);\n" + "</script>\n" + "</body>\n" + "</html>"; loadPageWithAlerts2(html); } /** * @throws Exception if the test fails */ @Test public void submitOnEnter() throws Exception { final String html = "<html>\n" + "<body>\n" + " <form action='result.html'>\n" + " <input id='t' value='hello'/>\n" + " </form>\n" + "</body>\n" + "</html>"; final WebDriver driver = loadPage2(html); final WebElement field = driver.findElement(By.id("t")); field.sendKeys("\n"); assertEquals(2, getMockWebConnection().getRequestCount()); } /** * @throws Exception if the test fails */ @Test @NotYetImplemented public void submitOnEnterWithoutForm() throws Exception { // this seem to be a bug in Selenium // real browsers simply ignore the missing form final String html = "<html>\n" + "<body>\n" + " <input id='t' value='hello'/>\n" + "</body>\n" + "</html>"; final WebDriver driver = loadPage2(html); final WebElement field = driver.findElement(By.id("t")); field.sendKeys("\n"); assertEquals(1, getMockWebConnection().getRequestCount()); } }