why python is throwing attribute attribute error - python-3.x

I have the following unit test written in python,When i try to run this code, i am getting an attribute error in the console that
AttributeError: 'testLogin' object has no attribute 'lp' I am very new to python and i would like to get some insight on this attribute issue
Is there any issues that needs to be fixed. Your help us highly recommended.
from utilities.teststatus import TestStatus as ts
import unittest
import pytest
#pytest.mark.usefixtures("oneTimeSetup", "setUp")
class testLogin(unittest.TestCase):
#pytest.fixture(autouse=True)
def classSetup(self,oneTimeSetup):
self.lp = LoginPage(self.driver)
#pytest.mark.order1
def test_validLogin(self):
self.lp.login("test#email.com", "abcabc")
userIcon = self.lp.verifyLoginSuccessful()
title = self.lp.verifyPageTitle()
ts.markFinal(title,"Login not successful")
assert userIcon == True
When i try to run this unittest, i am getting the error as AttributeError: 'testLogin' object has no attribute 'lp'
Can some one please help to fix it ?

Related

AttributeError: 'bytes' object has no attribute 'hexdigest'

I wrote the following code but the problem is that I recieved an error (AttributeError: 'bytes' object has no attribute 'hexdigest')
the error syntax doesn't work
import requests
import hashlib
def request_api_data (query_char):
url = 'https://api.pwnedpasswords.com/range/'+ query_char
res = requests.get(url)
if res.status_code != 200:
print('it is an error')
#raise RuntimeError(f'Error fetching: {res.status_code}, check api and try again')
return res
request_api_data('123')
def pwned_api_check(password):
sha1password= hashlib.sha1(password.encode('utf-8').hexdigest().upper())
print (sha1password)
#return sha1password
pwned_api_check('123')
Why does this error occur and how do I fix it??
You need to add a parenthesis after hashlib.sha1(password.encode('utf-8'), so hexdigest().upper() is called on it.
The following code works for me:
hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
I was taking the same class as you and got the same error. The parenthesis are in the wrong place.
sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()

Pytest mock using decorator to mock function containing context manager return value not passed

So I am struggling with mocking this function due to the context manager.
Function under test
import fitz
def with_test_func(next_pdfs_path):
text = ''
with fitz.open(next_pdfs_path) as doc:
text = doc
return text
Test code
#mock.patch("content_production.fitz.open.__enter__", return_value='value_out')
def test_with_test_func(mock_fitz_open):
assert cp.with_test_func('value_in') == 'value_out'
Error
RuntimeError: cannot open value_in: No such file or directory
I have tested this without the context manager and it works. So how would I fix this? thanks
Edit
So as suggested by #MrBean I tried this
#mock.patch("content_production.fitz.open.return_value.__enter__", return_value='value_out')
def test_with_test_func(mock_fitz_open):
assert cp.with_test_func('value_in') == 'value_out'
It gives me this error
thing = <class 'fitz.fitz.Document'>, comp = 'return_value', import_path = 'content_production.fitz.open.return_value'
def _dot_lookup(thing, comp, import_path):
try:
return getattr(thing, comp)
except AttributeError:
> __import__(import_path)
E ModuleNotFoundError: No module named 'content_production.fitz'; 'content_production' is not a package
The problem is that return_value is a property of the mock, not of the patched function, so you cannot put it into the patch argument string. Instead, you have to set the return value on the mock for the open method:
#mock.patch("content_production.fitz.open")
def test_with_test_func(mock_fitz_open):
mock_fitz_open.return_value.__enter__.return_value = 'value_out'
assert cp.with_test_func('value_in') == 'value_out'

module 'npyscreen' has no attribute 'wrapper_basic'

When I try to use a function of the npyscreen module it returns the error that the attribute can not be found. I do not understand why. Does anyone have an idea what is going wrong?
import npyscreen
def simple_function(*args):
print("nothing")
if (__name__ == "__main__"):
npyscreen.wrapper_basic(simple_function)
Error message:
AttributeError: module 'npyscreen' has no attribute 'wrapper_basic'
I named my python file npyscreen.py which is the same as the module its name. FIXED by renaming it!

TypeError: 'numpy.ndarray' object is not callable when import a function

Hi I am getting the following error.
TypeError: 'numpy.ndarray' object is not callable
I wrote a function module by myself,like this:
from numpy import *
import operator
def creatDataset() :
group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return group,labels
then,I want to use this function in Microsoft's command window ,I've written some code, as follows:
import KNN
group,labels=KNN.creatDataset()
group()
when I input the code "group()",the error will appear.It's the first time that i describe the question and ask for help, maybe the description is not clear ,,please forgive me.
Since "group" is a numpy.array, you cannot call it like a function.
So "group()" will not work.
I assume, you want to see it's values, so you would have to use something like
"print(group)".

classes in Python: AttributeError

I`m writing simple scripts for test automation using Selenium WebDriver in Python, but the issue relates to Python, not to Selenium.
There two classes FindByXPATH_1(base) & FindByXPATH_2(derived). I want to call an attribute "driver" from the base class in a method of FindByXPATH_2, but when I ran the code the AttributeError shows up: "type object 'FindByXPATH_1' has no attribute 'driver'"
Here is the code:
class FindByXPATH_1():
def __init__(self):
self.driver_location = '/usr/local/bin/chromedriver'
self.driver = webdriver.Chrome(self.driver_location)
self.driver.get('https://letskodeit.teachable.com/p/practice')
from basics.xpath_1 import FindByXPATH_1
import basics #the classes are in two different python files
class FindByXpath_2(FindByXPATH_1):
def __init__(self):
FindByXPATH_1.__init__(self)
def find_by_starts_with(self):
starting_with = FindByXPATH_1.driver.find_elements(By. XPATH,
'//div[#class="view-school"]//h3[starts-with(#)class, "subtitle"]')
print(len(starting_with))
test = FindByXPATH_2()
test.find_by_starts_with()
After running the code I get a message "AttributeError: type object 'FindByXPATH_1' has no attribute 'driver'"
How can I call that attribute?
In this line here:
starting_with = FindByXPATH_1.driver.find_elements(By. XPATH,
'//div[#class="view-school"]//h3[starts-with(#)class, "subtitle"]')
You should be calling self.driver.find_elements otherwise you are trying to access a class variable of FindByXPATH_1 and not the instance variable driver

Resources