test_sauce_demo_class.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ##### IMPORT
  2. import pytest
  3. import logging as log
  4. import time
  5. from selenium import webdriver
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.common.keys import Keys
  8. from selenium.webdriver.support import expected_conditions as EC
  9. from selenium.common.exceptions import TimeoutException
  10. from selenium.webdriver.support.ui import WebDriverWait
  11. from selenium.webdriver.support.ui import Select
  12. from selenium.webdriver.firefox.options import Options
  13. from test_saucedemo import writeInInput
  14. import os
  15. class TestSauceDemo:
  16. ##### METHODES PYTEST
  17. def setup_method(self, method):
  18. log.info("Methode de SETUP AVANT CHAQUE TEST")
  19. if self.is_windows():
  20. self.driver = webdriver.Firefox()
  21. else:
  22. options = webdriver.FirefoxOptions()
  23. options.add_argument("--headless")
  24. self.driver = webdriver.Firefox(options=options)
  25. self.driver.get("http://www.saucedemo.com")
  26. assert (
  27. self.driver.current_url == "https://www.saucedemo.com/"
  28. ), "Nous ne somme pas sur la bonne page"
  29. def teardown_method(self, method):
  30. log.info("Methode de TEARDOWN APRES CHAQUE TEST")
  31. self.driver.quit()
  32. ##### METHODES DIVERS
  33. def is_windows(self):
  34. # oups
  35. res = False
  36. if os.name == "nt":
  37. res == True
  38. return res
  39. def writeInInput(self, inputElement, text):
  40. assert inputElement.is_displayed(), "Le champ texte n'est pas affiché"
  41. assert inputElement.is_enabled(), "Le champ texte n'est pas activé"
  42. inputElement.clear()
  43. inputElement.send_keys(text)
  44. def login(self):
  45. username = self.driver.find_element(By.ID, "user-name")
  46. password = self.driver.find_element(By.ID, "password")
  47. login = self.driver.find_element(By.ID, "login-button")
  48. writeInInput(inputElement=username, text="standard_user")
  49. writeInInput(password, text="secret_sauce")
  50. login.click()
  51. def login_custum(self, user, pwd):
  52. username = self.driver.find_element(By.ID, "user-name")
  53. password = self.driver.find_element(By.ID, "password")
  54. login = self.driver.find_element(By.ID, "login-button")
  55. writeInInput(inputElement=username, text=user)
  56. writeInInput(password, text=pwd)
  57. login.click()
  58. def logout(self):
  59. burger = self.driver.find_element(By.ID, "react-burger-menu-btn")
  60. burger.click()
  61. logout = self.driver.find_element(By.ID, "logout_sidebar_link")
  62. logout.click()
  63. def add_item(self, item):
  64. self.driver.find_element(By.ID, f"add-to-cart-sauce-labs-{item}").click()
  65. return item
  66. def go_to_checkout(self):
  67. checkout = self.driver.find_element(By.CSS_SELECTOR, "#checkout")
  68. checkout.click()
  69. ##### TEST ABBAS
  70. @pytest.mark.skip("")
  71. def test_saucedemo_boutenbout(self):
  72. self.login()
  73. self.logout()
  74. ##### TEST MANEL
  75. @pytest.mark.skip("")
  76. def test_login_bad_username(self):
  77. self.login_custum("test", "secret_sauce")
  78. erreur_message = self.driver.find_element(By.CSS_SELECTOR, "[data-test=error]")
  79. assert erreur_message.is_displayed(), "Pas de message d'erreur "
  80. assert (
  81. erreur_message.text
  82. == "Epic sadface: Username and password do not match any user in this service"
  83. ), "le message est erroné"
  84. @pytest.mark.skip("")
  85. def test_login_bad_passwrd(self):
  86. self.login_custum("standard_user", "test")
  87. erreur_message = self.driver.find_element(By.CSS_SELECTOR, "[data-test=error]")
  88. assert erreur_message.is_displayed(), "Pas de message d'erreur "
  89. assert (
  90. erreur_message.text
  91. == "Epic sadface: Username and password do not match any user in this service"
  92. ), "le message est erroné"
  93. ##### TEST MATTHIEU
  94. #@pytest.mark.skip("")
  95. def test_affichage_Checkout(self):
  96. log.info("Test de la redirection sur la page de Checkout")
  97. # connexion Swag Labs
  98. self.login()
  99. # ajout "Sauce Labs Bike Light" au panier -- A changer si Tiff implémente une fonction pour le faire
  100. item_lamp = self.driver.find_element(By.ID, "add-to-cart-sauce-labs-bike-light")
  101. item_lamp.click()
  102. # affichage panier -- A changer si Tiff implémente une fonction pour le faire
  103. panier_btn = self.driver.find_element(By.ID, "shopping_cart_container")
  104. panier_btn.click()
  105. # redirection vers la page de checkout
  106. self.go_to_checkout()
  107. assert (
  108. self.driver.current_url
  109. == "https://www.saucedemo.com/checkout-step-one.html"
  110. ), "Nous ne somme pas sur la bonne page"
  111. ##### TEST TIFF
  112. @pytest.mark.skip("")
  113. def test_add_item(self):
  114. self.login()
  115. sauce_labs_item = self.add_item("backpack")
  116. shopping_container = self.driver.find_element(By.ID, "shopping_cart_container")
  117. assert (
  118. sauce_labs_item != shopping_container.text
  119. ), f"L'item {sauce_labs_item} ne correspond pas à l'item {shopping_container.text} présent dans le panier."
  120. log.info(
  121. f"L'item : {shopping_container.text} a été ajouté et l'item : {sauce_labs_item} était attendu"
  122. )