Run Python script unit testing - Selenium - python

I am a junior developer and totally new to Python and Selenium. I have to learn unit testing. They shared a small repository as an example. As required I installed Python and Selenium, but I don't know how to run. I have looked some tutorials, they just installed Python and Selenium, and added chromedriver, that's it. I have completed same steps, added PYTHONPATH and everything. But still getting errors.
My PYTHONPATH variable C:\Users\yanke\altrium-systemtests\altsys
Readme.md
System tests for Altrium frontend.
Install Python
export PYTHONPATH="."
python altsys/tests.py
tests.py
""Test altrium portal"""
import unittest
from selenium import webdriver
from altsys.selenium_helpers import sreenshot_on_fail
from altsys.page import AltriumPage
#sreenshot_on_fail()
class LoginTest(unittest.TestCase):
"""Test altrium portal login
Args:
unittest ([type]): [description]
"""
def setUp(self) -> None:
self.driver = webdriver.Chrome()
# self.driver = webdriver.Firefox()
self.page = AltriumPage(self.driver, 'https://alpha.altrium.sg/login')
def test_login(self):
"""Test login
"""
self.page.login(
'tdrobiszewski+st#gmail.com',
'TestPassword123!'
)
# SLACK_TOKEN = 'xoxb-1178000328886-3062418273846-EIxTJ17QSbELkwB7WnbfHuFr'
if __name__ == '__main__':
unittest.main()
When I run I am getting these errors:
Error
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\unittest\loader.py", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: tests
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\unittest\loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "C:\Users\yanke\altrium-systemtests\altsys\tests.py", line 5, in <module>
from altsys.selenium_helpers import sreenshot_on_fail
File "C:\Users\yanke\altrium-systemtests\altsys\selenium_helpers.py", line 3, in <module>
from slack_sdk import WebClient
ModuleNotFoundError: No module named 'slack_sdk'
Ran 1 test in 0.006s
FAILED (errors=1)
Process finished with exit code 1
On Terminal
Traceback (most recent call last):
File "C:\Users\yanke\altrium-systemtests\altsys\tests.py", line 5, in <module>
from altsys.selenium_helpers import sreenshot_on_fail
ModuleNotFoundError: No module named 'altsys'

Related

Python CanOpen canGetNumberOfChannels not defined | kvaser canlib is unavailable

i tried to run my Python Program on my Linux maschine but when i try to run it it is having a hard time finding all the nessasary modules needed for it to work.
i installed CanOpen to my project
import threading
import sys
sys.path.append('venv/Lib/site-packages')
sys.path.append('venv/Lib/site-packages/can/interfaces/kvaser')
sys.path.append('/test/Python39/Lib')
import canopen
from canopen.network import MessageListener, NodeScanner
from canopen.nmt import NmtMaster
from canopen.sync import SyncProducer
from canopen.timestamp import TimeProducer
if __name__ == '__main__':
network = canopen.Network()
network.connect(bustype='kvaser', channel=0, bitrate=250000)
it is showing me following error in the Terminal:
Kvaser canlib is unavailable.
Traceback (most recent call last):
File "/test/Can_T1/main.py", line 60, in <module>
network.connect(bustype='kvaser', channel=0, bitrate=250000)
File "/test/Can_T1/venv/Lib/site-packages/canopen/network.py", line 112, in connect
self.bus = can.interface.Bus(*args, **kwargs)
File "/test/Can_T1/venv/Lib/site-packages/can/interface.py", line 120, in __new__
bus = cls(channel, *args, **kwargs)
File "/test/Can_T1/venv/Lib/site-packages/can/interfaces/kvaser/canlib.py", line 437, in __init__
canGetNumberOfChannels(ctypes.byref(num_channels))
NameError: name 'canGetNumberOfChannels' is not defined
Is there any way to define 'canGetNumberOfChannels'?
You have to install the canlib on your machine as well. Right now, you just have installed the SDK. If you are running on linux you have to install libcanlib.so on your machine. Take a look at the part that is related to your operating system on this link: https://pypi.org/project/canlib/

Flask Error When I run python program in pycharme

i have this error after run:
Traceback (most recent call last):
File "/home/user/PycharmProjects/third_project/flask_test.py", line 1, in <module>
from flask import Flask
File "/home/user/PycharmProjects/third_project/flask.py", line 1, in <module>
from flask import Fl
ImportError: cannot import name 'Fl' from partially initialized module 'flask' (most likely due to a circular import) (/home/user/PycharmProjects/third_project/flask.py)
Process finished with exit code 1
************************ the code :flask_test.py**************************
from flask import Flask
skills_app = Flask(__name__)
#skills_app.route("/")
def hompage():
return "hello flask"
#skills_app.route("/about")
def about():
return "hello flask/about"
if __name__=="__main__":
skills_app.run(debug=True)

Importing module item of subpackage from another subpackage

I have this project structure:
root_package/
root_package/packA/
root_package/packA/__init__.py (empty)
root_package/packA/moduleA.py
root_package/packB/__init__.py (empty)
root_package/packB/moduleB.py
root_package/rootModule.py
In the rootModule.py I have from packA.moduleA import ModuleAClass.
At the packA.moduleA.py I have this from root_package.packB.moduleB import ModuleBItem.
When running rootModule either via PyCharm or the terminal with python ./rootModule.py I am getting this error:
Was this the right way of importing?
Traceback (most recent call last):
File "/project_dir/rootPackage/rootModule.py", line 7, in <module>
from packA.moduleA import ModuleAClass
File "/project_dir/rootPackage/packA/moduleA.py", line 8, in <module>
from rootPackage.packB.moduleB import module_b_method
File "/project_dir/rootPackage/rootModule.py", line 7, in <module>
from packA.wavelet_compression import WaveletCompression
ImportError: cannot import name WaveletCompression
How to solve this?
Update 1
I've added a test file at the project_folder (not the root_package folder).
So the current directory structure is this:
project_folder/
project_folder/root_package/
project_folder/root_package/packA/
project_folder/root_package/packA/__init__.py (empty)
project_folder/root_package/packA/moduleA.py
project_folder/root_package/packB/__init__.py (empty)
project_folder/root_package/packB/moduleB.py
project_folder/root_package/rootModule.py
project_folder/test_rootModule.py
I haven't made the project_folder a package (no __init__.py file) since, the test_rootModule is simply a script to help me run the experiments.
So, in root_package/packA/moduleA.py, after changing the from root_package.packB.moduleB import ModuleBitem, to from packB.moduleB import ModuleBitem, as the answer suggests, it works.
But now there are two problems:
1. PyCharm doesn't agree with the change:
I cannot run my experiments from the project_folder/test_rootModule.py script.
I got this error:
Traceback (most recent call last):
File "project_folder/test_rootModule.py", line 8, in
from root_package.rootModule import rootModuleClass
File "project_folder/root_package/rootModule.py", line 7, in
from packA.moduleA import ModuleAClass
File "project_folder/root_package/packA/moduleA.py", line 8, in
from packB.moduleB import module_b_item
ImportError: No module named packB.moduleB
I cannot seem to get the 2nd Traceback to look like a code segment.
Update 2
What solved the problem was going to the Project: project_name > Project Structure dialog in PyCharm, selecting the root_package and then setting it as a Sources folder.
Now, I can run via the IDE both the rootModule and the test_rootModule.
Although, I cannot get to run the test_rootModule from the terminal.
The test_rootModule has these imports:
from root_package.rootModule import RootModuleClass
from root_package.packB.moduleB import module_b_item
I am at the project_folder dir, and run python ./test_rootModule.py and get this error:
Traceback (most recent call last):
File "./test_rootModule.py", line 8, in <module>
from root_package.rootModule import RootModuleClass
File "project_folder/root_package/rootModule.py", line 7, in <module>
from packA.moduleA import ModuleAClass
File "project_folder/root_package/packA/moduleA.py", line 8, in <module>
from packB.moduleB import module_b_item
ImportError: No module named packB.moduleB
If you are running all your code from within this path:
project_folder
Then you should ensure that all your modules that reside in root_package are referenced by that first. So for example:
from root_package.modA import foo

not able to create a web driver instance in python

I am trying to create a web driver instance in the python with the following code:
from robot.libraries.BuiltIn import BuiltIn
import Selenium2Library
from Selenium2Library import Selenium2Library
def get_webdriver_instance():
s2l = BuiltIn().get_library_instance("Selenium2Library")
return s2l._current_browser()
but at BuiltIn().get_library_instance("Selenium2Library") I am getting the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/robot/libraries/BuiltIn.py", line 2922, in get_library_instance
return self._namespace.get_library_instance(name)
File "/usr/lib/python2.6/site-packages/robot/libraries/BuiltIn.py", line 70, in _namespace
return self._context.namespace
File "/usr/lib/python2.6/site-packages/robot/libraries/BuiltIn.py", line 65, in _context
raise RobotNotRunningError('Cannot access execution context')
robot.libraries.BuiltIn.RobotNotRunningError: Cannot access execution context
Could someone please help me in resolving this error
The error message is telling you that you can't use methods of the BuiltIn library unless you are actually running a test (via pybot, jybot, etc). You can't call BuiltIn().get_library_instance('Selenium2Library') in a standalone python script.
_current_browser is only return the current browser
If you want to use Selenium2Library in python than you can do the next
from Selenium2Library import Selenium2Library
sl = Selenium2Library()
sl.open_browser('firefox')

Python makes lib error when making FireFox Sync 1.5

I've tried to make my own server to new sync service in FireFox.
I used the following tutorial: https://docs.services.mozilla.com/howtos/run-sync-1.5.html .
When I wanted to check it by using make test I got the following error:
Traceback (most recent call last):
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/nose/loader.py", line 403, in loadTestsFromName
module = resolve_name(addr.module)
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/nose/util.py", line 311, in resolve_name
module = __import__('.'.join(parts_copy))
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/syncstorage/__init__.py", line 5, in <module>
import mozsvc.config
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/mozsvc/config.py", line 10, in <module>
from konfig import Config, SettingsDict
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/konfig/__init__.py", line 11, in <module>
from configparser import ConfigParser, ExtendedInterpolation
ImportError: cannot import name ExtendedInterpolation
What's happened?
configparser.ExtendedInterpolation does not exist in Python 2.
You need to use Python 3.

Categories