Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. # bnk
  2. import unittest
  3. import HtmlTestRunner
  4. from selenium import webdriver
  5.  
  6. import os, sys, inspect
  7. # fetch path to the directory in which current file is, from root directory or C:\ (or whatever driver number it is)
  8. currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  9. # extract the path to parent directory
  10. parentdir = os.path.dirname(currentdir)
  11. # insert path to the folder from parent directory from which the python module/ file is to be imported
  12. sys.path.insert(0, parentdir+'\Resources')
  13. sys.path.insert(0, parentdir+'\Resources\PO')
  14.  
  15. from Locators import Locators
  16. from TestData import TestData
  17. from PO import Pages
  18. from Pages import HomePage, SearchResultsPage, ProductDetailsPage, SubCartPage, CartPage, SignInPage
  19.  
  20. # Base Class for the tests
  21. class Test_AMZN_Search_Base(unittest.TestCase):
  22.  
  23. def setUp(self):
  24. # Setting up how we want Chrome to run
  25. chrome_options=webdriver.ChromeOptions()
  26. # chrome_options.add_argument('headless')
  27. # chrome_options.add_argument('window-size=1920x1080')
  28. # print ("Chrome Path: " + TestData.CHROME_EXECUTABLE_PATH + " ends here")
  29. self.driver=webdriver.Chrome(TestData.CHROME_EXECUTABLE_PATH, options=chrome_options)
  30. #browser should be loaded in maximized window
  31. self.driver.maximize_window()
  32.  
  33. def tearDown(self):
  34. # To do the cleanup after test has executed.
  35. self.driver.close()
  36. self.driver.quit()
  37.  
  38. # Test Class containing all tests
  39. class Test_AMZN_Search(Test_AMZN_Search_Base):
  40. def setUp(self):
  41. super().setUp()
  42.  
  43. def test_home_page_loaded_successfully(self):
  44. # instantiate an object of HomePage class. Remember when the constructor of HomePage class is called
  45. # it opens up the browser and navigates to Home Page of the site under test.
  46. self.homePage=HomePage(self.driver)
  47. # assert if the title of Home Page contains Amazon.in
  48. self.assertIn(TestData.HOME_PAGE_TITLE, self.homePage.driver.title)
  49.  
  50. def test_user_should_be_able_to_search(self):
  51. self.homePage=HomePage(self.driver)
  52. # search for the search term on Home Page. The search term would be picked from
  53. # test data file
  54. self.homePage.search()
  55. # instantiate an object of SearchResultsPage class passing in the driver as parameter.
  56. # this will allow the newly created object to have access to the browser and perform
  57. # operations further.
  58. self.searchResultsPage=SearchResultsPage(self.homePage.driver)
  59. # assert if the search term is present in the title of the Amazon's Search Results Page
  60. self.assertIn(TestData.SEARCH_TERM,self.searchResultsPage.driver.title)
  61. # assert that the search term indeed return some results.
  62. self.assertNotIn(TestData.NO_RESULTS_TEXT,self.searchResultsPage.driver.page_source)
  63.  
  64. def test_user_should_be_able_to_add_item_to_cart(self):
  65. self.homePage=HomePage(self.driver)
  66. self.homePage.search()
  67. self.searchResultsPage=SearchResultsPage(self.homePage.driver)
  68. # click on the first search result
  69. self.searchResultsPage.click_search_result()
  70. # since the click on search result loads the product in new tab, switch to new tab
  71. self.searchResultsPage.driver.switch_to.window(self.searchResultsPage.driver.window_handles[1])
  72. # instantiate an object of Product Details Page class
  73. self.productDetailsPage=ProductDetailsPage(self.searchResultsPage.driver)
  74. # click on the Add To Cart button
  75. self.productDetailsPage.click_add_to_cart_button()
  76. # instantiate an object of Sub Cart Page class
  77. self.subCartPage=SubCartPage(self.productDetailsPage.driver)
  78. # assert if the sub cart page has indeed loaded
  79. self.assertTrue(self.subCartPage.is_enabled(Locators.SUB_CART_DIV))
  80. # assert if the product was added to the cart successfully
  81. self.assertTrue(self.searchResultsPage.is_visible(Locators.PROCEED_TO_BUY_BUTTON))
  82.  
  83. def test_user_should_be_able_to_delete_item_from_cart(self):
  84. self.homePage=HomePage(self.driver)
  85. self.homePage.search()
  86. self.searchResultsPage=SearchResultsPage(self.homePage.driver)
  87. self.searchResultsPage.click_search_result()
  88. self.searchResultsPage.driver.switch_to_window(self.searchResultsPage.driver.window_handles[1])
  89. self.productDetailsPage=ProductDetailsPage(self.searchResultsPage.driver)
  90. self.productDetailsPage.click_add_to_cart_button()
  91. self.subCartPage=SubCartPage(self.productDetailsPage.driver)
  92. # click on the Cart's hyperlink to load the cart page
  93. self.subCartPage.click_cart_link()
  94. # instantiate an object of Cart Page class
  95. self.cartPage=CartPage(self.subCartPage.driver)
  96. # find the cart count before deleting an item from the cart
  97. cartCountBeforeDeletion=int(self.driver.find_element(*Locators.CART_COUNT).text)
  98. # delete an item from cart
  99. self.cartPage.delete_item()
  100. #to assert the item was deleted successfully
  101. self.assertTrue(int(self.driver.find_element(*Locators.CART_COUNT).text) < cartCountBeforeDeletion)
  102.  
  103. def test_user_must_signin_to_checkout(self):
  104. self.homePage=HomePage(self.driver)
  105. self.homePage.search()
  106. self.searchResultsPage=SearchResultsPage(self.homePage.driver)
  107. self.searchResultsPage.click_search_result()
  108. self.searchResultsPage.driver.switch_to_window(self.searchResultsPage.driver.window_handles[1])
  109. self.productDetailsPage=ProductDetailsPage(self.searchResultsPage.driver)
  110. self.productDetailsPage.click_add_to_cart_button()
  111. self.subCartPage=SubCartPage(self.productDetailsPage.driver)
  112. self.subCartPage.click_cart_link()
  113. # instantiate an object of Cart Page class
  114. self.cartPage=CartPage(self.subCartPage.driver)
  115. #click on Proceed to Checkout button
  116. self.cartPage.click_proceed_to_checkout_button()
  117. # instantiate an object of SignIn Page class
  118. self.signInPage=SignInPage(self.cartPage.driver)
  119. # to assert we are in indeed on Sign In Page, first we assert the title of the page
  120. self.assertTrue(TestData.SIGN_IN_PAGE_TITLE,self.signInPage.driver.title)
  121. # and then we assert for presence of email textbox on the page
  122. self.assertTrue(self.signInPage.is_visible(Locators.USER_EMAIL_OR_MOBIL_NO_TEXTBOX))
  123.  
  124. if __name__ == '__main__':
  125. # specify path where the HTML reports for testcase execution are to be generated
  126. unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output=parentdir + '\Reports'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement