-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
1144 lines (911 loc) · 47.8 KB
/
test.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import unittest
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import sqlite3
import os
import sys
current_directory = os.getcwd()
db_path=current_directory+"\\apps\\db.sqlite3"
image =current_directory+"\\Burger.PNG"
#Helper function that clean the code from the
def delete_test_user(path):
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('''SELECT id FROM user WHERE username = 'test_user';''')
result = c.fetchone()
if result is not None:
user_id = result[0]
query = "DELETE FROM user_profile WHERE id = "+str(user_id)+";"
c.execute(query)
c.execute('''DELETE FROM user WHERE username = 'test_user';''')
conn.commit()
conn.close()
def delete_test_follows(path):
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('''SELECT id FROM user WHERE username = 'followtester';''')
result = c.fetchone()
if result is not None:
user_id = result[0]
tables = ["comment", "rating", "user_profile", "shopping_list", "user_activity"]
for table in tables:
query = "DELETE FROM "+table+" WHERE user_id = "+str(user_id)+";"
c.execute(query)
c.execute("DELETE FROM follow WHERE follower_id = "+str(user_id)+";")
c.execute("DELETE FROM follow WHERE followed_id = "+str(user_id)+";")
conn.commit()
conn.close()
def delete_test_comment(path):
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('''DELETE FROM comment WHERE user_id = 1 AND content = 'Nice Meal';''')
conn.commit()
conn.close()
def delete_test_rating(path):
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('''DELETE FROM rating WHERE user_id = 1;''')
conn.commit()
conn.close()
def delete_recipe(path):
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('''SELECT id FROM recipe WHERE name = 'Beef Burger';''')
result = c.fetchone()
if result is not None:
user_id = result[0]
tables = ["instruction", "ingredient", "rating", "comment", "user_activity"]
for table in tables:
query = "DELETE FROM "+table+" WHERE recipe_id = "+str(user_id)+";"
c.execute(query)
c.execute('''DELETE FROM recipe WHERE name = 'Beef Burger';''')
conn.commit()
conn.close()
class WebAppTests(unittest.TestCase):
def setUp(self):
if sys.platform.startswith('win'):
self.driver = webdriver.Chrome(service=Service('Yumfinity\\chromedriver_win32\\chromedriver.exe'))
else:
self.driver = webdriver.Chrome(service=Service('Yumfinity\\linux_chromedriver\\chromedriver.exe'))
self.driver.implicitly_wait(2)
def tearDown(self):
self.driver.quit()
def test_login_success(self):
driver = self.driver
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
driver.implicitly_wait(10) # Wait for up to 10 seconds
home_title = driver.find_element(By.XPATH,"//h3[contains(text(),'Your online cookbook')]")
assert home_title.is_displayed()
def test_logout(self):
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
driver.implicitly_wait(10) # Wait for up to 10 seconds
#find logout button and click it
shut_down = driver.find_element(By.XPATH,'//*[@id="main-wrapper"]/aside/div[2]/div/div/a/i')
shut_down.click()
time.sleep(1)
username_field = driver.find_element(By.NAME,"username")
assert username_field.is_displayed()
def test_registration_success(self):
delete_test_user(db_path)
driver = self.driver
driver.get("http://localhost:5000")
#look for "register" button
register_link = driver.find_element(By.TAG_NAME, "a")
register_link.click()
#fill user info
username = driver.find_element(By.NAME, "username")
username.send_keys("test_user")
email = driver.find_element(By.NAME, "email")
email.send_keys("[email protected]")
password = driver.find_element(By.NAME, "password")
password.send_keys("test_password")
#click register button
register_button = driver.find_element(By.NAME,"register")
register_button.click()
# Wait for page to load
time.sleep(2)
# Check if registration is successful
success_message = driver.find_element(By.XPATH, "//*[contains(text(), 'User created successfully.')]")
self.assertTrue(success_message.is_displayed())
def test_registration_fail(self):
driver = self.driver
driver.get("http://localhost:5000")
#look for "register" button
register_link = driver.find_element(By.TAG_NAME, "a")
register_link.click()
#fill user info
username = driver.find_element(By.NAME, "username")
username.send_keys("Khaled Elbastawisy")
email = driver.find_element(By.NAME, "email")
email.send_keys("[email protected]")
password = driver.find_element(By.NAME, "password")
password.send_keys("Dodobasta5")
#click register button
register_button = driver.find_element(By.NAME,"register")
register_button.click()
# Wait for page to load
time.sleep(2)
# Check if registration is successful
fail_msg = driver.find_element(By.XPATH, "//*[contains(text(), 'already registered')]")
self.assertTrue(fail_msg.is_displayed())
def test_login_fail(self):
self.driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = self.driver.find_element(By.NAME,"username")
password_field = self.driver.find_element(By.NAME,"password")
username_field.send_keys("dfgdf")
password_field.send_keys("dfgddhg")
# Submit the login form
login_button = self.driver.find_element(By.NAME,"login")
login_button.click()
time.sleep(2)
# Wait for the error to show
login_fail = WebDriverWait(self.driver, 5).until(
EC.visibility_of_element_located((By.XPATH, "//span[contains(text(),'Wrong user or password')]"))
)
assert login_fail.is_displayed()
def test_recipe_of_the_day(self):
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
# Wait for up to 10 seconds
view_recipe_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "View Recipe Details")))
#scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
view_recipe_button.click()
#scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
#look for comments section
submit_comment_button = driver.find_element(By.NAME, "comment")
assert submit_comment_button.is_displayed() # Wait for up to 10 seconds
def test_cusuine_search(self):
self.driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = self.driver.find_element(By.NAME,"username")
password_field = self.driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = self.driver.find_element(By.NAME,"login")
login_button.click()
time.sleep(2)
# Wait for the page to load after login (e.g., check for an element on the home page)
home_title = WebDriverWait(self.driver, 3).until(
EC.visibility_of_element_located((By.XPATH, "//h3[contains(text(),'Your online cookbook')]"))
)
# finding the browse all recipes button
browse_button = self.driver.find_element(By.XPATH, "//button[contains(@class, 'btn-primary') and contains(., 'Browse All Recipes')]")
browse_button.click()
dropdown = self.driver.find_element(By.NAME, "cuisine")
dropdown.click()
# Wait for the options to be visible, if necessary
desired_option_value = "egyptian"
# Locate the option by value
option_by_value = dropdown.find_element(By.CSS_SELECTOR, f"option[value='{desired_option_value}']")
option_by_value.click()
WebDriverWait(self.driver, 3)
#locate the search button
search_button = self.driver.find_element(By.ID, "search-btn")
search_button.click()
time.sleep(5)
search_message = self.driver.find_element(By.ID, "results-count")
if search_message:
self.assertTrue(search_message.is_displayed())
def test_following(self):
#delete test follower to be created later. This is done to avoid filling up the database with test data
delete_test_follows(db_path)
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("followtester")
password_field.send_keys("followtester")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
# Wait for up to 10 seconds
view_recipe_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "View Recipe Details")))
#scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1)
#click view recipe button
view_recipe_button.click()
time.sleep(1)
#look for comments section
ingredients = driver.find_element(By.XPATH, "//h4[@class='section-title' and text()='Ingredients']")
ingredients_position = ingredients.location["y"]
#scroll to the follow button
scroll_script = "window.scrollBy(0, "+ str(ingredients_position-400) + ");"
driver.execute_script(scroll_script)
#find button
follow_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Follow')]")
time.sleep(1)
#click the follow button
follow_button.click()
time.sleep(2)
#scroll to the follow button
driver.execute_script(scroll_script)
time.sleep(2)
#check if the unfollow button shows
unfollow_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Unfollow')]")
assert unfollow_button.is_displayed()
def test_following_unfollow(self):
#delete test follower to be created later. This is done to avoid filling up the database with test data
delete_test_follows(db_path)
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("followtester")
password_field.send_keys("followtester")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
# Wait for up to 10 seconds
view_recipe_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "View Recipe Details")))
#scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1)
#click view recipe button
view_recipe_button.click()
time.sleep(1)
#look for comments section
ingredients = driver.find_element(By.XPATH, "//h4[@class='section-title' and text()='Ingredients']")
ingredients_position = ingredients.location["y"]
#scroll to the follow button
scroll_script = "window.scrollBy(0, "+ str(ingredients_position-400) + ");"
driver.execute_script(scroll_script)
#find button
follow_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Follow')]")
time.sleep(1)
#click the follow button
follow_button.click()
time.sleep(2)
#scroll to the follow button
driver.execute_script(scroll_script)
time.sleep(2)
#check if the unfollow button shows
unfollow_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Unfollow')]")
#click the follow button
time.sleep(1)
unfollow_button.click()
time.sleep(2)
#scroll to the follow button
driver.execute_script(scroll_script)
follow_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Follow')]")
assert follow_button.is_displayed()
def test_add_recipe_ingredients_to_shopping_list(self):
#delete test follower to be created later. This is done to avoid filling up the database with test data
delete_test_follows(db_path)
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("followtester")
password_field.send_keys("followtester")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
# Wait for up to 10 seconds
view_recipe_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "View Recipe Details")))
#scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1)
#click view recipe button
view_recipe_button.click()
time.sleep(1)
#look for comments section
ingredients = driver.find_element(By.XPATH, "//h4[@class='section-title' and text()='Ingredients']")
ingredients_position = ingredients.location["y"]
#scroll to the follow button
scroll_script = "window.scrollBy(0, "+ str(ingredients_position-400) + ");"
driver.execute_script(scroll_script)
#find button
add_to_list_button = driver.find_element(By.XPATH, '//*[@id="add-to-shopping-list-btn"]')
time.sleep(1)
#click the follow button
add_to_list_button.click()
time.sleep(2)
success_msg = driver.find_element(By.XPATH, '//*[@id="success-message"]')
assert success_msg.is_displayed()
def test_add_to_shopping_list(self):
#delete test follower to be created later. This is done to avoid filling up the database with test data
delete_test_follows(db_path)
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("followtester")
password_field.send_keys("followtester")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
time.sleep(1)
#look for comments section
shopping_list = driver.find_element(By.XPATH, '//a[contains(span,"Shopping List")]')
shopping_list.click()
time.sleep(2)
shopping_list_field = driver.find_element(By.XPATH,'/html/body/div[2]/div/div/form/div/input')
shopping_list_field.send_keys('Shopping_item')
add_to_list_button = driver.find_element(By.XPATH,'//*[@id="main-wrapper"]/div/div/form/button')
add_to_list_button.click()
time.sleep(2)
added_item = driver.find_element(By.XPATH,"//span[@class='item-name' and text()='Shopping_item']")
assert added_item.is_displayed()
def test_following_card(self):
#delete test follower to be created later. This is done to avoid filling up the database with test data
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
time.sleep(1)
following_tab = driver.find_element(By.XPATH,'//*[@id="main-wrapper"]/div/div[2]/div/div[1]/div[1]/div[1]/h5')
assert following_tab.is_displayed()
def test_notifications(self):
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
#Check if the notifications tab is visible
Notifications_tab = driver.find_element(By.XPATH,'//*[@id="main-wrapper"]/div/div[2]/div/div[2]/div/ul/li[2]/a')
Notifications_tab.click()
time.sleep(1)
assert Notifications_tab.is_displayed()
def test_favorites(self):
self.driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = self.driver.find_element(By.NAME,"username")
password_field = self.driver.find_element(By.NAME,"password")
username_field.send_keys("test_recipe")
password_field.send_keys("test_recipe")
self.driver.maximize_window()
# Submit the login form
login_button = self.driver.find_element(By.NAME,"login")
login_button.click()
time.sleep(2)
# Wait for the page to load after login (e.g., check for an element on the home page)
home_title = WebDriverWait(self.driver, 3).until(
EC.visibility_of_element_located((By.XPATH, "//h3[contains(text(),'Your online cookbook')]"))
)
# finding the browse all recipes button
browse_button = self.driver.find_element(By.XPATH, "//button[contains(@class, 'btn-primary') and contains(., 'Browse All Recipes')]")
browse_button.click()
time.sleep(2)
# Find the cards and loop over them
cards = self.driver.find_elements(By.CLASS_NAME, "card")
for card in cards:
try:
# Find the "Add to Favorites" button within the current card
add_to_favorites_button = card.find_element(By.XPATH, ".//a[contains(., 'Add to Favorites')]")
# Click thee button
add_to_favorites_button.click()
time.sleep(2)
# Find the element containing the text "Remove from Favourites" within the card element
added_favorites = self.driver.find_element(By.XPATH,'//a[contains(., "Remove from Favorites")]')
#if the item was added to favourites click on it and test if its removed
if added_favorites.is_displayed():
added_favorites.click()
time.sleep(2)
add_to_favorites_button = self.driver.find_element(By.XPATH, "//a[contains(., 'Add to Favorites')]")
self.assertTrue(add_to_favorites_button.is_displayed())
# Break the loop since the desired card is found
break
except NoSuchElementException:
pass # Continue to the next card if the button is not found
def test_user_profile(self):
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
#look for a followed user
followed_user = driver.find_element(By.XPATH,'//*[@id="main-wrapper"]/div/div[2]/div/div[1]/div[1]/div[2]/ul/li[1]/div[1]/strong/a')
followed_user.click()
#wait for user profile to load
time.sleep(2)
#Look for the unfollow button to make sure you are in the profile page
unfollow_button = driver.find_element(By.XPATH,'//*[@id="main-wrapper"]/div/div/div/div[1]/div/div/form/button')
assert unfollow_button.is_displayed()
def test_comments(self):
delete_test_comment(db_path)
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
# Wait for up to 10 seconds
view_recipe_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "View Recipe Details")))
#scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
view_recipe_button.click()
#scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
#look for comments section
comment_field = driver.find_element(By.NAME, "comment")
#Type comment
comment_field.send_keys("Nice Meal")
#click submit comment
submit_comment_buttom = driver.find_element(By.XPATH, "//button[text()='Submit Comment']")
submit_comment_buttom.click()
#Wait for page to load
time.sleep(1)
#Scroll to the bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
#Look for the comment card
comment_card = driver.find_element(By.XPATH, "//p[@class='card-text' and contains(text(), 'Nice Meal')]")
assert comment_card.is_displayed()
def test_rating(self):
delete_test_rating(db_path)
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
# Wait for the page to load after login (e.g., check for an element on the home page)
# Wait for up to 10 seconds
view_recipe_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "View Recipe Details")))
#scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
view_recipe_button.click()
#find the comments header and scroll to it to ensure the rating is visible
rate_field = driver.find_element(By.XPATH,"//input[@id='rating']")
#Give the recipe a rating of 4
rate_field.send_keys("4")
#Scroll down to find submit rating button
field_pos = rate_field.location["y"]
scroll_script = "window.scrollTo(0, "+str(field_pos)+");"
driver.execute_script(scroll_script)
#Wait for a few seconds
time.sleep(2)
#Click Submit Rating
rate_button = driver.find_element(By.XPATH,"//button[@type='submit' and contains(text(), 'Submit Rating')]")
rate_button.click()
#Wait for page to load then check that the recipe is rated
time.sleep(3)
rate_message = driver.find_element(By.XPATH,"//p[contains(text(), 'You have already rated this recipe.')]")
assert rate_message.is_displayed()
def test_user_activity(self):
driver = self.driver
driver.maximize_window()
driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = driver.find_element(By.NAME,"username")
password_field = driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = driver.find_element(By.NAME,"login")
login_button.click()
#Check if the activity tab is visible
Activity_tab = driver.find_element(By.XPATH,"//ul[contains(@class, 'nav-tabs') and contains(@class, 'profile-tab')]")
assert Activity_tab.is_displayed()
def test_add_recipe(self):
delete_recipe(db_path)
self.driver.get("http://localhost:5000")
# Find the username and password input fields and enter the credentials
username_field = self.driver.find_element(By.NAME,"username")
password_field = self.driver.find_element(By.NAME,"password")
username_field.send_keys("Khaled Elbastawisy")
password_field.send_keys("Dodobasta5")
# Submit the login form
login_button = self.driver.find_element(By.NAME,"login")
login_button.click()
time.sleep(2)
# Wait for the page to load after login (e.g., check for an element on the home page)
home_title = WebDriverWait(self.driver, 3).until(
EC.visibility_of_element_located((By.XPATH, "//h3[contains(text(),'Your online cookbook')]"))
)
# finding the browse all recipes button
button = self.driver.find_element(By.XPATH, "//button[contains(@class, 'btn-primary') and contains(., 'Add Recipe')]")
button.click()
time.sleep(3)
#finding the recipe fields and entering recipe details
name = self.driver.find_element(By.CSS_SELECTOR, 'input[name="name"]')
description = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="description"]')
cuisine = self.driver.find_element(By.NAME, "cuisine")
cuisine.click()
## Wait for the options to be visible, if necessary
desired_option_value = "american"
## Locate the option by value
option_by_value = cuisine.find_element(By.CSS_SELECTOR, f"option[value='{desired_option_value}']")
option_by_value.click()
ingredients = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="ingredients"]')
instructions = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="instructions"]')
cooking_time = self.driver.find_element(By.CSS_SELECTOR, 'input[name="cooking_time"]')
#Add recipe details
name.send_keys("Beef Burger")
description.send_keys("""A burger is a patty of ground beef grilled and placed between two halves of a bun. Slices of raw onion, lettuce, bacon, mayonnaise, and other ingredients add flavor. Burgers are considered an American food but are popular around the world.""")
ingredients.send_keys("""Ground beef
Hamburger buns
Cheese slices (optional)
Lettuce leaves
Tomato slices
Onion slices
Pickles
Ketchup
Mustard
Mayonnaise
Salt and pepper
Olive oil or butter (for greasing)""")
instructions.send_keys("""Preheat grill or stovetop pan over medium-high heat.
Season ground beef with salt and pepper, then shape into patties.
Grease grill or pan, then cook patties for 3-4 minutes per side.
Optional: Add cheese during the last minute of cooking.
Slice and toast burger buns on the grill or pan.
Assemble burgers with patties and desired toppings.
Serve and enjoy! """)
cooking_time.send_keys("20 mins")
## Attaching the image
input = self.driver.find_element(By.CSS_SELECTOR, "input.form-control#image")
input.send_keys(image)
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
## locating and clicking the create recipe button
create_button = self.driver.find_element(By.XPATH, "//button[text()='Create Recipe']")
create_button.click()
#Checking if the home page was opened
home_title = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//h3[contains(text(),'Your online cookbook')]")))
time.sleep(3)
assert home_title.is_displayed()
def test_add_delete_recipe(self):
delete_recipe(db_path)
self.driver.get("http://localhost:5000")
self.driver.maximize_window()
# Find the username and password input fields and enter the credentials
username_field = self.driver.find_element(By.NAME,"username")
password_field = self.driver.find_element(By.NAME,"password")
username_field.send_keys("test_recipe")
password_field.send_keys("test_recipe")
# Submit the login form
login_button = self.driver.find_element(By.NAME,"login")
login_button.click()
time.sleep(2)
# Wait for the page to load after login (e.g., check for an element on the home page)
home_title = WebDriverWait(self.driver, 3).until(
EC.visibility_of_element_located((By.XPATH, "//h3[contains(text(),'Your online cookbook')]"))
)
# finding the browse all recipes button
button = self.driver.find_element(By.XPATH, "//button[contains(@class, 'btn-primary') and contains(., 'Add Recipe')]")
button.click()
time.sleep(3)
#finding the recipe fields and entering recipe details
name = self.driver.find_element(By.CSS_SELECTOR, 'input[name="name"]')
description = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="description"]')
cuisine = self.driver.find_element(By.NAME, "cuisine")
cuisine.click()
## Wait for the options to be visible, if necessary
desired_option_value = "american"
## Locate the option by value
option_by_value = cuisine.find_element(By.CSS_SELECTOR, f"option[value='{desired_option_value}']")
option_by_value.click()
ingredients = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="ingredients"]')
instructions = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="instructions"]')
cooking_time = self.driver.find_element(By.CSS_SELECTOR, 'input[name="cooking_time"]')
#Add recipe details
name.send_keys("Beef Burger")
description.send_keys("""A burger is a patty of ground beef grilled and placed between two halves of a bun. Slices of raw onion, lettuce, bacon, mayonnaise, and other ingredients add flavor. Burgers are considered an American food but are popular around the world.""")
ingredients.send_keys("""Ground beef
Hamburger buns
Cheese slices (optional)
Lettuce leaves
Tomato slices
Onion slices
Pickles
Ketchup
Mustard
Mayonnaise
Salt and pepper
Olive oil or butter (for greasing)""")
instructions.send_keys("""Preheat grill or stovetop pan over medium-high heat.
Season ground beef with salt and pepper, then shape into patties.
Grease grill or pan, then cook patties for 3-4 minutes per side.
Optional: Add cheese during the last minute of cooking.
Slice and toast burger buns on the grill or pan.
Assemble burgers with patties and desired toppings.
Serve and enjoy! """)
cooking_time.send_keys("20 mins")
## Attaching the image
input = self.driver.find_element(By.CSS_SELECTOR, "input.form-control#image")
input.send_keys(image)
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
## locating and clicking the create recipe button
create_button = self.driver.find_element(By.XPATH, "//button[text()='Create Recipe']")
create_button.click()
#Checking if the home page was opened
home_title = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//h3[contains(text(),'Your online cookbook')]")))
time.sleep(3)
my_recipes_button = self.driver.find_element(By.XPATH, '//a[contains(span,"My Recipes")]')
my_recipes_button.click()
time.sleep(2)
# Locate the card based on the recipe name
recipe_name = "Beef Burger"
card_xpath = f'//div[@class="card-body"]//h5[contains(text(), "{recipe_name}")]'
time.sleep(3)
# Locate the delete button inside the card
delete_button_xpath = f'{card_xpath}//following-sibling::form//button[@class="btn btn-danger"]'
delete_button = self.driver.find_element(By.XPATH, delete_button_xpath)
# Click the delete button
delete_button.click()
time.sleep(3)
# Wait for the alert to be present
alert = WebDriverWait(self.driver, 10).until(EC.alert_is_present())
# Accept the alert by clicking OK
alert.accept()
time.sleep(2)
#locating the recipe card
try:
card_xpath = '//div[@class="card-body"]//h5[contains(text(), "test recipe")]'
card = self.driver.find_element(By.XPATH, card_xpath)
except NoSuchElementException:
# if the card is not found this means that the recipe was successfully deleted
self.assertTrue(True)
def test_add_edit_recipe(self):
delete_recipe(db_path)
self.driver.get("http://localhost:5000")
self.driver.maximize_window()
# Find the username and password input fields and enter the credentials
username_field = self.driver.find_element(By.NAME,"username")
password_field = self.driver.find_element(By.NAME,"password")
username_field.send_keys("test_recipe")
password_field.send_keys("test_recipe")
# Submit the login form
login_button = self.driver.find_element(By.NAME,"login")
login_button.click()
time.sleep(2)
# Wait for the page to load after login (e.g., check for an element on the home page)
home_title = WebDriverWait(self.driver, 3).until(
EC.visibility_of_element_located((By.XPATH, "//h3[contains(text(),'Your online cookbook')]"))
)
# finding the browse all recipes button
button = self.driver.find_element(By.XPATH, "//button[contains(@class, 'btn-primary') and contains(., 'Add Recipe')]")
button.click()
time.sleep(3)
#finding the recipe fields and entering recipe details
name = self.driver.find_element(By.CSS_SELECTOR, 'input[name="name"]')
description = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="description"]')
cuisine = self.driver.find_element(By.NAME, "cuisine")
cuisine.click()
## Wait for the options to be visible, if necessary
desired_option_value = "american"
## Locate the option by value
option_by_value = cuisine.find_element(By.CSS_SELECTOR, f"option[value='{desired_option_value}']")
option_by_value.click()
ingredients = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="ingredients"]')
instructions = self.driver.find_element(By.CSS_SELECTOR, 'textarea[name="instructions"]')
cooking_time = self.driver.find_element(By.CSS_SELECTOR, 'input[name="cooking_time"]')
#Add recipe details
name.send_keys("Beef Burger")
description.send_keys("""A burger is a patty of ground beef grilled and placed between two halves of a bun. Slices of raw onion, lettuce, bacon, mayonnaise, and other ingredients add flavor. Burgers are considered an American food but are popular around the world.""")
ingredients.send_keys("""Ground beef
Hamburger buns
Cheese slices (optional)
Lettuce leaves
Tomato slices
Onion slices
Pickles
Ketchup
Mustard
Mayonnaise
Salt and pepper
Olive oil or butter (for greasing)""")
instructions.send_keys("""Preheat grill or stovetop pan over medium-high heat.
Season ground beef with salt and pepper, then shape into patties.
Grease grill or pan, then cook patties for 3-4 minutes per side.
Optional: Add cheese during the last minute of cooking.
Slice and toast burger buns on the grill or pan.
Assemble burgers with patties and desired toppings.
Serve and enjoy! """)
cooking_time.send_keys("20 mins")
## Attaching the image
input = self.driver.find_element(By.CSS_SELECTOR, "input.form-control#image")
print(image)
input.send_keys(image)
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
## locating and clicking the create recipe button
create_button = self.driver.find_element(By.XPATH, "//button[text()='Create Recipe']")
create_button.click()
#Checking if the home page was opened
home_title = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//h3[contains(text(),'Your online cookbook')]")))
time.sleep(3)
my_recipes_button = self.driver.find_element(By.XPATH, '//a[contains(span,"My Recipes")]')
my_recipes_button.click()
time.sleep(2)
# Locate the card based on the recipe name