I am new to Sellinium/Python and running few practice exercises. I want to run this program with internet Explorer, so at command prompt, I have given below command:
py.test -s -v mainTest.py --browser IE
When I run above command, it is showing error as:
ERROR: usage: py.test [options] [file_or_dir] [file_or_dir] [...] py.test: error: unrecognized arguments: --browser IE inifile: None`` rootdir: C:\Users\Radha.Maravajhala\PycharmProjects\RadhaSelenium\Tests
It is not identifying --browser arguement.
How can i run my program with a specific browser as an arguement in py.test command. Please help.
My Code:
mainTest.py
import os
import pytest
import unittest
from executionEngine.DriverScript import driverScript
from Utilities.Constants import Constants
from selenium import webdriver
class mainTest(driverScript):
def main(self):
print("Main Test started...")
print("Selenium webdriver Version: %s" % (webdriver.__version__))
driver = driverScript('IE')
driver.getbrowserInstance()
m = mainTest()
m.main()
DriverScript.py
import os
import pytest
import unittest
from pip._internal.configuration import Configuration
from selenium import webdriver
from selenium.webdriver import Ie
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.ie.options import Options
from selenium.webdriver.common.keys import Keys
#import selenium.webdriver
from Utilities import Constants
class driverScript():
def __init__(self,browser=None):
if browser is None:
browser = {}
else:
self.browser = browser
print(self.browser)
self.constants = Constants.Constants()
# Method to invoke browser
def getbrowserInstance(self):
# create a new IE session
print("Browser invoke started")
if (self.browser=='IE'):
capabilities = DesiredCapabilities.INTERNETEXPLORER
print(capabilities["platform"])
print(capabilities["browserName"])
driver_location = self.constants.path_ie_driver
os.environ["webdriver.ie.driver"] = driver_location
ie_options = Options()
ie_options.ignore_protected_mode_settings = True
driver = webdriver.Ie(driver_location, options=ie_options)
print("Browser is Invoked")
driver.get("http://www.amazon.co.uk")
driver.quit()
elif (self.browser=='Chrome'):
capabilities = DesiredCapabilities.CHROME
print(capabilities["platform"])
print(capabilities["browserName"])
driver_location = self.constants.path_chrome_driver
os.environ["webdriver.chrome.driver"] = driver_location
driver = webdriver.Chrome(driver_location)
print("Chrome Browser is Invoked")
driver.get("http://www.amazon.co.uk")
driver.quit()
Constants.py:
class Constants():
#Driver Locations
path_ie_driver = "C:\\Selenium\\Drivers\\IEDriverServer.exe"
path_chrome_driver = "C:\\Selenium\\Drivers\\chromedriver.exe"
confest.py
class Constants():
import pytest
from executionEngine.DriverScript import driverScript
# Send request variable to the fixture
# Browser value will be set from request.config.getoption (from command
# prompt)
@pytest.yield_fixture(scope="class")
def invoke_browser(request,browser):
wdf = driverScript.getbrowserInstance(browser)
driver = wdf.getbrowserInstance()
# Set class attribute and assign the variable
if request.cls is not None:
request.cls.driver = driver
yield driver
driver.quit()
Create 2 parsers to get value from command prompt
def pytest_addoption(parser):
parser.addoption("--browser")
Return the argument value
@pytest.fixture(scope="session")
def browser(request):
return request.config.getoption("--browser")