Home > database >  Selenium/Python framework - import issue
Selenium/Python framework - import issue

Time:12-31

When I run my selenium python tests via right click run in PyCharm, all is good. However, when I try to run with command line (pytest), I get error. Here is my folder structure:

[projectname]/
├── Driver
    ├── Driver.py
├── Tests
    ├── TestFolder
        ├── TestName.py

Driver.py file looks like this:

from selenium import webdriver

Instance = None


def Initialize():
   global Instance
   Instance = webdriver.Chrome()
   Instance.implicitly_wait(2)
   return Instance


def QuitDriver():
   global Instance
   Instance.quit()

TestName.py looks like this:

import unittest
from Driver import Driver
from Tests.Transactions.HelperFunctions import *


class StreamsTest(unittest.TestCase):

    @classmethod
    def setUp(cls):
        Driver.Initialize()
    
    def testSameDayEverySecond(self):
        ConnectSenderWallet()
        AppPage.HandleDevAmountsTitleAndAddress()
        HandleCreateAndAssert()
    
    @classmethod
    def tearDown(cls):
        Driver.QuitDriver()

And when I run pytest -v -s Tests/TestFolder/TestName.py, I get following errors in my console:

    ImportError while importing test module '/Users/dusandev/Desktop/w-test/Tests/TestFolder/TestName.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../miniconda3/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
Tests/TestFolder/TestName.py:2: in <module>
    from Driver import Driver
E   ModuleNotFoundError: No module named 'Driver'

Thank you for your help!

CodePudding user response:

Python is trying to load the Driver class from Driver.py, however, you don't have one. You may want to do one of the following:

  • Create a Driver class in Driver.py
  • Replace from Driver import Driver with from Driver import *
  • Replace from Driver import Driver with import Driver
  • Related