0

I am currently learning about Selenium with Python binding. I am currently trying to design a test where I could do run a test with different browsers by passing the browser name as a parameter through the terminal.

The command that I use to run is: (venv) cp@cp fetchwebsite % pytest -s -v -q TestCases/test_login.py --browser="chrome"

it keeps returning this error for me

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --browser=chrome
  inifile: None
  rootdir: /Users/cp/PycharmProjects/fetchwebsite

Below is my conftest.py

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


@pytest.fixture()
def setup(browser):
    options = Options()
    options.add_experimental_option('detach', True)
    if browser == "chrome":
        driver = webdriver.Chrome(options=options)
    elif browser == "firefox":
        driver = webdriver.Firefox(options=options)
    return driver

def pytest_addoption(parser):
    parser.addoption("--browser", default="chrome", action="store")


@pytest.fixture()
def browser(request):
    return request.config.getoption("--browser")

this is my test_login.py

import pytest
from PageObjects.login_page import LoginPage
from PageObjects.myStuff_page import MyStuffPage
from PageObjects.pin_page import PINPage
from selenium import webdriver
from Utilities.property_reader import ReadConfig
from Utilities.log_generator import LogGenerator
from Configurations.conftest import setup


class Test_LoginByActivationCode:
    # getting all the configs needed for this test
    loginURL = ReadConfig.getURL()
    validPIN = ReadConfig.getValidPIN()
    validCode = ReadConfig.getValidCode()
    log = LogGenerator.logGenerator()

    def test_pageTitle(self, setup):
        self.driver = setup
        self.driver.get(self.loginURL)
        self.driver.implicitly_wait(5)

        self.log.info("LOGIN TEST 1: Login By Valid Activation Code and PIN")
        self.log.info(":: Verifying Page Title")
        page_title = self.driver.title
        if page_title == "Fetch Account":
            self.log.info(":: > Page Title is verified")
            assert True
            self.driver.close()
        else:
            self.log.error(":: > Page Title is NOT verified")
            assert False
            self.driver.close()


    def test_loginCorrectActivationCodeCorrectPIN(self, setup):
        self.driver = setup
        self.driver.get(self.loginURL)
        self.driver.implicitly_wait(5)

        lp = LoginPage(self.driver)
        pp = PINPage(self.driver)
        msp = MyStuffPage(self.driver)

        lp.closeCookieBanner()
        self.log.info(":: Finding Cookie Banner and Closing it")
        lp.setValidActivationCode(self.validCode)
        self.log.info(":: Entering a valid activation code: " + self.validCode)
        lp.clickSignInButton()
        self.log.info(":: Clicking on Sign In Button")
        self.driver.implicitly_wait(5)

        pp.setValidPIN(self.validPIN)
        self.log.info(":: Entering a valid PIN: " + self.validPIN)
        self.driver.implicitly_wait(5)

        # locate My Stuff heading and print it
        self.log.info(":: Finding and Verifying My Stuff Page Heading")
        heading = msp.findMyStuffPageHeading()
        if heading == "My Stuff":
            self.log.info(":: > My Stuff Heading is verified")
            self.log.info(":: > Login is Successful")
            assert True
            self.driver.close()
        else:
            self.log.error(":: > My Stuff Heading is NOT verified: " + heading)
            self.log.error(":: > Login is NOT Successful")
            assert False
            self.driver.close()


Directories structure inside my Project Project directories

I tried to search for answers for two weeks now but I am stucked. If anyone could help it would be great.

My goal is when I pass 'chrome' it would launch the chrome browser and when I pass 'firefox' it would launch the firefox browser.

Thank you

CP0901
  • 1
  • 2
  • 1
    You don't show how you use the fixture in your test - please add that code. As an aside, it's not a good idea to name a fixture `setup` that returns a driver - naming it `driver` would make the tests better readable. – MrBean Bremen Aug 06 '23 at 17:44
  • Hi @MrBeanBremen, I just added my test_login.py file. I haven't changed any naming in this case. – CP0901 Aug 08 '23 at 01:05
  • Your code looks fine at first glance. Actually, I missed that "unrecognized argument" - are you sure that your `conftest.py` is in the correct place and found? – MrBean Bremen Aug 08 '23 at 06:15
  • I have added a screenshot of my directories structure @MrBeanBremen honestly not sure if it reads or not but it does not really complain about it – CP0901 Aug 09 '23 at 09:37
  • This explains it - your `conftest.py` is in a separate directory parallel to the tests and will not be found. It has to be in the project root, in the test directory, or in some directory between. The same is true for `pytest.ini`. – MrBean Bremen Aug 09 '23 at 10:40
  • Hi @MrBeanBremen, I moved my conftest.py to root and now my test is doing what I want :) Thank you so much Any suggestion a good documentation on pytest? I ve bee searching for answer but nada. Thanks again ! Also, silly question. How do I mark this question as answered? – CP0901 Aug 11 '23 at 10:38
  • The pytest documentation is meanwhile quite good, though this specific question may be a bit hidden. [Here](https://docs.pytest.org/en/latest/how-to/fixtures.html#overriding-fixtures-on-various-levels) is probably the best place to look. As for the answer - I didn't answer it, just commented. I'm quite sure this has been asked before, just didn't bother to look for a duplicate yet... probably [this one](https://stackoverflow.com/questions/34466027/in-pytest-what-is-the-use-of-conftest-py-files). – MrBean Bremen Aug 11 '23 at 11:57

0 Answers0