Autocompleting Selenium locators

A Jupyter Kernel is a long living process. It gets started after the first code cell is executed and it is shutdown only when explicitly requested so (or when the Jupyter client in use is shutdown).

For a Robot Framework kernel this means that the same kernel process outlives all the Robot Framework test runs it executes. This makes it possible for RobotKernel, for example, to “remember” all the Selenium connections (browsers) left open by the last Robot Framework run and reuse those at the next run.

And there is more: this makes it possible for RobotKernel to ask the currently open browser for available locators directly. Even inject an interactive picker to autocomplete a locator value.

Configuration

Let’s start with loading SeleniumLibrary and also SeleniumScreenshots, because it makes fun demos with Selenium.

But it is important that we don’t define the usual Suite teardown  Close all browsers, because we want to reuse the same browser window through multiple task executions.

[1]:
*** Settings ***

Library  SeleniumLibrary
Library  SeleniumScreenshots

Next we define one useful keyword for this approach: Open singleton browser. This only opens a new browser if the browser is not already open.

[7]:
*** Keywords ***

Open singleton browser
    [Documentation]  Open a browser or re-use the opened browser
    [Arguments]  ${url}=about:blank
    Open browser  ${url}  alias=singleton

Autocompleting locators

Now we are ready to learn, how to use RobotKernel’s Selenium locator autocompletion.

Let’s start with simply opening https://wikipedia.org/ in our singleton browser window.

[3]:
*** Tasks ***

Show Wikipedia
    Open singleton browser  https://wikipedia.org
    Capture and crop page screenshot
    ...  wikipedia.png
    ...  css:H1
../_images/notebooks_05_Interactive_Selenium_13_1.png

Now we should have https://wikipedia.org/ open. Try executing that cell multiple times. It should be pretty fast, because the browser is never closed between executions and the subsequent executions may uses the already open browser.

Now let’s repeat the previous keyword, but try to enter input into the search box by typing Input text id: and pressing TAB key, the Jupyter autocompletion default key.

After that TAB Jupyter should propose us all the available ids at the current page and also highlight them on the page.

Unfortunately, it is not currently possible to highlight them one by one while selecting (but you could highlight the selected one by pressing TAB after selecting the value to be autocompleted).

Thanks to Wikipedia’s design, it is easy to guess that id:searchInput is the locator for the search input field.

[4]:
*** Tasks ***

Type a search into Wikipedia's prompt
    Open singleton browser  https://wikipedia.org/
    Input text
    ...  id:searchInput
    ...  Robot Framework
    Capture and crop page screenshot  wikipedia.png
    ...  css:[id='search-form'] > *

Log |