SeleniumBase/help_docs/recorder_mode.md

9.6 KiB
Executable File

Recorder Mode

🔴 SeleniumBase Recorder Mode lets you record & export browser actions into automation scripts.

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

pytest TEST_NAME.py --rec -s

🔴 To add manual actions, you'll need to create a breakpoint inside your test to activate "Debug Mode" while in "Recorder Mode": (For reference, "Debug Mode" is also known as the "ipdb debugger".)

import ipdb; ipdb.set_trace()

🔴 You can also activate Debug Mode at the start of your test by adding --trace as a pytest command-line option: (This is useful when running Recorder Mode without any breakpoints.)

pytest TEST_NAME.py --trace --rec -s

🔴 Once you've reached the breakpoint, you can take control of the browser and add in any actions that you want recorded. The Recorder will capture browser actions on URLs that begin with https:, http:, and file:; (the Recorder won't work on data: URLS). 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 as: recordings/my_first_test_rec.py
*******************************************************

🔴 If running tests from one module, recordings will share a file:

>>> RECORDING ADDED 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 --rec on a Python file such as this one:

from seleniumbase import BaseCase

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

🔴 The above code 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 within the test instead of using pytest --rec 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 during that part of the test.

🔴 SeleniumBase 1.66.1 adds the ability to record changes to "Choose File" input fields. Sometimes the "Choose File" input field is hidden on websites, so self.show_file_choosers() was added to get around this edge case. Version 1.66.1 also adds self.set_content_to_frame(frame), which lets you record actions inside of iframes.

🔴 SeleniumBase 1.66.2 adds the ability to save selectors using the ":contains(TEXT)" selector. If a Python file being recorded has multiple tests being run, then all those tests will get saved to the generated *_rec.py file. The Recorder will now save common self.assert_* calls made during tests. In order to escape iframes when using self.set_content_to_frame(frame), a new method was added: self.set_content_to_default(). The self.set_content_to_*() methods will be automatically used in place of self.switch_to_*() methods in Recorder Mode, unless a test explicitly calls self._rec_overrides_switch = False before the self.switch_to_*() methods are called. Additionally, if an iframe contains the src attribute, that page will get loaded in a new tab when switching to it in Recorder Mode.

🔴 SeleniumBase versions 1.66.3, 1.66.4, 1.66.5, 1.66.6, 1.66.7, 1.66.8, and 1.66.9 improve the algorithm for converting recorded actions into SeleniumBase code.

🔴 SeleniumBase 1.66.10 adds better error-handling to the Recorder. It also adds the console script option -r for sbase mkfile to generate a new test file with a breakpoint for Recorder Mode: sbase mkfile NEW_FILE.py -r

🔴 SeleniumBase 1.66.12 adds the ability to instantly create a new test recording by running sbase mkrec FILE.py. Once the browser spins up, you can open a new web page and start performing actions that will get recorded and saved to the file you specified.

🔴 SeleniumBase 1.66.13 lets you add assertions for elements and text while making a recording. To add an element assertion, press the [^]-key (SHIFT+6), (the border will become purple) then click on elements that you'd like to assert. To add a text assertion, press the [&]-key (SHIFT+7), (the border will become orange) then click on text elements that you'd like to assert. To go back to the regular Record Mode, press any other key. While in the special assertion modes, certain actions such as clicking on links won't have any effect. This lets you make assertions on elements without certain actions getting in the way.

🔴 SeleniumBase 1.66.14 improves the algorithm for converting recorded assertions into SeleniumBase code. Text assertions that contain the newline character will now be handled correctly. If a text assertion has a :contains selector, then the text assertion will be changed to an element assertion. Asserted text from multi-line assertions will use self.assert_text() on the first non-empty line. Asserted text from single-line assertions will use self.assert_exact_text(). Element assertions will be handled with self.assert_element().

🔴 SeleniumBase 2.0.1 adds the ability to preview selectors via the page title when hovering over elements. It also fixes an issue that may occur when opening up new URLs while in Recorder Mode.


To learn more about SeleniumBase, check out the Docs Site:
SeleniumBase.io Docs
All the code is on GitHub:
SeleniumBase on GitHub