Creating a GUI Test from a Python IDE
If you as a Python developer prefer to use your own Selenium script instead of a YAML configuration file, you can do so by modifying your script to automatically create a GUI Functional Test when executed from your local Python IDE.
Here we will outline required modifications. For the purpose of this guide, all examples here will assume a Selenium script written in Python, though Selenium scripts in Java are also supported. BlazeMeter supports Selenium 4.1.3 and 4.1.4 or higher.
Whereas this guide's purpose is to advise how to add BlazeMeter functionality to your own existing script, please be aware that BlazeMeter Support does not provide scripting assistance. You can find example python scripts here.
- Setup BlazeMeter Authentication
- Configure BlazeMeter Features
- Configure BlazeMeter Connection
- (Optional) Add Test Suites & Test Cases
- (Optional) Add Pass/Fail/Broken Statuses
- (Optional) Automatically Launch Browser to Show Report
Step 1: Setup BlazeMeter Authentication
Set values for the variables base (the URL to BlazeMeter), API_KEY, and API_SECRET to ensure an authenticated connection to your BlazeMeter account. (For more information on how to find or generate your API key, see BlazeMeter API keys.)
base = 'a.blazemeter.com'
API_KEY = '{your API key}'
API_SECRET = '{your secret key}'
blazegrid_url = 'https://{}:{}@{}/api/v4/grid/wd/hub'.format(API_KEY, API_SECRET, base)
Step 2: Configure BlazeMeter Features
Specify the GUI Functional Test features you want to enable for your script. You can specify a variety of settings that enable optional features or specify certain configurations. For a full list of what can be included in this dictionary, please see our GUI Test "Desired Capabilities" Options reference guide.
For Selenium 4.1.3:
desired_capabilities = { 'browserName':'firefox', 'blazemeter.sessionName': 'My Session', 'blazemeter.videoEnabled': 'True' }
For Selenium 4.1.4 or higher:
bzm_options = { 'blazemeter.sessionName': 'My Session', 'blazemeter.videoEnabled': 'True' } browser_options = webdriver.FirefoxOptions() browser_options.browser_version = 'default' browser_options.set_capability('bzm:options', bzm_options)
Step 3: Configure BlazeMeter Connection
To ensure your script connects to and runs on BlazeMeter with all the features you specified, simply add the following line to set the blazegrid_url value:
For Selenium 4.1.3:
driver = webdriver.Remote(command_executor=blazegrid_url, desired_capabilities=desired_capabilities)
For Selenium 4.1.4 or higher:
driver = webdriver.Remote(command_executor=blazegrid_url,options=browser_options)
You can actually stop here if you like, as you've now implemented the minimum requirements to run your test on BlazeMeter -- it IS that easy! However, you may want to add some additional features to further tweak how your test will appear in BlazeMeter, in which case, continue reading below.
Step 4: Add Test Suites & Test Cases
You can optionally designate BlazeMeter test suites and test cases in your script. (If you opt not to, your test will appear in the report under "Default Test Suite" and "Default Test Case".)
Doing so is easy -- First, simply create a dictionary that assigns names to the variables testSuiteName and testSuiteCase.
args = {
'testSuiteName': 'My Test Suite',
'testCaseName': 'My Test Case Name'
}
Next, add execute_script( ) calls before and after your test steps to mark the beginning and end of the test case. In the following example, we have a single-line test that simply navigates to our demo site:
driver.execute_script("/* FLOW_MARKER test-case-start */", args)
driver.get('http://blazedemo.com')
driver.execute_script("/* FLOW_MARKER test-case-stop */", args)
All you're doing is wrapping the test suite/case around your existing test.
If you want to break your test up into multiple test suites and cases, just repeat the process with another dictionary and new start/stop execute_script() calls.
If you want to add another test case to the existing test suite, then in your next dictionary, repeat the previous testSuiteName value and add a new testCaseName value. If you want a both a new suite and a new case, add new values to both.
Step 5: Add Pass/Fail/Broken Statuses
Another optional feature you can add is pass/fail status reporting. (If you opt not to perform this step, all statuses will default to a blue "Undefined" in the test report.)
Simply expand your dictionary to include status reporting by adding a value to the "status" variable. You can also add a "message" variable to assign a custom message that will appear when you hover over the status in the report.
args = {
'testSuiteName': 'My Test Suite',
'testCaseName': 'My Test Case Name',
'status': 'passed',
'message': 'Look! It passed!'
}
In the resulting report, a "pass" status will appear in green in the report, a "fail" status will appear in red, and a "broken" status will appear in orange.
Passed/failed (past tense) in the code results in pass/fail (present tense) in the report.
Automatically Launch Browser to Show Report
If you would like your script to automatically launch your web browser to immediately show you your BlazeMeter test report, simply add the following:
webbrowser.open('https://'+ base +'/api/v4/grid/sessions/' + driver.session_id + '/redirect/to/report')
After you complete the steps above, simply finish writing the rest of your script as you would normally. That's it!