@Test(expected = ElementNotVisibleException.class) public void throwExceptionWhenInteractingWithInvisibleElement() { server.setGetHandler( new HttpRequestCallback() { @Override public void call(HttpServletRequest req, HttpServletResponse res) throws IOException { res.getOutputStream() .println( "<!DOCTYPE html>" + "<html>" + " <head>\n" + " <title>test</title>\n" + " </head>\n" + " <body>\n" + " <input id=\"visible\">\n" + " <input style=\"display:none\" id=\"invisible\">\n" + " </body>" + "</html>"); } }); WebDriver d = getDriver(); d.get(server.getBaseUrl()); WebElement visibleInput = d.findElement(By.id("visible")); WebElement invisibleInput = d.findElement(By.id("invisible")); String textToType = "text to type"; visibleInput.sendKeys(textToType); assertEquals(textToType, visibleInput.getAttribute("value")); invisibleInput.sendKeys(textToType); }
@JavascriptEnabled @Ignore( value = {HTMLUNIT, SELENESE, ANDROID, OPERA, OPERA_MOBILE}, reason = "untested user agents") @Test public void testChordControlHomeShiftEndDelete() { // FIXME: macs don't have HOME keys, would PGUP work? if (Platform.getCurrent().is(Platform.MAC)) { return; } driver.get(pages.javascriptPage); WebElement result = driver.findElement(By.id("result")); WebElement element = driver.findElement(By.id("keyReporter")); element.sendKeys("!\"#$%&'()*+,-./0123456789:;<=>?@ ABCDEFG"); element.sendKeys(Keys.HOME); element.sendKeys("" + Keys.SHIFT + Keys.END); assertThat(result.getText(), containsString(" up: 16")); element.sendKeys(Keys.DELETE); assertThat(element.getAttribute("value"), is("")); }
/*Admin Login*/ public void admin(String userAd, String passAd) { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); siteAdmin.click(); userFieldAdmin.sendKeys(userAd); passFieldAdmin.sendKeys(passAd); loginButtonAdmin.click(); }
@JavascriptEnabled @Ignore( value = {HTMLUNIT, SELENESE, ANDROID, OPERA, OPERA_MOBILE}, reason = "untested user agents") @Test public void testChordReveseShiftHomeSelectionDeletes() { // FIXME: macs don't have HOME keys, would PGUP work? if (Platform.getCurrent().is(Platform.MAC)) { return; } driver.get(pages.javascriptPage); WebElement result = driver.findElement(By.id("result")); WebElement element = driver.findElement(By.id("keyReporter")); element.sendKeys("done" + Keys.HOME); assertThat(element.getAttribute("value"), is("done")); element.sendKeys("" + Keys.SHIFT + "ALL " + Keys.HOME); assertThat(element.getAttribute("value"), is("ALL done")); element.sendKeys(Keys.DELETE); assertThat(element.getAttribute("value"), is("done")); element.sendKeys("" + Keys.END + Keys.SHIFT + Keys.HOME); assertThat(element.getAttribute("value"), is("done")); assertThat( // Note: trailing SHIFT up here result.getText().trim(), containsString(" up: 16")); element.sendKeys("" + Keys.DELETE); assertThat(element.getAttribute("value"), is("")); }
@JavascriptEnabled @Ignore( value = {HTMLUNIT, IPHONE, SELENESE, ANDROID}, reason = "untested user agents") @Test public void testShouldReportKeyCodeOfArrowKeysUpDownEvents() { assumeFalse( Browser.detect() == Browser.opera && TestUtilities.getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.javascriptPage); WebElement result = driver.findElement(By.id("result")); WebElement element = driver.findElement(By.id("keyReporter")); element.sendKeys(Keys.ARROW_DOWN); assertThat(result.getText().trim(), containsString("down: 40")); assertThat(result.getText().trim(), containsString("up: 40")); element.sendKeys(Keys.ARROW_UP); assertThat(result.getText().trim(), containsString("down: 38")); assertThat(result.getText().trim(), containsString("up: 38")); element.sendKeys(Keys.ARROW_LEFT); assertThat(result.getText().trim(), containsString("down: 37")); assertThat(result.getText().trim(), containsString("up: 37")); element.sendKeys(Keys.ARROW_RIGHT); assertThat(result.getText().trim(), containsString("down: 39")); assertThat(result.getText().trim(), containsString("up: 39")); // And leave no rubbish/printable keys in the "keyReporter" assertThat(element.getAttribute("value"), is("")); }
@JavascriptEnabled @Ignore( value = {HTMLUNIT, IE, IPHONE, SELENESE, ANDROID}, reason = "firefox-specific. Android uses prev/next.") @Test public void testShouldReportKeyCodeOfArrowKeys() { assumeFalse( Browser.detect() == Browser.opera && TestUtilities.getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.javascriptPage); WebElement result = driver.findElement(By.id("result")); WebElement element = driver.findElement(By.id("keyReporter")); element.sendKeys(Keys.ARROW_DOWN); checkRecordedKeySequence(result, 40); element.sendKeys(Keys.ARROW_UP); checkRecordedKeySequence(result, 38); element.sendKeys(Keys.ARROW_LEFT); checkRecordedKeySequence(result, 37); element.sendKeys(Keys.ARROW_RIGHT); checkRecordedKeySequence(result, 39); // And leave no rubbish/printable keys in the "keyReporter" assertThat(element.getAttribute("value"), is("")); }
@JavascriptEnabled @Ignore( value = {SAFARI, MARIONETTE}, reason = " Safari: issue 4061. Other platforms: not properly tested") @Test public void testChangeEventIsFiredAppropriatelyWhenFocusIsLost() { driver.get(pages.javascriptPage); WebElement input = driver.findElement(By.id("changeable")); input.sendKeys("test"); moveFocus(); assertThat( driver.findElement(By.id("result")).getText().trim(), Matchers.<String>either(is("focus change blur")).or(is("focus blur change"))); input.sendKeys(Keys.BACK_SPACE, "t"); moveFocus(); // I weep. assertThat( driver.findElement(By.id("result")).getText().trim(), Matchers.<String>either(is("focus change blur focus blur")) .or(is("focus blur change focus blur")) .or(is("focus blur change focus blur change")) .or(is("focus change blur focus change blur"))); // What Chrome does }
/*Manage Categories*/ public void categoryManage(String titleCat, String aliasC, String textCat) { catmanagerButton.click(); newCategory.click(); titleFieldCat.sendKeys(titleCat); aliasCat.sendKeys(aliasC); toggleEdCat.click(); textFieldCategory.sendKeys(textCat); saveClose.click(); }
/*Edit Site Settings from HomePage*/ public void siteEdit(String siteNameText, String meta, String metaK) { siteSettings.click(); siteNameLabel.clear(); siteNameLabel.sendKeys(siteNameText); metaDescr.clear(); metaDescr.sendKeys(meta); metaKeys.clear(); metaKeys.sendKeys(metaK); saveSiteSettings.click(); }
@Test public void testSearchFeature() { // find search, check if search works by sending a search time, see that results exist WebElement element = driver.findElement(By.name("search_block_form")); element.sendKeys("Laboon"); element.sendKeys(Keys.RETURN); assert (driver.findElement(By.name("Laboon")) != null); // is it good enough? }
@Test public void testUntitled2() throws Exception { driver.get(baseUrl + "php4dvd/"); WebElement userNameWebEdit = driver.findElement(By.id("username")); WebElement passwordWebEdit = driver.findElement(By.name("password")); WebElement subminButton = driver.findElement(By.name("submit")); userNameWebEdit.clear(); userNameWebEdit.sendKeys("admin"); passwordWebEdit.clear(); passwordWebEdit.sendKeys("admin"); subminButton.click(); }
/*Edit Template Settings from HomePage*/ public void edit(String colour, String bkColour, String frontTitle, String descrpt) { templateSettings.click(); temColour.clear(); temColour.sendKeys(colour); backColour.clear(); backColour.sendKeys(bkColour); titleFront.clear(); titleFront.sendKeys(frontTitle); description.clear(); description.sendKeys(descrpt); saveColour.click(); }
/*Edit Article from Control Panel*/ public void editArtCP(String labelArt, String datee) throws InterruptedException { articleMangerCP.click(); articleClickToEdit.click(); Thread.sleep(5000); toggleEdfromCP.click(); editLabelArt.clear(); editLabelArt.sendKeys(labelArt); publish.click(); FinishPub.clear(); FinishPub.sendKeys(datee); saveEditArt.click(); }
@JavascriptEnabled @Test public void testSendingKeysToAnotherElementShouldCauseTheBlurEventToFire() { assumeFalse(browserNeedsFocusOnThisOs(driver)); driver.get(pages.javascriptPage); WebElement element = driver.findElement(By.id("theworks")); element.sendKeys("foo"); WebElement element2 = driver.findElement(By.id("changeable")); element2.sendKeys("bar"); assertEventFired("blur"); }
@JavascriptEnabled @Ignore( value = {OPERA, IPHONE, SELENESE, ANDROID}, reason = "untested user agents, Opera: F4 triggers sidebar") @Test public void testFunctionKeys() { driver.get(pages.javascriptPage); WebElement element = driver.findElement(By.id("keyReporter")); element.sendKeys("FUNCTION" + Keys.F4 + "-KEYS" + Keys.F4); element.sendKeys("" + Keys.F4 + "-TOO" + Keys.F4); assertThat(element.getAttribute("value"), is("FUNCTION-KEYS-TOO")); }
private static void val(WebDriver driver, WebElement element, String value) { if (isSelectTag(element)) { new Select(element).selectByValue(value); } else if (isInputTag(element) || isTextareaTag(element)) { if (isInputWithTypeWeDontChangeValue(element)) { String type = element.getAttribute("type"); warnAboutNotChangingValue("<input type=\"" + type + "\">", "#my" + capitalize(type)); return; } if (!isInputFileTag(element)) { element.clear(); } element.sendKeys(value); } else if (isOptionTag(element)) { warnAboutNotChangingValue("<option>", "#myOption"); } else if (isContentEditable(element)) { // #Cross-Driver if (DriverVersionUtils.getInstance().isHtmlUnitDriver(driver)) { changeContentEditableValueInHtmlUnit(driver, element, value); } else { if (driver instanceof FirefoxDriver) { // #Cross-Driver // in firefox, an editable div cannot be empty try { element.sendKeys( Keys.chord(Keys.CONTROL, Keys.HOME), Keys.chord(Keys.CONTROL, Keys.SHIFT, Keys.END)); element.sendKeys(value); } catch (ElementNotVisibleException e) { // we could work it out, possibly via JavaScript, but we decided not to, as a user would // not be able to edit it! throw new ElementNotVisibleException( "Empty contenteditable elements are not visible in Firefox, " + "so a user can't directly interact with them. Try picking an element before the contenteditable one " + "and sending the TAB key to it, so the focus is switched, and then try calling .val() to change its value.", e); } } else { element.clear(); element.sendKeys(value); } } } else { LOGGER.warn( "Function .val() called in element not known to be editable. Will attempt to send keys anyway. Element: " + element); element.clear(); element.sendKeys(value); } }
@Ignore( value = {IPHONE}, reason = "iPhone: sendKeys implemented incorrectly.") @Test public void testSendingKeyboardEventsShouldAppendTextInInputs() { driver.get(pages.formPage); WebElement element = driver.findElement(By.id("working")); element.sendKeys("some"); String value = element.getAttribute("value"); assertThat(value, is("some")); element.sendKeys(" text"); value = element.getAttribute("value"); assertThat(value, is("some text")); }
@JavascriptEnabled @Ignore( value = {HTMLUNIT, SELENESE, ANDROID, OPERA_MOBILE}, reason = "untested user agents") @Test public void testHomeAndEndAndPageUpAndPageDownKeys() { // FIXME: macs don't have HOME keys, would PGUP work? if (Platform.getCurrent().is(Platform.MAC)) { return; } driver.get(pages.javascriptPage); WebElement element = driver.findElement(By.id("keyReporter")); element.sendKeys( "abc" + Keys.HOME + "0" + Keys.LEFT + Keys.RIGHT + Keys.PAGE_UP + Keys.PAGE_DOWN + Keys.END + "1" + Keys.HOME + "0" + Keys.PAGE_UP + Keys.END + "111" + Keys.HOME + "00"); assertThat(element.getAttribute("value"), is("0000abc1111")); }
@JavascriptEnabled @Test public void testSendingKeysToAFocusedElementShouldNotBlurThatElement() { assumeFalse(browserNeedsFocusOnThisOs(driver)); driver.get(pages.javascriptPage); WebElement element = driver.findElement(By.id("theworks")); element.click(); // Wait until focused boolean focused = false; WebElement result = driver.findElement(By.id("result")); for (int i = 0; i < 5; ++i) { String fired = result.getText(); if (fired.contains("focus")) { focused = true; break; } try { Thread.sleep(200); } catch (InterruptedException e) { throw new RuntimeException(e); } } if (!focused) { fail("Clicking on element didn't focus it in time - can't proceed so failing"); } element.sendKeys("a"); assertEventNotFired("blur"); }
@JavascriptEnabled @Ignore( value = {HTMLUNIT, IPHONE, SELENESE, ANDROID}, reason = "untested user agents") @Test public void testNumberpadKeys() { driver.get(pages.javascriptPage); WebElement element = driver.findElement(By.id("keyReporter")); element.sendKeys( "abcd" + Keys.MULTIPLY + Keys.SUBTRACT + Keys.ADD + Keys.DECIMAL + Keys.SEPARATOR + Keys.NUMPAD0 + Keys.NUMPAD9 + Keys.ADD + Keys.SEMICOLON + Keys.EQUALS + Keys.DIVIDE + Keys.NUMPAD3 + "abcd"); assertThat(element.getAttribute("value"), is("abcd*-+.,09+;=/3abcd")); }
@JavascriptEnabled @Test public void shouldBeAbleToExecuteAsynchronousScripts() { driver.get(pages.ajaxyPage); WebElement typer = driver.findElement(By.name("typer")); typer.sendKeys("bob"); assertEquals("bob", typer.getAttribute("value")); driver.findElement(By.id("red")).click(); driver.findElement(By.name("submit")).click(); assertEquals( "There should only be 1 DIV at this point, which is used for the butter message", 1, getNumDivElements()); driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS); String text = (String) executor.executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "window.registerListener(arguments[arguments.length - 1]);"); assertEquals("bob", text); assertEquals("", typer.getAttribute("value")); assertEquals( "There should be 1 DIV (for the butter message) + 1 DIV (for the new label)", 2, getNumDivElements()); }
@JavascriptEnabled @Ignore( value = {ANDROID, IPHONE, OPERA, SAFARI, SELENESE, OPERA_MOBILE}, reason = "Android/iOS/Opera Mobile: does not support contentEditable;" + "Safari/Selenium: cannot type on contentEditable with synthetic events") @Test public void testTypingIntoAnIFrameWithContentEditableOrDesignModeSet() { driver.get(pages.richTextPage); driver.switchTo().frame("editFrame"); WebElement element = driver.switchTo().activeElement(); element.sendKeys("Fishy"); driver.switchTo().defaultContent(); WebElement trusted = driver.findElement(By.id("istrusted")); WebElement id = driver.findElement(By.id("tagId")); assertThat( trusted.getText(), anyOf( equalTo("[true]"), // Chrome does not set a trusted flag. equalTo("[n/a]"), equalTo("[]"))); assertThat(id.getText(), anyOf(equalTo("[frameHtml]"), equalTo("[theBody]"))); }
@Test public void testShouldBeAbleToEnterTextIntoATextAreaBySettingItsValue() { driver.get(pages.javascriptPage); WebElement textarea = driver.findElement(By.id("keyUpArea")); String cheesey = "brie and cheddar"; textarea.sendKeys(cheesey); assertThat(textarea.getAttribute("value"), equalTo(cheesey)); }
public void templateMng(String cbeez) { temManagerCP.click(); BeezTemp.click(); AdvanceBeez.click(); colorBeez.clear(); colorBeez.sendKeys(cbeez); saveCloseBeez.click(); }
@JavascriptEnabled @Test public void testShouldBeAbleToTypeOnAnEmailInputField() { driver.get(pages.formPage); WebElement email = driver.findElement(By.id("email")); email.sendKeys("foobar"); assertThat(email.getAttribute("value"), equalTo("foobar")); }
@Test public void testShouldBeAbleToMixUpperAndLowerCaseLetters() { driver.get(pages.javascriptPage); WebElement keyReporter = driver.findElement(By.id("keyReporter")); keyReporter.sendKeys("*****@*****.**"); assertThat(keyReporter.getAttribute("value"), is("*****@*****.**")); }
@JavascriptEnabled @Ignore( value = {HTMLUNIT, IPHONE, SELENESE, ANDROID, OPERA, SAFARI, OPERA_MOBILE}, reason = "untested user agents. Opera: F2 focuses location bar" + "Safari: issue 4221", issues = {4221}) @Test public void testShiftSelectionDeletes() { driver.get(pages.javascriptPage); WebElement element = driver.findElement(By.id("keyReporter")); element.sendKeys("abcd efgh"); assertThat(element.getAttribute("value"), is("abcd efgh")); element.sendKeys(Keys.SHIFT, Keys.LEFT, Keys.LEFT, Keys.LEFT); element.sendKeys(Keys.DELETE); assertThat(element.getAttribute("value"), is("abcd e")); }
@JavascriptEnabled @Ignore(value = {HTMLUNIT, SELENESE, OPERA, ANDROID, OPERA_MOBILE}) @Test public void testNonPrintableCharactersShouldWorkWithContentEditableOrDesignModeSet() { driver.get(pages.richTextPage); // not tested on mac if (Platform.getCurrent().is(Platform.MAC)) { return; } driver.switchTo().frame("editFrame"); WebElement element = driver.switchTo().activeElement(); element.sendKeys("Dishy", Keys.BACK_SPACE, Keys.LEFT, Keys.LEFT); element.sendKeys(Keys.LEFT, Keys.LEFT, "F", Keys.DELETE, Keys.END, "ee!"); assertEquals("Fishee!", element.getText()); }
@Test public void testShouldTypeLowerCaseLetters() { driver.get(pages.javascriptPage); WebElement keyReporter = driver.findElement(By.id("keyReporter")); keyReporter.sendKeys("abc def"); assertThat(keyReporter.getAttribute("value"), is("abc def")); }
@Test public void testShouldBeAbleToTypeCapitalLetters() { driver.get(pages.javascriptPage); WebElement keyReporter = driver.findElement(By.id("keyReporter")); keyReporter.sendKeys("ABC DEF"); assertThat(keyReporter.getAttribute("value"), is("ABC DEF")); }