Example #1
0
    def test_google_rating(self):
        web_stag.sg_stag_random_url(self)
        web_stag.stag_authentication(self)
        #web_stag.au_stag_random_url(self)

        # 关掉客服对话框,但是如果跑au_stag的话需要注释掉这条
        web_stag.initial_web(self)

        # add to cart and chekcout
        web_stag.add_to_cart_and_checkout(self)

        # 从account.csv中随机选择一个用户登录。可以拆分成20个文件或用循环来用每个用户下单
        # web_stag.login_user(self,1) 从1-20拆分成20个文件依次跑每个user,比较稳定,网络出问题可以随时补跑
        user_account_index = random.randint(1, 20)
        web_stag.login_user(self, user_account_index)

        # 点击continue去支付
        web_stag.address_and_shipment(self)

        # 这里只用paypal支付,其他支付方式会导致最后的iframe index变化
        payment_web.payment_paypal(self)

        #accept google review
        web_stag.click_accept_review(self)
        print "use account " + str(user_account_index) + " placed an order"
    def test_stag_order(self):
        driver = self.driver
        driver.get("https://stag.castlery.com")
        # stag 验证
        web_stag.stag_authentication(self)
        # stag 身份验证以及关掉各种弹窗
        web_stag.initial_web(self)

        # 打开all sofas 中的第一个产品
        above = driver.find_element_by_link_text("Furniture")
        ActionChains(driver).move_to_element(above).perform()
        driver.find_element_by_link_text("All Sofas").click()
        sleep(10)
        driver.find_element_by_xpath("//a[@class='_1Dt6JI__name']").click()
        sleep(5)

        # add to cart and chekcout
        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)

        # login user
        web_stag.login_web(self)

        # 点击continue去支付
        driver.find_element_by_xpath(
            "//*[@data-selenium='checkout-shipping-address']").click()
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='checkout-shipping-method']").click()
        sleep(5)

        # 随机选择一种支付方式,在instalment中也会随机选择一种银行卡支付
        payment_method = random.randint(1, 3)
        if payment_method == 1:
            print "pay with credit card"
            payment_web.payment_credit_card(self)
        elif payment_method == 2:
            print "pay with paypal "
            payment_web.payment_paypal(self)
        elif payment_method == 3:
            print "pay with instlment"
            payment_web.payment_instalment(self)
        # logout
        #web_stag.logout_web(self)
        sleep(10)
    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_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)
    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()
Example #6
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()
    def test_stag_order(self):
        driver = self.driver
        driver.get("https://stag.castlery.com")
        # stag 验证
        web_stag.stag_authentication(self)
        # stag 身份验证以及关掉各种弹窗
        web_stag.initial_web(self)

        # 打开all sofas 中的第一个产品
        above = driver.find_element_by_link_text("Furniture")
        ActionChains(driver).move_to_element(above).perform()
        driver.find_element_by_link_text("All Sofas").click()
        sleep(10)
        #driver.find_element_by_xpath("//div[@class='_2qSCjD']//div[1]//a[2]").click()
        driver.find_element_by_xpath(
            "(//*[@data-selenium='category-product'])[1]").click()
        sleep(5)

        # add to swatch group 1 to cart and chekcout
        web_stag.add_swatch_group1(self)

        #只能加3个swatch,3个以后没有add to cart button了
        print "case1: if have 3 swatchs in cart, chose the new one,then can't find add to cart button"
        #driver.find_element_by_xpath("//div[@class='_20Msl6']//div[5]//div[1]//div[1]//div[2]//div[1]//button[1]").click()
        driver.find_element_by_xpath(
            "//img[@alt='Swatch, Warm Brown']").click()
        sleep(5)
        text_on_cart = driver.find_element_by_xpath(
            "//*[@data-selenium='swatch-modal-button']").text
        text_alert1 = "Up To 3 Swatches"
        try:
            self.assertEqual(text_on_cart, text_alert1)
            print "can only add 3 swatches to cart,case1 passed"
        except AssertionError as e:
            print "something wrong! case1 failed"
        #关掉swatch选择框,打开cart
        driver.find_element_by_xpath(
            "//button[@class='_2n3TEM__close btn']").click()
        sleep(3)
        driver.find_element_by_xpath(
            "//*[@data-selenium='header-cart']").click()
        sleep(5)
        # add chekcout
        #driver.find_element_by_link_text("Checkout").click()
        driver.find_element_by_xpath("//*[@data-selenium='check-out']").click()
        sleep(5)
        # login user
        web_stag.login_web(self)
        #logout user
        driver.get(
            "https://stag.castlery.com/products/wayne-2-seater-sofa?quantity=1&material=stone_grey"
        )
        sleep(5)
        above_account = driver.find_element_by_xpath(
            "//*[@data-selenium='header-account']")
        ActionChains(driver).move_to_element(above_account).perform()
        driver.find_element_by_link_text("Log Out").click()
        sleep(5)
        #add swatch group 2 to cart
        driver.find_element_by_xpath(
            "//span[contains(text(),'Custom-Made')]").click()
        sleep(5)
        driver.find_element_by_xpath(
            "//button[contains(text(),'Free Fabric Swatch >')]").click()
        sleep(5)
        #tiffany blue
        driver.find_element_by_xpath(
            "//img[@alt='Swatch, Tiffany Blue']").click()
        sleep(5)
        swatch_color1 = driver.find_element_by_xpath(
            "//p[@class='_1K7ola__intro__name']").text
        driver.find_element_by_xpath(
            "//*[@data-selenium='swatch-modal-button']").click()
        sleep(5)
        #canary yellow
        driver.find_element_by_xpath(
            "//img[@alt='Swatch, Canary Yellow']").click()
        sleep(5)
        swatch_color2 = driver.find_element_by_xpath(
            "//p[@class='_1K7ola__intro__name']").text
        driver.find_element_by_xpath(
            "//*[@data-selenium='swatch-modal-button']").click()
        sleep(5)
        #iron grey
        driver.find_element_by_xpath("//img[@alt='Swatch, Iron Grey']").click()
        sleep(5)
        swatch_color3 = driver.find_element_by_xpath(
            "//p[@class='_1K7ola__intro__name']").text
        driver.find_element_by_xpath(
            "//*[@data-selenium='swatch-modal-button']").click()
        sleep(5)
        #关掉swatch 打开cart
        driver.find_element_by_xpath(
            "//button[@class='_2n3TEM__close btn']").click()
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='header-cart']").click()
        sleep(5)
        # add chekcout
        driver.find_element_by_xpath("//*[@data-selenium='check-out']").click()
        sleep(5)
        # login user
        web_stag.login_web(self)
        #go back and check swatch qty in cart
        driver.get(
            "https://stag.castlery.com/products/wayne-2-seater-sofa?quantity=1&material=stone_grey"
        )
        sleep(5)
        #这时候因改只有3个swatch在cart中,并且因改和swatch group2中的一致
        print "case2: there are 3 swatchs in cart"
        try:
            driver.find_element_by_xpath(
                "//div[@class='ombnOy']//div//div[@class='_3Kbabv']//span[contains(text(),'3')]"
            ).click()
            swatchs_qty_3 = True
        except:
            swatchs_qty_3 = False
        if swatchs_qty_3 == True:
            print "Right, maximum 3 swatchs"
        elif swatchs_qty_3 == False:
            print "Wrong, need to check swatch in cart logic"
        #打开cart检查产品颜色否是在swatch_group2中的那些颜色
        driver.find_element_by_xpath(
            "//*[@data-selenium='header-cart']").click()
        sleep(5)

        print "case3: swatch, tiffany blau should exist in cart"
        swatch1_in_cart = driver.find_element_by_xpath(
            "//div[contains(text(),'Swatch, Tiffany Blue')]").text
        swatch_name1 = "Swatch" + "," + " " + swatch_color1
        try:
            self.assertEqual(swatch_name1, swatch1_in_cart)
            print u"swatch, tiffany blue in cart, case3 passed"
        except AssertionError as e:
            print u"case3 failed"

        print "case4: swatch, canary yellow should exist in cart"
        swatch2_in_cart = driver.find_element_by_xpath(
            "//div[contains(text(),'Swatch, Canary Yellow')]").text
        swatch_name2 = "Swatch" + "," + " " + swatch_color2
        try:
            self.assertEqual(swatch_name2, swatch2_in_cart)
            print u"swatch, canary yellow in cart, case4 passed"
        except AssertionError as e:
            print u"case4 failed"

        print "case5: swatch, iron gray should exist in cart"
        swatch3_in_cart = driver.find_element_by_xpath(
            "//div[contains(text(),'Swatch, Iron Grey')]").text
        swatch_name3 = "Swatch" + "," + " " + swatch_color3
        try:
            self.assertEqual(swatch_name3, swatch3_in_cart)
            print u"swatch, iron gray in cart, case5 passed"
        except AssertionError as e:
            print u"case5 failed"
        #checkout
        driver.find_element_by_xpath("//*[@data-selenium='check-out']").click()
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='checkout-shipping-address']").click()
        sleep(5)
        driver.find_element_by_xpath(
            "//*[@data-selenium='checkout-shipping-method']").click()
        sleep(5)
    def test_review(self):
        driver = self.driver
        driver.get("https://pos-stag.castlery.sg/")

        #login pos
        pos_stag.login_pos(self)

        #pos上下单
        pos_stag.search_and_add_product_to_cart_pos(self)
        pos_stag.search_and_add_customer_testreview_pos(self)
        pos_stag.checkout_and_payment_pos(self)

        #去web上的account中check账户下的order以及是否有新review
        driver.get("https://stag.castlery.com")
        web_stag.stag_authentication(self)
        web_stag.initial_web(self)
        web_stag.login_account(self)
        driver.find_element_by_xpath(
            "//*[@data-selenium='header-account']").click()
        sleep(1)
        driver.find_element_by_link_text("Orders").click()
        sleep(1)

        #case1: if order's shipping status not equal 'shipped', should have not review button
        print "case1 start"
        new_order_shipping_status = driver.find_element_by_xpath(
            "(//td[@rowspan='1'])[1]//span").text
        shipping_status_text = "Estimated Deliver Date"
        if new_order_shipping_status == shipping_status_text:
            print "case1: shippment not finished"
            try:
                driver.find_element_by_xpath(
                    "(//td[@rowspan='1'])[1]//a[contains(text(),'Review')]")
                review = True
            except:
                review = False
            if review == True:
                print "case1: failed"
            elif review == False:
                print "case: passed"
        else:
            print "case1: shippment finished, something wrong! need check order status"

        #去后台改变order的shipment status,只有状态为delivered的order才能review
        driver.get("https://knight-stag.castlery.sg/spree/admin/orders")
        sleep(3)
        knight_stag.login_knight(self)
        knight_stag.edit_order_shipment_to_delivered(self)
        #case2:此时shipment status因该变成了delivered
        print "case2 start"
        shipment_status_order = driver.find_element_by_xpath(
            "//dd[@id='shipment_status']/span").text
        try:
            self.assertEqual(shipment_status_order, "delivered")
            print "shipment status is correct case2 passed"
        except AssertionError as e:
            print "something wrong, need check order, case2 failed"

        #回到website account/orders ,这时候shipping status变成shipped,并且下面会出现review button
        driver.get("https://stag.castlery.com/account/orders")
        sleep(2)
        #case3: shipping status 变成 shipped
        print "case3 start"
        delivered_order_shipping_status = driver.find_element_by_xpath(
            "(//td[@rowspan='1'])[1]/div").text
        try:
            self.assertEqual(delivered_order_shipping_status, "Shipped")
            print "shipping status is correct, case3 passed"
        except AssertionError as e:
            print "something wrong, need to check order, case3 failed"
        #case4: review button 出现了
        print "case4 start"
        review_button_text = driver.find_element_by_xpath(
            "(//td[@rowspan='1'])[1]/a").text
        try:
            self.assertEqual(review_button_text, "Review")
            print "there is review button now, case4 passed"
        except AssertionError as e:
            print "something wrong, need to check, case4 failed"

        #点击review button进入review page,并对产品进行review
        driver.find_element_by_xpath("(//td[@rowspan='1'])[1]/a").click()
        sleep(1)
        #case5: 确认review page 可以看见标题 "Review And Be Rewarded"
        print "case5 start"
        review_page_title = driver.find_element_by_xpath(
            "//div[@class='container-small']//h1").text
        try:
            self.assertEqual(review_page_title, "Review And Be Rewarded")
            print "case5 passed"
        except AssertionError as e:
            print "wrong page, case5 failed"
        #提示超过50个字符的评论可以给$10,验证写49个字符无变化,再+1个字符$0 变成$10
        ran_str_49 = ''.join(
            random.sample(string.ascii_letters + string.digits, 49))
        ran_str_1 = ''.join(
            random.sample(string.ascii_letters + string.digits, 1))
        #case6:当输入49个字符的时候还是$0
        print "case6 start"
        driver.find_element_by_xpath("//textarea").send_keys(ran_str_49)
        voucher_49_characters_text = driver.find_element_by_xpath(
            "(//span)[12]").text
        try:
            self.assertEqual(voucher_49_characters_text, '$0')
            print "case6 passed"
        except AssertionError as e:
            print "something wrong! need to check"
        #多加一个字符,到了50字符后voucher会变成$10
        print "case7 start"
        driver.find_element_by_xpath("//textarea").send_keys(ran_str_1)
        voucher_50_characters_text = driver.find_element_by_xpath(
            "(//span)[12]").text
        try:
            self.assertEqual(voucher_50_characters_text, '$10')
            print "case7 paased"
        except AssertionError as e:
            print "something wrong! need to check"

        #清空text开始添加图片
        driver.find_element_by_xpath("//textarea").clear()
        sleep(1)
        web_stag.add_review_image(self)
        sleep(5)
        #case8 没text的情况下上传图片后voucher依然是$0
        print "case8 start"
        voucher_image_text = driver.find_element_by_xpath("(//span)[12]").text
        try:
            self.assertEqual(voucher_image_text, '$0')
            print "case8 passed"
        except AssertionError as e:
            print "case8 failed"
        #case9 有图片并且有50个字符text的情况下,会显示voucher为$30
        ran_str_50 = ''.join(
            random.sample(string.ascii_letters + string.digits, 50))
        driver.find_element_by_xpath("//textarea").send_keys(ran_str_50)
        sleep(1)
        voucher_50charactersandimage_text = driver.find_element_by_xpath(
            "(//span)[12]").text
        print "case9 start"
        try:
            self.assertEqual(voucher_50charactersandimage_text, "$30")
            print "case9 passed"
        except AssertionError as e:
            print "case9 failed"
        #case10 必须选一个评分才能提交review,不勾选的话会报错
        print "case10 start"
        driver.find_element_by_xpath(
            "//button[contains(text(),'Submit Your Review')]").click()
        sleep(2)
        try:
            driver.find_element_by_xpath(
                "//p[contains(text(),'Please give a rating for reviewed products.')]"
            )
            review_alert = True
        except:
            review_alert = False
        if review_alert == True:
            print "case10 passed"
        elif review_alert == False:
            print "case10 failed"
        #点掉alert,去给review勾选评分
        driver.find_element_by_xpath("//a[contains(text(),'Got it')]").click()
        sleep(1)
        driver.find_element_by_xpath("(//button)[5]").click()
        sleep(1)
        driver.find_element_by_xpath(
            "//button[contains(text(),'Submit Your Review')]").click()
        sleep(1)
        #review完成,可以去account/review页面下查看或是编辑review
        driver.get("https://stag.castlery.com/account/reviews")
        sleep(2)
        driver.find_element_by_xpath(
            "//button[contains(text(),'Edit Review')]").click()
        sleep(2)
        #这里随机选择一个评分,方便后面判断只有大于4星的评分才能出现在product page
        star_xpath = random.randint(5, 9)
        xpath_star_chose = "(//button)" + "[" + str(star_xpath) + "]"
        driver.find_element_by_xpath(xpath_star_chose).click()
        ran_str_58 = ''.join(
            random.sample(string.ascii_letters + string.digits, 58))
        review_test_text = "testreview" + "+" + ran_str_58
        driver.find_element_by_xpath("//textarea").clear()
        driver.find_element_by_xpath("//textarea").send_keys(review_test_text)
        #移除之前的图片
        driver.find_element_by_xpath("(//button)[10]").click()
        sleep(1)
        #加上新的图片
        web_stag.add_review_image(self)
        sleep(5)
        driver.find_element_by_xpath(
            "//button[contains(text(),'Update Review')]").click()
        sleep(1)

        #现在要去后端审核review了
        driver.get("https://knight-stag.castlery.sg/spree/admin/reviews")
        sleep(5)
        #选择编辑第一个review
        driver.find_element_by_xpath(
            "(//tr/td[10]/a[@data-action='edit'])[1]").click()
        sleep(2)
        knight_stag.edit_review_to_approved(self)
        #获取一下编辑后的review状态,rating, incentive,voucher_code
        rating_product = driver.find_element_by_xpath("(//tr/td[6])[1]").text
        review_incentive = driver.find_element_by_xpath("(//tr/td[9])[1]").text
        #去promotion那边找到最新voucher code,并记录下来,这里的url只有在数据库改变后才需要改动
        if review_incentive == "$10 OFF":
            driver.get(
                "https://knight-stag.castlery.sg/spree/admin/promotions/219/edit"
            )
        elif review_incentive == "$30 OFF":
            driver.get(
                "https://knight-stag.castlery.sg/spree/admin/promotions/220/edit"
            )
        elif review_incentive == "$50 OFF":
            driver.get(
                "https://knight-stag.castlery.sg/spree/admin/promotions/221/edit"
            )
        sleep(2)
        driver.find_element_by_xpath(
            "//a[contains(text(),'Promotion Code List')]").click()
        sleep(1)
        voucher_code = driver.find_element_by_xpath("//tr[1]/td[2]").text

        #去web上查看review,会发现review已经变成approved,并且没法编辑review了
        driver.get("https://stag.castlery.com/account/reviews")
        sleep(3)
        #case11 现在最新的一个review因改是approved的,这里暂时只能用绝对路径表示最新的review的状态,等以后加了相应定位元素再改
        review_status = driver.find_element_by_xpath(
            "/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/span[1]"
        ).text
        print "case11 start"
        try:
            self.assertEqual(review_status, "Approved")
            print "case11 passed"
        except AssertionError as e:
            print "case11 failed"
        #case12 现在review页面因改不存在edit reveiw的button了
        try:
            driver.find_element_by_xpath(
                "//button[contains(text(),'Edit Review')]")
            edit_review_button = True
        except:
            edit_review_button = False
        if edit_review_button == True:
            print "case12 failed"
        elif edit_review_button == False:
            print "case12 passed"
        # 去vouchers 页面核对vouchers code
        driver.get("https://stag.castlery.com/account/vouchers")
        sleep(1)

        #case13 review_incentive 因改在这里打印出来,并且要和后端给的数值一样
        print "case13 start"
        #如果class变动就用下面绝对路径
        # driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[4]").text
        coupon_div_text = driver.find_element_by_xpath(
            "//div[@class='_2Malad__coupon__value']").text
        coupon_div_text_value = Decimal(sub(r'[^\d.]', '', coupon_div_text))
        review_incentive_value = Decimal(sub(r'[^\d.]', '', review_incentive))
        try:
            self.assertEqual(coupon_div_text_value, review_incentive_value)
            print "case13 passed"
        except AssertionError as e:
            print "case13 failed"

        #case14 voucher code也会在这里打印出来,并且要和后端permeation code list中获取的一样
        print "case14 start"
        #如果class变动用下面绝对路径
        # driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[5]").text
        coupon_code_div = driver.find_element_by_xpath(
            "//div[@class='_2Malad__coupon__code']").text
        coupon_code = coupon_code_div.split()[-1]
        try:
            self.assertEqual(coupon_code, voucher_code)
            print "case14 passed"
        except AssertionError as e:
            print "case14 failed"

        #这里可以加一个case 用来判断voucher code的使用期限,但是这里时间是固定60天,也没法验证是否确实是60天过期,只是前端打印数值

        #现在需要去review产品页面确认已经approved的review以及其内容(可以从review中的产品链接点击跳转,但是这里明确产品,所以直接访问url)
        driver.get(
            "https://stag.castlery.com/products/bambu-armchair?quantity=1&material=dove_grey"
        )
        sleep(3)
        #使用js聚焦元素,目标是customer reviews,直接用js先跳转到这里
        target = driver.find_element_by_xpath("(//h2)[2]")
        driver.execute_script("arguments[0].scrollIntoView();", target)
        sleep(1)
        #case15 ranting大于等于4,这里将会找到刚完成的review,这里只简单用review的text来判断是否是同一个review
        print "case15 start"
        review_comment = driver.find_element_by_xpath(
            "/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[4]/div[3]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/p[1]"
        ).text
        if int(rating_product) >= 4:
            try:
                self.assertEqual(review_comment, review_test_text)
                print "case15 passed"
            except AssertionError as e:
                print "case15 failed"
        else:
            try:
                self.assertEqual(review_comment, review_test_text)
                print "case15 failed, ranting < 4 review should not exist in product page"
            except AssertionError as e:
                print "case15 passed, ranting < 4 review not exsit in product page"

        #最后在pos上下单并且使用这个voucher code
        driver.get("https://pos-stag.castlery.sg/")
        sleep(3)
        pos_stag.search_and_add_product_to_cart_pos(self)
        pos_stag.search_and_add_customer_posorder_pos(self)
        driver.find_element_by_xpath(
            "//label[contains(text(),'Add Coupon Code')]").click()
        sleep(1)
        driver.find_element_by_name("coupon_code").send_keys(coupon_code +
                                                             Keys.ENTER)
        sleep(2)
        pos_stag.checkout_and_payment_pos(self)

        #完成订单并且使用掉code后去网站检查因改已经找不到这个code了
        driver.get("https://stag.castlery.com/account/vouchers")
        print "case16 start"
        try:
            driver.find_element_by_xpath(
                "//div[@class='_2Malad__coupon__code']")
            voucher_code_exist = True
        except:
            voucher_code_exist = False
        if voucher_code_exist == True:
            print "case16 failed"
        elif voucher_code_exist == False:
            print "case16 passed"