Browse Source

Test button add to cart part 2

display text button and badge cart icon
vdiez-devweb 1 year ago
parent
commit
ec8f57b631
1 changed files with 23 additions and 10 deletions
  1. 23 10
      test_saucedemo.py

+ 23 - 10
test_saucedemo.py

@@ -180,13 +180,20 @@ def test_ajout_panier():
     slip(2)
     driver.quit()
 
-def exists_element_by_class(driver, class_elt):
+def exists_element_by_classname(driver, class_elt):
     try:
         driver.find_element(By.CLASS_NAME,class_elt)
     except:
         return False
     return True
 
+def exists_element_by_id(driver, class_elt):
+    try:
+        driver.find_element(By.ID,class_elt)
+    except:
+        return False
+    return True
+
 #@pytest.mark.skip("JE VEUX PAS LE FAIRE")
 def test_click_button_add_to_cart():
     log.info('Test sur le bouton "add to cart" change pour "remove" quand on clic dessus + bouton panier s\'incrémente')
@@ -212,27 +219,33 @@ def test_click_button_add_to_cart():
 
     # Récupérer état button "cart" : s'il a un badge récupérer le n° d'articles ajoutés, sinon n° article =0
     nb_product_initial = 0
-    if exists_element_by_class(driver,"add-to-cart-sauce-labs-backpack") and exists_element_by_class(driver,"shopping_cart_badge"):
+    if exists_element_by_classname(driver,"shopping_cart_badge"):
         badge_panier = driver.find_element(By.CLASS_NAME,"shopping_cart_badge")
-        nb_product_initial = badge_panier.text
+        nb_product_initial = int(badge_panier.text)
     log.info(f"à la connexion, il y a {nb_product_initial} dans le panier")
     slip(2)
 
     # Vérification clic button "add to cart" change de texte vers "remove"
     button_add = driver.find_element(By.ID,"add-to-cart-sauce-labs-backpack")
     button_add.click()
+    log.info("click sur 'add to cart' change button and add a badge on icon cart")
     badge_panier = driver.find_element(By.CLASS_NAME,"shopping_cart_badge")
-    log.info(f"à l'ajout d'un article', il y a {badge_panier.text} dans le panier")
+    nb_product = int(badge_panier.text)
+    log.info(f"à l'ajout d'un article', il y a {nb_product} dans le panier")
     slip(2)
-    assert False == exists_element_by_class(driver,"add-to-cart-sauce-labs-backpack")
-    assert True == exists_element_by_class(driver,"remove-sauce-labs-backpack")
+    assert False == exists_element_by_id(driver,"add-to-cart-sauce-labs-backpack")
+    assert True == exists_element_by_id(driver,"remove-sauce-labs-backpack")
     # et que le button "cart" a un badge avec n° incrémenté
-    assert str(nb_product_initial+1) == badge_panier.text
+    assert nb_product_initial+1 == nb_product
 
     # Vérification clic button "remove" change de texte vers "add to cart"
-    log.info("click sur 'add to cart' change button and add a badge on icon cart")
-
-    # Vérification button "cart" a un badge avec n° incrémenté ou supprimé si de 1 à 0
+    button_remove = driver.find_element(By.ID,"remove-sauce-labs-backpack")
+    button_remove.click()
+    log.info("click sur 'Remove' change button and remove the badge on icon cart")
+    assert True == exists_element_by_id(driver,"add-to-cart-sauce-labs-backpack")
+    assert False == exists_element_by_id(driver,"remove-sauce-labs-backpack")
+    # Vérification button "cart" a un badge supprimé car 0 articles dans le panier
+    assert False == exists_element_by_classname(driver,"shopping_cart_badge")
 
     slip(2)
     driver.quit()