Example usage for org.openqa.selenium Keys TAB

List of usage examples for org.openqa.selenium Keys TAB

Introduction

In this page you can find the example usage for org.openqa.selenium Keys TAB.

Prototype

Keys TAB

To view the source code for org.openqa.selenium Keys TAB.

Click Source Link

Usage

From source file:org.jspringbot.keyword.selenium.SeleniumHelper.java

License:Open Source License

private String mapAsciiKeyCodeToKey(int keyCode) {
    Map<Integer, Keys> keysMap = new HashMap<Integer, Keys>();
    keysMap.put(0, Keys.NULL);/*from   www .  j  ava  2  s .  c  o  m*/
    keysMap.put(8, Keys.BACK_SPACE);
    keysMap.put(9, Keys.TAB);
    keysMap.put(10, Keys.RETURN);
    keysMap.put(13, Keys.ENTER);
    keysMap.put(24, Keys.CANCEL);
    keysMap.put(27, Keys.ESCAPE);
    keysMap.put(32, Keys.SPACE);
    keysMap.put(42, Keys.MULTIPLY);
    keysMap.put(43, Keys.ADD);
    keysMap.put(44, Keys.SUBTRACT);
    keysMap.put(56, Keys.DECIMAL);
    keysMap.put(57, Keys.DIVIDE);
    keysMap.put(59, Keys.SEMICOLON);
    keysMap.put(61, Keys.EQUALS);
    keysMap.put(127, Keys.DELETE);
    Keys key = keysMap.get(keyCode);

    if (key == null) {
        Character c = (char) keyCode;
        return c.toString();
    }

    return key.toString();
}

From source file:org.julianharty.accessibility.automation.KeyboardHelpers.java

License:Apache License

/**
 * Uses the tab key to navigate around a web page.
 *
 * Helps determine which elements are reachable with the tab key and
 * whether the first active element can be reached again (indicating we
 * can 'loop' around the tabable elements on the page.
 *
 * Any iFrames are visited recursively.//from  w  w w.  j a v a  2s . c o  m
 * 
 * A sample call follows:
 * <code><br/>
public void testTabbingThroughGoogleSearchResults() throws InterruptedException { <br/>
   FirefoxDriver driver = new FirefoxDriver(); <br/>
   driver.get("http://www.google.com/search?q=cheese"); <br/>
   int maxTabsToEnter = 300; <br/>
   int tabs = KeyboardHelpers.tabThroughWebPage(driver, maxTabsToEnter);<br/>
   assertTrue("Expected at least 50 tabs, only needed " + tabs, tabs > 50);<br/>
 } </code>
 *
 * @param driver a WebDriver connection with the target page loaded.
 * @param maxTabsToEnter the maximum tabs we should need to visit all the
 * tabable elements. Consider providing a 2x or 3x the expected number,
 * particularly for complex, unknown content.  
 * @return the number of tabs issued if the method reached an element that
 * matched the first active element, else -1 if it failed to complete
 * within the specified number of tabs. 
 * @throws InterruptedException if the call to sleep fails. 
 */
public static int tabThroughWebPage(WebDriver driver, int maxTabsToEnter) throws InterruptedException {

    WebElement firstElement = driver.switchTo().activeElement();
    WebElement currentElement = firstElement;
    currentSize = firstElement.getSize();

    int tabsIssued = 0;
    int iFrame = 0;
    String currentTagName = "(not set)";
    try {
        while (tabsIssued < (maxTabsToEnter)) {
            Point currentLocation = currentElement.getLocation();
            logDetailsOfWebElement(currentElement, currentLocation, tabsIssued);
            currentTagName = currentElement.getTagName();

            /*
             * for iframes switch to the iframe and call ourself recursively. We
             * think it always starts from _top of the iframe. That call will
             * tab until it 'falls out' of the active elements in the iframe.
             * Body will then be returned, then we need to switch back to the
             * parent.
             * 
             * Note: use periods to separate nested frames.
             */

            if (currentTagName.equals("iframe")) {
                driver.switchTo().frame(iFrame++);
                // A simple calculation to limit tabs in a 'broken' iFrame for now.
                tabThroughWebPage(driver, maxTabsToEnter - tabsIssued);
                // Will the following skip over the 'body' of the iFrame?
                driver.switchTo().defaultContent();
                currentElement = driver.switchTo().activeElement();
            }

            String currentTitle = getTitleOfCurrentElement(driver, currentElement);

            // Here is one of the termination conditions, if we find one of our titles.
            if (currentTitle.contains(TAB_KEYWORD + 0)) {
                logTerminationCondition(
                        String.format("Title %s of element matches the value set", currentTitle),
                        currentElement, tabsIssued);
                return tabsIssued;
            }

            setWebElementAttributesAsAppropriate(driver, currentElement, tabsIssued, currentTagName);

            currentElement.sendKeys(Keys.TAB); // "\t" also works
            tabsIssued++;
            // TODO 2011Aug16 Don't sleep with IE driver, it's too slow
            // Thread.sleep(500L);

            String previousTagName = currentTagName;
            Point previousLocation = currentLocation;
            Dimension previousSize = currentSize;
            currentElement = driver.switchTo().activeElement();
            currentLocation = currentElement.getLocation();
            currentSize = currentElement.getSize();
            currentTagName = currentElement.getTagName();

            /* Loop termination is still imprecise. Typically the first element
             * is the body element, however in some GWT applications it's a div
             * element for reasons I don't yet fathom. 
             * TODO(jharty): improve the precision and reliability of the 
             * matching as we learn more about how to interact with elements
             * on the page. 
             */

            if (GeneralHelpers.locationMatches(currentLocation, previousLocation)
                    && GeneralHelpers.dimensionsAreEqual(currentSize, previousSize)
                    && currentTagName.equals(previousTagName) && !currentTagName.equals("body")) {
                logTerminationCondition(String.format(
                        "Bad news: Element %s seems to match %s after tabbing. Are NativeEvents working?",
                        currentTagName, previousTagName), currentElement, tabsIssued);
                // Tell the user to check native events are working
                return -1;
            }

            if (currentElement.equals(firstElement) && tabsIssued >= 3) {
                logTerminationCondition("Current element matches first element", currentElement, tabsIssued);
                return tabsIssued;
            }
        }
    } catch (WebDriverException wde) {
        String innerHTML = (String) ((JavascriptExecutor) driver)
                .executeScript("return arguments[0].innerHTML;", currentElement);
        LOG.warning(String.format("Current Tag %s, InnerHTML for problem element is %s", currentTagName,
                innerHTML));
        throw wde;
    }
    return -1;
}

From source file:org.julianharty.accessibility.automation.LayoutAndOrdering.java

License:Apache License

/**
 * Tests how the reported location of an element varies as the font size grows.
 * /*from  w ww.j a  va 2 s . com*/
 * In theory, I would have expected the offset to either remain constant,
 * or to increase as the rendered font size increases. However, the 
 * debug text from this test indicates the co-ordinates vary slightly, and
 * may increase or decrease after the font size is changed.
 */
public void testOffsetIncreasesAfterControlPlusKeyStroke() {
    driver.get("http://localhost:" + port + "/explicittabindex.html");
    WebElement firstElement = driver.switchTo().activeElement();
    WebElement currentElement = firstElement;

    // Go to first tabbable element on page
    currentElement.sendKeys(Keys.TAB);
    currentElement = driver.switchTo().activeElement();
    Point initialElementLocation = currentElement.getLocation();
    System.out.println("testOffsetIncreasesAfterControlPlusKeyStroke");
    int maxChanges = 5;

    // First increase the rendered font size
    Keys modifierKey = Keys.ADD;
    applyModifierKeyTo(currentElement, maxChanges, modifierKey);

    // Now decrease the rendered font size
    modifierKey = Keys.SUBTRACT;
    applyModifierKeyTo(currentElement, maxChanges, modifierKey);

}

From source file:org.julianharty.accessibility.automation.LayoutAndOrdering.java

License:Apache License

private void tabLayoutTest(WebDriver driver) throws InterruptedException {
    WebElement bodyElement = driver.findElement(By.tagName("body"));
    WebElement firstElement = driver.switchTo().activeElement();
    WebElement currentElement = firstElement;
    Point preTabLocation = currentElement.getLocation();
    int tabsIssued = 0;
    while (tabsIssued < MAX_TABS) {
        // currentElement.sendKeys(Keys.CONTROL, Keys.ADD);
        currentElement.sendKeys(Keys.TAB); // "\t" also works
        tabsIssued++;//w  w w.  j  av  a  2  s .  c  o m
        Thread.sleep(50L);
        currentElement = driver.switchTo().activeElement();
        currentElement.sendKeys(Keys.TAB);
        currentElement = driver.switchTo().activeElement();

        Point postTabLocation = currentElement.getLocation();
        System.out.println(GeneralHelpers.printElementLocations(tabsIssued, preTabLocation, postTabLocation));

        if (GeneralHelpers.locationMatches(postTabLocation, preTabLocation)) {
            // log termination condition;
            // Tell the user to check native events are working
            throw new InterruptedException("We don't seem to have moved, abandoning this test.");
        }
        if (currentElement.equals(firstElement)
                || currentElement.getLocation().equals(firstElement.getLocation())) {
            // Declare victory :)
            System.out.println("!!!!!! Yay!!!");
            return;
        }
        if (GeneralHelpers.compareElementLocationsForSaneTabOrder(preTabLocation, postTabLocation, tolerance)) {
            fail(GeneralHelpers.printElementLocations(tabsIssued, preTabLocation, postTabLocation));
        }
        preTabLocation = postTabLocation;

    }
}

From source file:org.kie.wb.selenium.model.persps.authoring.ImportExampleModal.java

License:Apache License

public void selectCustomRepository(String repoUrl) {
    customRepositoryRadioOption.click();
    repoUrlInput.clear();/*from w w  w  .j a  v a 2s .  c o m*/
    repoUrlInput.sendKeys(repoUrl);
    repoUrlInput.sendKeys(Keys.TAB);
    next();
    Waits.elementPresent(By.id("projects"));
}

From source file:org.kuali.rice.krad.demo.uif.library.clientresponsiveness.DemoClientResponsivenessAjaxFieldQueryAft.java

License:Educational Community License

/**
 * focus, blur seem real flaky on xvfb, maybe click and tab will be better
 * @param name//from   ww w  .j a  va2 s. co m
 * @throws InterruptedException
 */
private void clickAndTabByName(String name) throws InterruptedException {
    waitAndClickByName(name);
    driver.switchTo().activeElement().sendKeys(Keys.TAB); // update to call typeTab() in 2.5+
}

From source file:org.kuali.rice.testtools.selenium.JiraIssueCreation.java

License:Educational Community License

@Test
public void testCreateJira() throws InterruptedException, IOException {
    for (Map<String, String> jiraMap : jiraMaps.values()) {

        // Jira/*  w  w w.  ja v a 2  s  .c o  m*/
        WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("create_link"),
                this.getClass().toString());
        driver.get(jiraBase + "/secure/CreateIssue!default.jspa");

        // Project
        WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("project-field"),
                this.getClass().toString()).sendKeys(jiraMap.get("jira.project"));

        // Issue type
        WebElement issue = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(),
                By.id("issuetype-field"), this.getClass().toString());
        issue.click();
        issue.sendKeys(Keys.BACK_SPACE);
        issue.sendKeys(jiraMap.get("jira.issuetype"));
        //            issue.sendKeys(Keys.ARROW_DOWN);
        issue.sendKeys(Keys.TAB);

        WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("issue-create-submit"),
                this.getClass().toString()).click();

        // Summary // TODO remove things that look like java object references
        // TODO if the error messages are the same for all jiras then include it in the summary
        WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("summary"),
                this.getClass().toString()).sendKeys(jiraMap.get("jira.summary"));

        // Components
        WebElement component = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(),
                By.id("components-textarea"), this.getClass().toString());
        String components = jiraMap.get("jira.component");
        StringTokenizer tokens = new StringTokenizer(components);
        while (tokens.hasMoreElements()) {
            component.click();
            component.sendKeys(tokens.nextToken());
            //                component.sendKeys(Keys.ARROW_DOWN);
            component.sendKeys(Keys.TAB);
        }

        // Description
        WebElement descriptionElement = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(),
                By.id("description"), this.getClass().toString());
        descriptionElement.click();
        descriptionElement.sendKeys(jiraMap.get("jira.description"));

        // Priority
        WebElement priority = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(),
                By.id("priority-field"), this.getClass().toString());
        priority.click();
        priority.sendKeys(Keys.BACK_SPACE);
        priority.sendKeys(jiraMap.get("jira.priority"));
        //            priority.sendKeys(Keys.ARROW_DOWN);
        priority.sendKeys(Keys.TAB);

        // Version
        WebElement version = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(),
                By.id("versions-textarea"), this.getClass().toString());
        version.click();
        version.sendKeys(jiraMap.get("jira.versions"));
        //            version.sendKeys(Keys.ARROW_DOWN);
        version.sendKeys(Keys.TAB);

        // Fix version
        WebElement fixVersion = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(),
                By.id("fixVersions-textarea"), this.getClass().toString());
        fixVersion.click();
        fixVersion.sendKeys(jiraMap.get("jira.fixVersions"));
        //            fixVersion.sendKeys(Keys.ARROW_DOWN);
        fixVersion.sendKeys(Keys.TAB);

        // Release notes unchecked
        WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("customfield_11621-1"),
                this.getClass().toString()).click();

        WebDriverUtils.waitFor(driver, 8, By.id("issue-create-submit"), this.getClass().toString());

        //            WebDriverUtils.acceptAlertIfPresent(driver); // Dialog present when running Se.....
    }
}

From source file:org.kuali.rice.testtools.selenium.WebDriverAftBase.java

License:Educational Community License

protected void assertFocusTypeTabError(String field, String textToType) throws InterruptedException {
    fireEvent(field, "focus");
    waitAndTypeByName(field, textToType);
    driver.switchTo().activeElement().sendKeys(Keys.TAB);
    Thread.sleep(500);//ww  w . j  a va  2s. com
    assertAttributeClassRegexMatches(field, REGEX_ERROR);
    clearTextByName(field);
}

From source file:org.kuali.rice.testtools.selenium.WebDriverLegacyITBase.java

License:Educational Community License

protected void typeTab() {
    jGrowl("Press Tab");
    driver.switchTo().activeElement().sendKeys(Keys.TAB);
}

From source file:org.musetest.selenium.values.KeystrokesStringSourceTests.java

License:Open Source License

@Test
void testSingleMixedInMiddle() throws MuseInstantiationException, ValueSourceResolutionError {
    ValueSourceConfiguration source = ValueSourceConfiguration.forTypeWithSource(KeystrokesStringSource.TYPE_ID,
            ValueSourceConfiguration.forValue("a{TAB}b"));
    Object result = source.createSource(new SimpleProject()).resolveValue(new MockStepExecutionContext());
    Assertions.assertEquals("a" + Keys.TAB + "b", result.toString());
}