Reputation: 153
I'm trying to generate random title for an auction and then use it outside the method.
add_consignment.py:
class AddConsignmentPage(BasePage):
def __init__(self, driver):
super(AddConsignmentPage, self).__init__(driver, self._title)
self._title_uuid = get_random_uuid(7)
inst = AddConsignmentPage(AddConsignmentPage)
and I want to use the same _title_uuid to view added consignment (type its title into search field)
view_consignments.py
from pages.add_consignment import inst
class ViewConsignmentsPage(BasePage):
_title_uuid = inst._title_uuid
def check_added_consignment(self):
self.send_keys(self._title_uuid, self._auction_search)
In this scenario the title is generated two times so title in added consignment is different than title in search field
So how to pass the value of _title_uuid from AddConsignmentPage to ViewConsignmentsPage? I want it to be the same in two methods, but different for every consignment(test case)
How to get it generated once for every consignment?
Upvotes: 1
Views: 106
Reputation: 153
I've fixed the problem by adding ViewConsignmentsPage._title_uuid = self._title_uuid to init method
add_consignment.py:
from pages.view_consignments import ViewConsignmentsPage
class AddConsignmentPage(BasePage):
def __init__(self, driver):
super(AddConsignmentPage, self).__init__(driver, self._title)
self._title_uuid = get_random_uuid(7)
ViewConsignmentsPage._title_uuid = self._title_uuid
Upvotes: 0
Reputation: 11368
That's because _title_uuid
is a class variable and not an instance variable: It's only initialized once. But if you initialize it in the constructor, it should work.
See also Static class variables in Python
E.g.,
import random
class Foo:
num1 = random.randint(1,10)
def __init__(self):
self.num2 = random.randint(1,10)
for n in range(10):
foo = Foo()
print(foo.num1, foo.num2)
Running the above gives:
(7, 2)
(7, 6)
(7, 6)
(7, 5)
(7, 7)
(7, 1)
(7, 2)
(7, 3)
(7, 7)
(7, 7)
You can also do print(Foo.num1)
here, if that clarifies anything, but not print(Foo.num2)
because that only exists for instantiated objects.
As you can see, num1
is initialized once, while num2
is initialized for every instantiation of the object.
In you case, you can probably just do:
class AddConsignmentPage(BasePage):
def __init__(self):
super(AddConsignmentPage, self).__init__() # initializes BasePage
self._title_uuid = get_random_uuid(7)
# ...
Upvotes: 1
Reputation: 2249
I think you should define the _title_uuid inside an __init__
method since the value on the class will be changed each time.
In your case it could be:
def __init__(self, *args, **kw):
super(AddConsignmentPage, self).__init__(*args, **kw)
self._title_uuid = get_random_uuid(7)
Upvotes: 0