Skip to content

Add SeleniumBase "Recorder Mode"

Compare
Choose a tag to compare
@mdmintz mdmintz released this 20 Sep 07:42
· 3966 commits to master since this release
7ad83c4

Add SeleniumBase "Recorder Mode"

🔴 SeleniumBase Recorder Mode gives you the power to create automation scripts from manual browser actions.
(Only Chromium browsers such as Chrome and Edge are supported.)

🔴 To activate Recorder Mode, add --recorder to your pytest run command when running an existing test: (Also add -s to allow breakpoints, unless you already have a pytest.ini file present with --capture=no in it.)

pytest TEST_NAME.py --recorder -s

🔴 To add your own actions inside the test, you'll need to create a breakpoint somewhere inside of it:

import pdb; pdb.set_trace()

🔴 Once you've reached the breakpoint, you can take control of the browser and add in any actions that you want recorded. When you are finished recording, type "c" on the command-line and press [Enter] to let the test continue from the breakpoint. After the test completes, a file called TEST_NAME_rec.py will be automatically created in the ./recordings folder, which will include the actions performed by the test, and the manual actions that you added in. Below is a command-line notification:

>>> RECORDING saved to: recordings/my_first_test_rec.py
*******************************************************

🔴 While a recording is in progress, you can hit the [ESC] key to pause the recording. To resume the recording, you can hit the [~`] key, which is located directly below the [ESC] key on most keyboards.

🔴 If you want to create a recording from scratch, just run:
pytest --recorder on a Python file such as this one:

from seleniumbase import BaseCase

class RecorderTest(BaseCase):
    def test_recorder(self):
        import pdb; pdb.set_trace()

🔴 The above file gives you a basic SeleniumBase file with a breakpoint in it so that you can immediately start recording after you've opened a new web page in the browser.

🔴 Recorder Mode works by saving your recorded actions into the browser's sessionStorage. SeleniumBase then reads from the browser's sessionStorage to take the raw data and generate a full test from it. Keep in mind that sessionStorage is only present for a website while the browser tab remains on a web page of the same domain/origin. If you leave that domain/origin, the sessionStorage of that tab will no longer have the raw data that SeleniumBase needs to create a full recording. To compensate for this, all links to web pages of a different domain/origin will automatically open a new tab for you while in Recorder Mode. Additionally, the SeleniumBase self.open(URL) method will also open a new tab for you in Recorder Mode if the domain/origin is different from the current URL. When the recorded test completes, SeleniumBase will scan the sessionStorage of all open browser tabs for the data it needs to generate the complete SeleniumBase automation script.

🔴 If you just want to record actions on a single URL of a multi-URL test, you can call self.activate_recorder() from the test instead of using pytest --recorder from the command-line. When doing so, make sure that the browser tab is still on the same domain/origin at the end of the test, or else SeleniumBase will not have access to the sessionStorage data that it needs for generating a complete test.

🔴 (Note that same domain/origin is not the same as same URL. Example: https://xkcd.com/353/ and https://xkcd.com/1537/ are two different URLs with the same domain/origin. That means that both URLs will share the same sessionStorage data, and that any changes to sessionStorage from one URL will carry on to the sessionStorage of a different URL when the domain/origin is the same. If you want to find out a website's origin during a test, just call: self.get_origin(), which returns the value of window.location.origin from the browser's console.)

🔴 The launch of Recorder Mode has brought a new SeleniumBase method along with it: self.open_if_not_url(URL). This method will open the URL given if the browser is not currently on that page. This is used as a method in recorded scripts when SeleniumBase detects that a click action has already brought the test to the given page. This method not only prevents an extra page load if not needed, but it also lets people know the current page of the browser at that point in the test.


Other changes in this release include:

  • Make the "Disable-CSP" extension more efficient.
  • Make improvements to self.set_value() to account for event listeners.
  • Add self.click_active_element().
  • Make improvements to the SeleniumBase Dashboard.
  • Update Python dependencies:
    -- setuptools>=58.0.4;python_version>="3.6"
    -- setuptools-scm>=6.3.2;python_version>="3.6"
    -- charset-normalizer==2.0.6;python_version>="3.5"
    -- more-itertools==8.10.0;python_version>="3.5"
    -- pytest-rerunfailures==10.2;python_version>="3.6"
    -- beautifulsoup4==4.10.0;python_version>="3.5"
    -- decorator==5.1.0;python_version>="3.5"
    -- virtualenv>=20.8.0
    -- rich==10.10.0;python_version>="3.6"