-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherShopperTwoItems.py
253 lines (189 loc) · 9.72 KB
/
WeatherShopperTwoItems.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
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 from framework########################################
result_flag = False
for num in range(1,2):
for product in self.product_sunscreens_category:
price_product = 100000
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))
num =+ num
print(num)
time.sleep(15)
self.driver.find_element_by_xpath("//button[@class='thin-text nav-link' and contains(@onclick,'goToCart')]").click()
print("All items added")
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 from framework########################################
result_flag = False
for num in range(1,2):
for product in self.product_moisturizers_category:
price_product = 100000
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))
num =+ num
print(num)
time.sleep(15)
self.driver.find_element_by_xpath("//button[@class='thin-text nav-link' and contains(@onclick,'goToCart')]").click()
print("All items added")
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))
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.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:
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)
return result_flag
def check_redirect_cart(self):
"Check if we have been redirected to the redirect page"
result_flag = False
result_flag = True if self.check_element_present(self.checkout_heading) else False
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:
print(e)
return dom_element
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
weather=weathershopper_tests()
weather.setUp()
weather.buy_item()
weather.tearDown()