Autocompleting Selenium locators
Contents
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.
*** 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.
*** 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.
*** Tasks ***
Show Wikipedia
Open singleton browser https://wikipedia.org
Capture and crop page screenshot
... wikipedia.png
... css:H1
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.
*** 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'] > *
This simple is how autocompleting locators with RobotKernel works. Autocompleting supports most of the Selenium locator stregies:
For
id:
,name:
, andlink:
strategies RobotKernel will list all the available options.For other strategies it is required to give some beginning for the locator, and then, RobotKernel tries to find completed matches for them, or automatically autocompletes the locator if there is only one matching element found.
Then, finally, there is a very single special locator, empty css:
…
Interactive picker for the locator¶
When TAB
is pressed after empty css:
locator, RobotKernel does something special: RobotKernel prompts you to select the the element directly from the browser.
Let’s use this for pressing the search button.
Again, let’s copy the previous task, but after Input text
keyword, add Click button
css:
and press TAB
.
*** Tasks ***
Execute Wikipedia search
Open singleton browser https://wikipedia.org/
Input text
... id:searchInput
... Robot Framework
Click button
... css:FIELDSET > BUTTON
Capture and crop page screenshot wikipedia.png
... css:H1
These autocompletion and picker features are subject to change in the future RobotKernel releases, but hopefully only for better.
Closing the browser window¶
The “singleton” browser window used in this example is closed when the Jupyter client is shutown or kernel is shutdown from the Jupyter client user interface.
Yet, it might be a good practice to register a closing keyword at the end of notebook. Then it should be easy to avoid including it when authoring the notebook, but it would be taken account if the notebook was executed from the command line.