def test_promotion3(self):
        driver = self.driver
        driver.get("https://knight-stag.castlery.sg/spree/admin/promotions")
        sleep(5)

        #login knight
        web_stag.login_knight(self)

        #搜索auto test 3 并激活它,如果没有则创建它
        driver.find_element_by_id("q_name_cont").send_keys("auto test 3")
        driver.find_element_by_name("button").click()
        sleep(5)
        try:
            driver.find_element_by_xpath(
                "//*[@class='fa fa-edit icon_link with-tip no-text']")
            promoexist = True
        except:
            promoexist = False
        if promoexist == True:
            driver.find_element_by_xpath(
                "//*[@class='fa fa-edit icon_link with-tip no-text']").click()
            sleep(5)
            #检查promotion如果没激活就激活
            active = driver.find_element_by_id(
                "promotion_status").get_attribute("checked")
            if active == True:
                driver.find_element_by_name("button").click()
            elif active == None:
                driver.find_element_by_id("promotion_status").click()
                driver.find_element_by_name("button").click()
        #如果连接不存在,则需要创建promotion
        elif promoexist == False:
            create_promotion.auto_test_3(self)

        #去website上添加产品
        driver.get("https://stag.castlery.com")

        #stag验证
        web_stag.stag_authentication(self)
        #关掉各种弹窗
        web_stag.initial_web(self)
        #先清空cart方便做测试
        #web_stag.clear_cart(self)
        web_stag.move_to_furniture(self)
        driver.find_element_by_link_text("Armchairs").click()
        sleep(5)
        #driver.find_element_by_xpath("//a[contains(text(),'Bambu Armchair')]").click()
        driver.find_element_by_xpath(
            "(//*[@data-selenium='category-product'])[1]").click()
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='add-to-cart']").click()
        sleep(5)
        driver.find_element_by_xpath("//*[@class='ZncuLx__close btn']").click()
        sleep(1)
        #web_stag.move_to_furniture(self)
        #driver.find_element_by_link_text("All Sofas").click()
        #sleep(5)
        #driver.find_element_by_xpath("//div[@class='_2qSCjD']//div[1]//a[2]").click()
        #sleep(5)
        driver.get(
            "https://stag.castlery.com/products/wayne-2-seater-sofa?quantity=1&material=stone_grey"
        )
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='add-to-cart']").click()
        sleep(5)
        #case1 cart中加入2个产品,只有一个满足rule中的taxons,promotion不生效
        print "case1: only 1 product in taxons 'Armchair',promotion not works"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion dos not works(should be)"
        #case2 bambu armchair qty+!,rule 条件满足,但是action不满足,promotion不生效
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[1]').click()
        sleep(3)
        print "case2: 2 product in taxons 'Armchair', but no product in eligible action taxons,promotion not works"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion dos not works(should be)"
        #case3 add a product in tv console make promotion works
        driver.find_element_by_xpath("//*[@class='ZncuLx__close btn']").click()
        sleep(1)
        #web_stag.move_to_furniture(self)
        #driver.find_element_by_link_text("TV Consoles").click()
        #sleep(5)
        #driver.find_element_by_xpath("//a[contains(text(),'Bambu TV Console')]").click()
        #sleep(5)
        driver.get(
            "https://stag.castlery.com/products/bambu-tv-console?quantity=1")
        sleep(5)
        text_bambu_tv = driver.find_element_by_xpath(
            "//span[@data-selenium='product-price']").text
        price_bambu_tv = Decimal(sub(r'[^\d.]', '', text_bambu_tv))
        driver.find_element_by_xpath(
            "//*[@data-selenium='add-to-cart']").click()
        sleep(5)
        print "case3: 2procuts in taxon 'Armchair',1 product in 'TV console',promotion works"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works(should be)"
        elif promo == False:
            print "promotion dos not works"
        #case4: 只有action中的taxons里面的产品才能打折
        print "case4: discount = price_bambu_tv/10"
        discount_case4_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case4 = Decimal(sub(r'[^\d.]', '', discount_case4_text))
        discount_calculate_case4 = price_bambu_tv / 10
        try:
            self.assertEqual(discount_case4, discount_calculate_case4)
            print u"discount = price_bambu_tv/10"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        #case5 tv console qty+1 discount = price_bambu_tv*2
        #删除Wayne sofa
        driver.find_element_by_xpath(
            "(//*[@data-selenium='cart-item-remove'])[2]").click()
        sleep(5)
        #bambu tv console qty+1
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[2]').click()
        sleep(5)
        discount_case5_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case5 = Decimal(sub(r'[^\d.]', '', discount_case5_text))
        discount_calculate_case5 = (price_bambu_tv * 2) / 10
        print "case5: discount = price_bambu_tv*2"
        try:
            self.assertEqual(discount_case5, discount_calculate_case5)
            print u"discount = (price_bambu_tv*2)/10"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        #case6 bambu tv console qty再次+1,discount = price_bambu_tv*2,only 2 action taxons products can get discount
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[2]').click()
        sleep(5)
        print "case6: only 2 products in action taxons can get discount,discount_case6 = discount_case5"
        discount_case6_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case6 = Decimal(sub(r'[^\d.]', '', discount_case6_text))
        try:
            self.assertEqual(discount_case6, discount_case5)
            print u"discount not changed,discount_case6 = discount_5"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        #case7 bambu armchair qty -1 promotion not works
        driver.find_element_by_xpath(
            "(//*[@data-selenium='cart-item-minus'])[1]").click()
        sleep(5)
        print "case7: only 1 product in rules taxons, promotion not work"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion dos not works(should be)"
        #去dining table添加seb dining table。
        driver.find_element_by_xpath("//*[@class='ZncuLx__close btn']").click()
        sleep(1)
        #web_stag.move_to_furniture(self)
        #driver.find_element_by_link_text("Dining Tables")
        #sleep(5)
        #driver.find_element_by_xpath("//div[@class='_2qSCjD']//div[3]//a[2]").click()
        #sleep(5)
        driver.get(
            "https://stag.castlery.com/products/seb-dining-table?quantity=1&length=1_5m"
        )
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='add-to-cart']").click()
        sleep(5)
        #case8 rule里面设置了2个taxons数量为2,所以必须至少某一个taxons中有2个产品promotion才会生效,每样一个不可以
        print "case8: every taxons in rules have only 1 product,promotion will not work"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion dos not works(should be)"
        #case9 seb dining table qty+1 promotion生效了
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[3]').click()
        sleep(5)
        print "case9: seb dining qty+1,one taxons in rules have 2 products now ,promotion works"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works(should be)"
        elif promo == False:
            print "promotion dos not works"
        #case10 discount_case9 = discount_case6
        print "discount_case9 = discount_case6"
        discount_case9_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case9 = Decimal(sub(r'[^\d.]', '', discount_case9_text))
        try:
            self.assertEqual(discount_case9, discount_case6)
            print u"discount will not changed, discount_case9 = discount_case6"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        #去添加一个coffee table产品
        driver.find_element_by_xpath("//*[@class='ZncuLx__close btn']").click()
        sleep(1)
        #web_stag.move_to_furniture(self)
        #driver.find_element_by_link_text("Coffee Tables").click()
        #sleep(5)
        #driver.find_element_by_xpath("//a[contains(text(),'Scarlett Coffee Table')]").click()
        #sleep(5)
        driver.get(
            "https://stag.castlery.com/products/scarlett-coffee-table?quantity=1&length=1_0m"
        )
        sleep(5)
        text_scarlett_coffee = driver.find_element_by_xpath(
            "//span[@data-selenium='product-price']").text
        price_scarlett_coffee = Decimal(
            sub(r'[^\d.]', '', text_scarlett_coffee))
        driver.find_element_by_xpath(
            "//*[@data-selenium='add-to-cart']").click()
        sleep(10)
        #case11 由于是line item级别的action,所以action中设置的2个taxons中的产品分别会有2个产品都享受折扣,这和order级别不同,order级别是算总数
        print "case11: discount_case11 = (price_bambu_tv*2)/10 + price_scarlett/10"
        discount_case11_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case11 = Decimal(sub(r'[^\d.]', '', discount_case11_text))
        discount_calculate_case11 = (price_bambu_tv *
                                     2) / 10 + price_scarlett_coffee / 10
        try:
            self.assertEqual(discount_case11, discount_calculate_case11)
            print u"both products in action taxons can get discount"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        #case12 每属于action taxons的产品只有2个可以享受折扣,把coffee table的数量加到3,但是只有2个享受折扣
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[4]').click()
        sleep(10)
        discount_case12_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case12 = Decimal(sub(r'[^\d.]', '', discount_case12_text))
        discount_calculate_case12 = (price_bambu_tv * 2 +
                                     price_scarlett_coffee * 2) / 10
        print "case12: discount_case12 = (price_bambu_tv*2 + price_scarlett_coffee*2)/10"
        try:
            self.assertEqual(discount_case12, discount_calculate_case12)
            print u"maximum 2 products in every action taxons can get discount"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        #case13 再次把coffee table的qty+1 discount不变
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[4]').click()
        sleep(10)
        discount_case13_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case13 = Decimal(sub(r'[^\d.]', '', discount_case13_text))
        print "case13: in every action taxons can maximum 2 products get discount"
        try:
            self.assertEqual(discount_case13, discount_case12)
            print u"discount will not changed"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        driver.find_element_by_xpath("//*[@class='ZncuLx__close btn']").click()
        sleep(1)
        #去coffee tables中找一个价格高于scarlett coffee table的产品seb coffee table
        #web_stag.move_to_furniture(self)
        #driver.find_element_by_link_text("Coffee Tables").click()
        #sleep(5)
        #添加一个价格比bambu armchair高的 beck armchair leather
        #driver.find_element_by_xpath("//a[contains(text(),'Seb Coffee Table')]").click()
        #sleep(5)
        driver.get(
            "https://stag.castlery.com/products/seb-coffee-table?quantity=1")
        sleep(10)
        text_seb_coffee = driver.find_element_by_xpath(
            "//span[@data-selenium='product-price']").text
        price_seb_coffee = Decimal(sub(r'[^\d.]', '', text_seb_coffee))
        driver.find_element_by_xpath(
            "//*[@data-selenium='add-to-cart']").click()
        sleep(10)
        #case14 对于line item级别的action每个line item都享受下面的数量,就是没法限制享受promotion的产品数量
        print "case14: discount_case14 = (price_bambu_tv*2 + price_scarlett_coffee*2 + price_seb_coffee)/10"
        discount_case14_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case14 = Decimal(sub(r'[^\d.]', '', discount_case14_text))
        discount_calculate_case14 = (price_bambu_tv * 2 +
                                     price_scarlett_coffee * 2 +
                                     price_seb_coffee) / 10
        try:
            self.assertEqual(discount_case14, discount_calculate_case14)
            print u"seb coffee table get discount"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        #case15: seb coffee table qty+1这时候discount要再加上price_seb_coffee/10
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[5]').click()
        sleep(10)
        print "case15: discount_case15 = (price_bambu_tv*2 + price_scarlett_coffee*2 + price_seb_coffee*2)/10"
        discount_case15_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case15 = Decimal(sub(r'[^\d.]', '', discount_case15_text))
        discount_calculate_case15 = (price_bambu_tv * 2 +
                                     price_scarlett_coffee * 2 +
                                     price_seb_coffee * 2) / 10
        try:
            self.assertEqual(discount_case15, discount_calculate_case15)
            print u"seb coffee table can also get discount for 2 pices"
        except AssertionError as e:
            print u"promotion wrong! test failed"
        #case16 seb coffee table qty = 3的时候还是只有2个可以享受折扣
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[5]').click()
        sleep(10)
        print "for every line item in action taxons can maximum 2 pieces get discount"
        discount_case16_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case16 = Decimal(sub(r'[^\d.]', '', discount_case16_text))
        try:
            self.assertEqual(discount_case16, discount_case15)
            print u"maximum 2 pieces get discount, so discount not changed"
        except AssertionError as e:
            print u"promotion wrong! test failed"
    def test_change_leadtime_and_qty(self):
        driver = self.driver

        #change sg stag
        driver.get("https://knight-stag.castlery.sg/")
        sleep(3)
        web_stag.login_knight(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/85856/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/22190/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/31688/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/21526/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/31417/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/24374/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/86789/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/86066/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/stock_items/57545/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        #change au stag
        driver.get("https://knight-stag.castlery.com.au")
        sleep(3)
        web_stag.login_knight(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/9626/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/9495/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/12396/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/13843/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/19353/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/13899/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/11413/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/7969/edit"
        )
        sleep(3)
        web_stag.change_qty(self)

        driver.get(
            "https://knight-stag.castlery.com.au/spree/admin/stock_items/19979/edit"
        )
        sleep(3)
        web_stag.change_qty(self)
    def test_promotion1(self):
        driver = self.driver
        driver.get("https://knight-stag.castlery.sg/spree/admin/promotions")
        sleep(5)

        #login knight
        web_stag.login_knight(self)

        #搜索auto test 1 并active 它 如果没有则创建它
        driver.find_element_by_id("q_name_cont").send_keys("auto test 1")
        driver.find_element_by_name("button").click()
        sleep(5)
        try:
            driver.find_element_by_xpath(
                "//*[@class='fa fa-edit icon_link with-tip no-text']")
            promoexist = True
        except:
            promoexist = False
        if promoexist == True:
            driver.find_element_by_xpath(
                "//*[@class='fa fa-edit icon_link with-tip no-text']").click()
            #检查promotion如果没激活就激活
            active = driver.find_element_by_id(
                "promotion_status").get_attribute("checked")
            if active == True:
                driver.find_element_by_name("button").click()
            elif active == None:
                driver.find_element_by_id("promotion_status").click()
                driver.find_element_by_name("button").click()
        #如果连接不存在,则需要创建promotion
        elif promoexist == False:
            create_promotion.auto_test_1(self)

        #到web去添加产品
        driver.get(
            "https://stag.castlery.com/products/colbert-pendant-light-51850015?quantity=1"
        )

        #stag验证
        web_stag.stag_authentication(self)
        #stag 身份验证并关掉各种弹窗
        web_stag.initial_web(self)

        #添加产品
        driver.find_element_by_xpath(
            "//*[@data-selenium='top-layer']//*[@data-selenium='add-to-cart']"
        ).click()
        sleep(5)

        #判断promotion是否存在
        #case1
        print "case1: item total<200, promotion not works, shipping is not free"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "OK! promotion works"
        elif promo == False:
            print "WRONG! promotion should not work"

        #判断是否shipping free
        shippingcost = driver.find_element_by_xpath(
            "//*[@data-selenium='cart-shipping']/span").text
        try:
            self.assertEqual(u"Free", shippingcost)
            print u" total>$300 shipping is free now"
        except AssertionError as e:
            print u"total<$300 shipping is not free"

        #产品qty+1 = 2 total=278 有shippingcost 有promotion
        #case2
        print "case2: 200<total=278<300, promotion works, discount=20, shipping not free"
        driver.find_element_by_xpath(
            '//*[@data-selenium="cart-item-plus"]').click()
        sleep(5)
        #判断promotion是否存在
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "OK! promotion works"
        elif promo == False:
            print "WRONG! promotion should not work"

        #判断discount 数值是否为20
        disc = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        try:
            self.assertEqual(u"-$20", disc)
            print u"200< total=278 <500 discount=$20"
        except AssertionError as e:
            print u"promotion error"

        #判断是否shipping free
        shippingcost = driver.find_element_by_xpath(
            "//*[@data-selenium='cart-shipping']/span").text
        try:
            self.assertEqual(u"Free", shippingcost)
            print u" total>$300 shipping is free now"
        except AssertionError as e:
            print u"total<$300 shipping is not free"

        #qty再次+1 = 3 total=417 shipping free 有discount
        #case3
        print "case3: 300<total<500 promotion works, discount=20, shipping free"
        driver.find_element_by_xpath(
            '//*[@data-selenium="cart-item-plus"]').click()
        sleep(5)
        #判断promotion是否存在
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "OK! promotion works"
        elif promo == False:
            print "WRONG! promotion should not work"

        #discount 还是20
        disc1 = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        try:
            self.assertEqual(u"-$20", disc1)
            print u"200< total=417 <500 discount=$20"
        except AssertionError as e:
            print u"promotion error"

        #判断是否shipping free
        shippingcost = driver.find_element_by_xpath(
            "//*[@data-selenium='cart-shipping']/span").text
        try:
            self.assertEqual(u"Free", shippingcost)
            print u" total>$300 shipping is free now"
        except AssertionError as e:
            print u"total<$300 shipping is not free"

        #qty再+1 = 4 total=556 shipping free 并且没有 discount
        #case4
        print "case4: total>500, promotion should not work, shipping free, no disount"
        driver.find_element_by_xpath(
            '//*[@data-selenium="cart-item-plus"]').click()
        sleep(5)

        #判断promotion是否存在
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "OK! promotion works"
        elif promo == False:
            print "WRONG! promotion should not work"

        #判断是否shipping free
        shippingcost = driver.find_element_by_xpath(
            "//*[@data-selenium='cart-shipping']/span").text
        try:
            self.assertEqual(u"Free", shippingcost)
            print u" total>$300 shipping is free now"
        except AssertionError as e:
            print u"total<$300 shipping is not free"

        #清空cart
        driver.find_element_by_xpath(
            "//*[@data-selenium='cart-item-remove']").click()
        sleep(3)
        driver.find_element_by_css_selector(
            "button.ZncuLx__close.btn > svg").click()
        sleep(1)

        #去后台关掉promotion
        driver.get("https://knight-stag.castlery.sg/spree/admin/promotions")
        driver.find_element_by_xpath("//*[@id='q_name_cont']").send_keys(
            "auto test 1")
        driver.find_element_by_xpath("//*[@class='button']").click()
        sleep(3)
        driver.find_element_by_xpath("//a[@data-action='edit']").click()
        driver.find_element_by_id("promotion_status").click()
        driver.find_element_by_name("button").click()
    def test_lastqty(self):
        driver = self.driver
        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/products/debbie-chair/stock"
        )
        sleep(5)

        # login knight
        web_stag.login_knight(self)

        # change dark gray 的MY stock的qty=2
        driver.find_element_by_xpath(
            "//*[@href='https://knight-stag.castlery.sg/spree/admin/stock_items/58588/edit']"
        ).click()
        sleep(5)
        driver.find_element_by_id("stock_item_count_on_hand").clear()
        driver.find_element_by_id("stock_item_count_on_hand").send_keys(2)
        driver.find_element_by_name("button").click()
        sleep(5)

        # 返回并编辑defalut leadtime 的 qty = 0
        driver.get(
            "https://knight-stag.castlery.sg/spree/admin/products/debbie-chair/stock"
        )
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@href='https://knight-stag.castlery.sg/spree/admin/stock_items/31042/edit']"
        ).click()
        sleep(5)
        driver.find_element_by_id("stock_item_count_on_hand").clear()
        driver.find_element_by_id("stock_item_count_on_hand").send_keys(0)
        driver.find_element_by_name("button").click()
        sleep(5)

        # 打开web stag
        driver.get(
            "https://stag.castlery.com/products/nixon-dining-table-with-4-debbie-chairs?table=91&chair1=132&chair2=132&quantity=1"
        )

        #stag验证
        web_stag.stag_authentication(self)
        # stag 身份验证以及关掉各种弹窗
        web_stag.initial_web(self)

        # 选择第二个sub product为dark gray
        driver.find_element_by_xpath(
            "//img[@alt='Debbie Chair, Light Grey']").click()
        driver.find_element_by_xpath("//img[@alt='Dark Grey']").click()
        driver.find_element_by_xpath("//*[contains(text(),'Confirm')]").click()
        sleep(3)

        # 选择第三个sub product为dark gray
        driver.find_element_by_xpath(
            "//img[@alt='Debbie Chair, Light Grey']").click()
        driver.find_element_by_xpath("//img[@alt='Dark Grey']").click()
        driver.find_element_by_xpath("//*[contains(text(),'Confirm')]").click()
        sleep(3)

        # add to cart and checkout
        driver.find_element_by_xpath(
            "//*[@data-selenium='add-to-cart']").click()
        sleep(5)
        driver.find_element_by_xpath("//*[@data-selenium='check-out']").click()
        sleep(5)

        # loghin user
        web_stag.login_web(self)

        # 点击continue出现报错信息
        driver.find_element_by_xpath(
            "//*[@data-selenium='checkout-shipping-address']").click()
        sleep(5)

        # 拿到报错提示信息,然后验证是否符合预期qty not enough
        alert = driver.find_element_by_xpath(
            "//*[@class='_1Nr1lS__body']").text
        try:
            self.assertEqual(
                u"This product is out of stock for the selected quantity.",
                alert)
            print u"test success: get alert because quantity not enough for this order"
        except AssertionError as e:
            print u"test last quantity failed"

        driver.find_element_by_xpath("//*[contains(text(),'Got it')]").click()

        # 返回首页然后清空cart
        driver.find_element_by_xpath(
            "//img[@alt='Castlery, online furniture shop in Singapore']"
        ).click()
        sleep(5)
        web_stag.clear_cart(self)

        # logout
        web_stag.logout_web(self)
Example #5
0
    def test_promotion2(self):
        driver = self.driver
        driver.get("https://knight-stag.castlery.sg/spree/admin/promotions")
        sleep(5)

        #login knight
        web_stag.login_knight(self)

        #搜索auto test 2 并激活它,如果没有则创建它
        driver.find_element_by_id("q_name_cont").send_keys("auto test 2")
        driver.find_element_by_name("button").click()
        sleep(5)
        try:
            driver.find_element_by_xpath(
                "//*[@class='fa fa-edit icon_link with-tip no-text']")
            promoexist = True
        except:
            promoexist = False
        if promoexist == True:
            driver.find_element_by_xpath(
                "//*[@class='fa fa-edit icon_link with-tip no-text']").click()
            sleep(5)
            #检查promotion如果没激活就激活
            active = driver.find_element_by_id(
                "promotion_status").get_attribute("checked")
            if active == True:
                driver.find_element_by_name("button").click()
            elif active == None:
                driver.find_element_by_id("promotion_status").click()
                driver.find_element_by_name("button").click()
        #如果连接不存在,则需要创建promotion
        elif promoexist == False:
            create_promotion.auto_test_2(self)

        #去website上添加产品
        driver.get("https://stag.castlery.com")

        #stag验证
        web_stag.stag_authentication(self)
        #关掉各种弹窗
        web_stag.initial_web(self)
        #先清空cart方便做测试
        #web_stag.clear_cart(self)

        #去all sofa搜索一个sale product 加入cart
        web_stag.move_to_furniture(self)
        driver.find_element_by_link_text("All Sofas").click()
        #在fliter那边点击sale过滤出产品
        driver.find_element_by_xpath(
            "//*[@class='sk-panel__content']//*[contains(text(),'Sale')]"
        ).click()
        sleep(5)
        driver.find_element_by_xpath(
            "(//*[@data-selenium='category-product'])[1]").click()
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='add-to-cart']").click()
        sleep(5)
        #case1 add Moore sofa to cart
        print "case1: sale product in cart, item total<1500, so promotion not works"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion not works"
        sleep(3)
        driver.find_element_by_xpath("//*[@class='ZncuLx__close btn']").click()
        sleep(3)
        #重新打开all sofas 选择产品,chromedriver有问题,只能通过直接访问产品url的方式才能点到add to cart
        driver.get(
            "https://stag.castlery.com/products/beck-sofa?quantity=1&material=tiffany_blue"
        )
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='top-layer']//*[@data-selenium='add-to-cart']"
        ).click()
        sleep(5)
        #case2 添加了beck sofa to cart
        print "case2: item total > 1500, product's total in tag 'sale'<1500 ,promotion not works"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion not works"

        #case3 moore sofa qty+1
        print "case3: sale product total>1500, no new product in cart, promotion not works"
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[1]').click()
        sleep(5)
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion not works"
        #删除beck sofa
        driver.find_element_by_xpath(
            "(//*[@data-selenium='cart-item-remove'])[2]").click()
        sleep(3)
        #关掉cart
        driver.find_element_by_css_selector(
            "button.ZncuLx__close.btn > svg").click()
        sleep(3)

        driver.get(
            "https://stag.castlery.com/products/seb-dining-table?quantity=1&length=1_5m"
        )
        sleep(10)
        text_seb = driver.find_element_by_xpath(
            "//*[@data-selenium='top-layer']//span[@data-selenium='product-price']"
        ).text
        price_seb = Decimal(sub(r'[^\d.]', '', text_seb))
        driver.find_element_by_xpath(
            "//*[@data-selenium='top-layer']//*[@data-selenium='add-to-cart']"
        ).click()
        sleep(5)
        #case4 添加产品seb dining table to cart 这样cart中有了一个sale tag pro product
        print "case4: sale product total>1500, have 1 new tag product, promotion works,"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion not works"
        #case5 discount 是new product 总价的10%
        print "case5: discount_case5 = price_sub * 10%"
        discount_case5_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case5 = Decimal(sub(r'[^\d.]', '', discount_case5_text))
        try:
            self.assertEqual(discount_case5, price_seb / 10)
            print u"promotion works! discount = 10% of price_seb"
        except AssertionError as e:
            print u"promotion wrong! test faild"
        #case6 将Moore sofa的qty-1,这样total price in tag sale <1500,promotion not work
        print "case6: total(proudct in tag 'sale')<1500, promotion not works"
        driver.find_element_by_xpath(
            "//*[@data-selenium='cart-item-minus']").click()
        sleep(5)
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion not works"
        #继续把Moore sofa的qty+1,准备之后的test
        driver.find_element_by_xpath(
            '//*[@data-selenium="cart-item-plus"]').click()
        sleep(5)
        driver.find_element_by_xpath("//*[@class='ZncuLx__close btn']").click()
        sleep(3)

        driver.get(
            "https://stag.castlery.com/products/wayne-2-seater-sofa?quantity=1&material=stone_grey"
        )
        sleep(5)
        text_wayne = driver.find_element_by_xpath(
            "//*[@data-selenium='top-layer']//span[@data-selenium='product-price']"
        ).text
        price_wayne = Decimal(sub(r'[^\d.]', '', text_wayne))

        driver.find_element_by_xpath(
            "//*[@data-selenium='top-layer']//*[@data-selenium='add-to-cart']"
        ).click()
        sleep(5)
        #case7 promotion works
        print "case7:promotion works"
        try:
            driver.find_element_by_xpath("//*[contains(text(),'Promotion')]")
            promo = True
        except:
            promo = False
        if promo == True:
            print "promotion works"
        elif promo == False:
            print "promotion not works"
        #case8 ,discount 为这2个产品总和的10%
        print "discount=(price_seb+price_wayne)*10%"
        discount_case8_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case8 = Decimal(sub(r'[^\d.]', '', discount_case8_text))
        discount_calculate1 = (price_seb + price_wayne) / 10
        try:
            self.assertEqual(discount_case8, discount_calculate1)
            print u"promotion works! discount = 10% of (price_seb+price_wayne)"
        except AssertionError as e:
            print u"promotion wrong! test faild"

        #case9 seb dining table 的qty+1,discount不变
        print "case9: seb dining table qty+1,discount = (price_seb+price_wayne)*10%"
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[2]').click()
        sleep(3)
        discount_case9_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case9 = Decimal(sub(r'[^\d.]', '', discount_case9_text))
        if price_seb > price_wayne:
            discount_calculate2 = (price_seb * 2) / 10
        else:
            discount_calculate2 = (price_seb + price_wayne) / 10
        try:
            self.assertEqual(discount_case9, discount_calculate2)
            print u"promotion works! discount_case9 = discount_case8,discount has not changed"
        except AssertionError as e:
            print u"promotion wrong! test faild"

        #case10 wayne sofa qty+1
        print "case10: wayne sofa qty+1,discount=(price_wayne*2)/10"
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[3]').click()
        sleep(3)
        discount_case10_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case10 = Decimal(sub(r'[^\d.]', '', discount_case10_text))
        if price_seb > price_wayne:
            discount_calculate3 = (price_seb * 2) / 10
        else:
            discount_calculate3 = (price_wayne * 2) / 10
        try:
            self.assertEqual(discount_case10, discount_calculate3)
            print u"promotion works! disount_case10=(price_wayne*2)/10"
        except AssertionError as e:
            print u"promotion wrong! test faild"

        #case11 remove wayne sofa and seb dining table qty +1
        print "case11: only 2 product in tag new can get discount"
        driver.find_element_by_xpath(
            "(//*[@data-selenium='cart-item-remove'])[3]").click()
        sleep(3)
        driver.find_element_by_xpath(
            '(//*[@data-selenium="cart-item-plus"])[2]').click()
        sleep(3)
        discount_case11_text = driver.find_element_by_xpath(
            "(//*[@class='_1H_YbK__wrapper'])[3]//span").text
        discount_case11 = Decimal(sub(r'[^\d.]', '', discount_case11_text))
        discount_calculate4 = (price_seb * 2) / 10
        try:
            self.assertEqual(discount_case11, discount_calculate4)
            print u"promotion works! only 2 sub dining table get discount"
        except AssertionError as e:
            print u"promotion wrong! test"

        #清空cart
        driver.find_element_by_xpath(
            "//*[@data-selenium='cart-item-remove']").click()
        sleep(3)
        driver.find_element_by_xpath(
            "//*[@data-selenium='cart-item-remove']").click()
        sleep(3)
        driver.find_element_by_css_selector(
            "button.ZncuLx__close.btn > svg").click()

        #去后台关掉promotion
        driver.get("https://knight-stag.castlery.sg/spree/admin/promotions")
        sleep(5)
        driver.find_element_by_xpath("//*[@id='q_name_cont']").send_keys(
            "auto test 2")
        driver.find_element_by_xpath("//*[@class='button']").click()
        sleep(3)
        driver.find_element_by_xpath("//a[@data-action='edit']").click()
        sleep(5)
        driver.find_element_by_id("promotion_status").click()
        driver.find_element_by_name("button").click()