-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherShopperAddProduct.py
303 lines (232 loc) · 12.9 KB
/
WeatherShopperAddProduct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from selenium import webdriver
from page_objects import PageFactory
import conf.locators_conf as locators
import conf.product_payment_conf as product
from utils.Wrapit import Wrapit
from page_objects.Base_Page import Base_Page
from selenium.webdriver.support.ui import WebDriverWait
import re
import time
class weathershopper_tests():
product_price_element = locators.product_price_element
product_add_element = locators.product_add_element
product_category = []
cart_button = locators.click_cart
checkout_heading = locators.checkout_heading
product_moisturizers_category = ['Aloe','Almond']
product_sunscreens_category = ['SPF-50','SPF-30']
def setUp(self):
try:
self.driver=webdriver.Chrome()
self.driver.get('https://weathershopper.pythonanywhere.com')
self.driver.maximize_window()
if(self.driver.title=="The best moisturizers in the world!"):
print ("Success: The best moisturizers in the world!")
else:
print ("Failed: Title is incorrect")
except Exception as e:
print(e)
def buy_item(self):
try:
temp=self.driver.find_element_by_xpath("//span[@id='temperature']")
print("The temperature is %s"%temp.text)
print("The temperature in bytes is %s"%temp.text.encode('utf-8'))
if (temp.text.encode('utf8'))>= bytes('20','utf-8'):
self.driver.find_element_by_xpath("//button[@class='btn btn-primary' and text()='Buy sunscreens']").click()
is_screen_visible=self.driver.find_element_by_xpath("//h2[contains(text(),'Sunscreens')]")
if is_screen_visible.is_displayed():
print("You are on Buy Sunscreens page")
time.sleep(5)
###################Code for adding all items##############
# links = self.driver.find_elements_by_xpath("//button[@class='btn btn-primary' and contains(text(),'Add')]")
# click on each of those links
# for link in links:
# link.location_once_scrolled_into_view
# link.click()
# time.sleep(5)
# print("All items added")
#
#############################code from framework########################################
result_flag = False
for product in self.product_sunscreens_category:
price_product = 10000
product_elements = self.get_elements(self.product_price_element%product)
print(product_elements)
for element in product_elements:
product_price = element.text
product_price = re.findall(r'\b\d+\b', product_price)
print(product_price)
if int(product_price[0]) < price_product:
price_product = int(product_price[0])
print(price_product)
self.click_element(self.product_add_element%(product,price_product))
time.sleep(15)
self.driver.find_element_by_xpath("//button[@class='thin-text nav-link' and contains(@onclick,'goToCart')]").click()
print("All items added")
# result_flag = self.click_element(self.product_add_element%(product,price_product))
# self.conditional_write(result_flag,
#positive='Successfully added products',
#negative='Failed to add products',
#level='debug')
return result_flag
############################ end ########################################################
else:
print("You are on wrong page")
else:
self.driver.find_element_by_xpath("//button[@class='btn btn-primary' and text()='Buy moisturizers']").click()
is_screen_visible=self.driver.find_element_by_xpath("//h2[contains(text(),'Moisturizers')]")
if is_screen_visible.is_displayed():
print("You are on Buy Moisturizers page")
time.sleep(5)
#### Code for adding all items #########
#links = self.driver.find_elements_by_xpath("//button[@class='btn btn-primary' and contains(text(),'Add')]")
# click on each of those links
#for link in links:
#link.location_once_scrolled_into_view
#link.click()
#time.sleep(5)
# print("All items added")
#############################code from framework########################################
result_flag = False
for product in self.product_moisturizers_category:
price_product = 10000
product_elements = self.get_elements(self.product_price_element%product)
print(product_elements)
for element in product_elements:
product_price = element.text
product_price = re.findall(r'\b\d+\b', product_price)
print(product_price)
if int(product_price[0]) < price_product:
price_product = int(product_price[0])
print(price_product)
self.click_element(self.product_add_element%(product,price_product))
time.sleep(15)
self.driver.find_element_by_xpath("//button[@class='thin-text nav-link' and contains(@onclick,'goToCart')]").click()
print("All items added")
# result_flag = self.click_element(self.product_add_element%(product,price_product))
# self.conditional_write(result_flag,
# positive='Successfully added products',
# negative='Failed to add products',
# level='debug')
return result_flag
############################ end ########################################################
else:
print("You are on wrong page")
except Exception as e:
print(e)
def add_products(self,product_category):
"Add products to the cart"
result_flag = False
for product in product_category:
price_product = 100000
product_elements = self.get_elements(self.product_price_element%product)
for element in product_elements:
product_price = element.text
product_price = re.findall(r'\b\d+\b', product_price)
if int(product_price[0]) < price_product:
price_product = int(product_price[0])
result_flag = self.click_element(self.product_add_element%(product,price_product))
# self.conditional_write(result_flag,
# positive='Successfully added products',
# negative='Failed to add products',
# level='debug')
return result_flag
def get_elements(self,locator,msg_flag=True):
"Return a list of DOM elements that match the locator"
dom_elements = []
try:
locator = self.split_locator(locator)
dom_elements = self.driver.find_elements(*locator)
except Exception as e:
print(e)
return dom_elements
def click_element(self,locator,wait_time=3):
"Click the button supplied"
result_flag = False
try:
link = self.get_element(locator)
if link is not None:
link.click()
result_flag=True
# self.wait(wait_time)
self.driver.implicitly_wait(wait_time)
except Exception as e:
print(e)
return result_flag
def split_locator(self,locator):
"Split the locator type and locator"
result = ()
try:
result = tuple(locator.split(',',1))
except Exception as e:
# self.write("Error while parsing locator")
# self.exceptions.append("Unable to split the locator-'%s' in the conf/locators.conf file"%(locator[0],locator[1]))
print(e)
return result
def process_selected_products(self,product_category):
"Process the selected products"
result_flag = self.add_products(product_category)
result_flag &= self.click_cart()
result_flag &= self.check_redirect_cart()
return result_flag
def select_product_type(self,product_moisturizers_category,product_sunscreens_category):
"Select products type"
title = self.get_title()
title = title.decode('utf-8')
result_flag = None
if title in "Moisturizers":
result_flag = self.process_selected_products(product_moisturizers_category)
elif title in 'Sunscreens':
result_flag = self.process_selected_products(product_sunscreens_category)
return result_flag
def click_cart(self):
"Click on the Cart button"
result_flag = self.click_element(self.cart_button)
# self.conditional_write(result_flag,
# positive='Clicked on the "cart" button',
# negative='Failed to click on "cart" button',
# level='debug')
return result_flag
def check_redirect_cart(self):
"Check if we have been redirected to the redirect page"
result_flag = False
#self.driver.title = "The best sunscreens in the world!"
#remove after Arun changes the title for sunscreens
result_flag = True if self.check_element_present(self.checkout_heading) else False
# self.conditional_write(result_flag,
# positive="User is redirected to cart Page ",
# negative="User is not redirected to cart Page",
# level='debug')
if result_flag == True:
self.switch_page("cart")
return result_flag
def switch_page(self,page_name):
"Switch the underlying class to the required Page"
self.__class__ = PageFactory.PageFactory.get_page_object(page_name,base_url=self.base_url).__class__
def check_element_present(self,locator):
"This method checks if the web element is present in page or not and returns True or False accordingly"
result_flag = False
if self.get_element(locator,verbose_flag=False) is not None:
result_flag = True
return result_flag
def get_element(self,locator,verbose_flag=True):
"Return the DOM element of the path or 'None' if the element is not found "
dom_element = None
try:
locator = self.split_locator(locator)
dom_element = self.driver.find_element(*locator)
except Exception as e:
# if verbose_flag is True:
# self.write(str(e),'debug')
# self.write("Check your locator-'%s,%s' in the conf/locators.conf file" %(locator[0],locator[1]))
# self.exceptions.append("Check your locator-'%s,%s' in the conf/locators.conf file" %(locator[0],locator[1]))
print(e)
return dom_element
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
weather=weathershopper_tests()
weather.setUp()
weather.buy_item()
# weather.add_products()
weather.tearDown()