Selenium: I want to choose which browser to use with invocation in Command Prompt
Currently, I have a .ini file called testcase.ini which looks something like this:
[TEST]
DRIVER_PATH = C:\Python\
BROWSER = CHROME
; BROWSER = EDGE
; BROWSER = FIREFOX
CHROME_PATH = C:\Program Files\Google\Chrome\Application\chrome.exe
; EDGE_PATH = C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
; FIREFOX_PATH = C:\Program Files\Mozilla Firefox\firefox.exe
To run my automated tests, I open a command prompt in my Test Plan folder and enter a command like this:
python TestPlan_LoginTest.py
Instead of going in to the testcase config file and commenting out which browsers I don't wish to use when running my automated tests, I'd like to be able to choose the browser when I invoke the test in the command prompt, i.e. something like this:
python TestPlan_LoginTest.py Firefox
or
python TestPlan_LoginTest.py Edge
If I don't include a browser in the invocation, it defaults to Chrome.
Is this possible with unittest? What changes would I need to make and where?
Solution 1:
If you're using a pytest
plugin called SeleniumBase, you can set the browser from the command-line. (It defaults to "chrome" if not specified.) Example:
pytest test_demo_site.py --edge
pytest my_first_test.py --firefox
Or if you prefer to use your own framework, you can find the necessary code for setting pytest
command-line options here in SeleniumBase: pytest_plugin.py
That link shows you about using pytest_addoption(parser):
as well as code such as:
parser.addoption(
"--edge",
action="store_true",
dest="use_edge",
default=False""",
)
which lets you customize command-line options for use in tests.