Hybrid/Web Elements

Demo Video

Switch to a web view context

Ruby:

driver.switch_to.window('WEBVIEW')

Java:

driver.switchTo().window("WEBVIEW");

If multiple web views are displayed, the command get window handles is then returning the names of the different web views.

Supported Element Locators

Id
Finds the element by Id.
name
Finds the element by content description (accessibility label).
link text
Finds the element by text.
partial link text
Finds the element by partial text.
class
Finds the element by full class name (e.g. android.widget.Button).
xpath
Finds the element by a xpath expression.
tag name
Finds the element by tag name.
css
Finds the element by a css locator.

Web locator examples

Based on a sample html (Source ), the locators can be used in the following way:

<html>
<head><title>sample page</title></head>
<body class="logged_out env-production">
<div id="main">
    <div id="header" class="true">
        ...
        <div class="topsearch">
            <ul class="nav logged_out">
                <li class="pricing">
                    <a href="https://github.com/plans">Signup and Pricing</a>
                </li>
                <li class="explore">
                    <a href="https://github.com/explore">Explore GitHub</a>
                </li>
                <li class="features">
                    <a href="https://github.com/features">Features</a>
                </li>
                <li class="blog">
                    <a href="https://github.com/blog">Blog</a>
                </li>
                <li class="login">
                    <a href="https://github.com/login">Login</a>
                </li>
            </ul>
        </div>
        ...
    </div>
    ...
</div>
...
</body>
</html>

By Id

Means the id of an element that is described in the html source of the web page.

Ruby:

driver.find_element(:id,'header')

Java:

driver.findElement(By.id("header"));

By Name

Is mapped to the name of the view element.

Ruby:

driver.find_element(:name,'name')

Java:

driver.findElement(By.name("name"));

By Link text

Mapped to the displayed text of the element.

Ruby:

driver.find_element(:link_text,'Blog')

Java:

driver.findElement(By.linkText("Blog"));

By Partial Link Text

Mapped to the displayed text of the element.

Ruby:

driver.find_element(:partial_link_text,'Blo')

Java:

driver.findElement(By.partialLinkText("Blo"));

By Class

Mapped to the ui element class of the view.

Ruby:

driver.find_element(:class_name,'login')

Java:

driver.findElement(By.className("login"));

By Tag Name

Mapped to the name of the ui element tag e.g.: a.

Ruby:

driver.find_element(:tag_name,'a')

Java:

driver.findElement(By.tagName("a"));

By XPath

XPath is the language used for locating nodes in an XML document. XPath extends beyond (as well as supporting) the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third Button on the activity.

Ruby:

driver.find_element(:xpath,'//a[@title='logo']')

Java:

driver.findElement(By.xpath("//a[@title='logo']"));

By Css

Ruby:

driver.find_element(:css,'ul.nav li')

Java:

driver.findElement(By.cssSelector("ul.nav li"));

Supported Element Interactions

  • Get Text
  • Click
  • Send Keys
  • Get Attribute of Element
  • Clear
  • Submit
  • Is Selected
  • Is Displayed
  • Is Enabled
  • Get Size
  • Get Location