<?xml version="1.0" encoding="UTF-8"?>
<!--
 /**
  * Copyright © Magento, Inc. All rights reserved.
  * See COPYING.txt for license details.
  */
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
    <test name="OnePageCheckoutAsCustomerUsingDefaultAddressTest">
        <annotations>
            <features value="OnePageCheckout"/>
            <stories value="OnePageCheckout within Offline Payment Methods"/>
            <title value="OnePageCheckout as customer using default address test"/>
            <description value="Checkout as customer using default address"/>
            <severity value="CRITICAL"/>
            <testCaseId value="MC-14741"/>
            <group value="checkout"/>
            <group value="mtf_migrated"/>
        </annotations>
        <before>
            <!-- Create Simple Product -->
            <createData entity="SimpleProduct2" stepKey="createSimpleProduct">
                <field key="price">560</field>
            </createData>

            <!-- Create customer -->
            <createData entity="Simple_US_Customer_Multiple_Addresses" stepKey="createCustomer"/>
        </before>
        <after>
            <!-- Admin log out -->
            <actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>

            <!-- Customer log out -->
            <actionGroup ref="StorefrontCustomerLogoutActionGroup" stepKey="customerLogout"/>

            <!-- Delete created product -->
            <deleteData createDataKey="createSimpleProduct" stepKey="deleteSimpleProduct"/>

            <!-- Delete customer -->
            <deleteData createDataKey="createCustomer" stepKey="deleteCustomer"/>
        </after>

        <!-- Add Simple Product to cart -->
        <amOnPage url="{{StorefrontProductPage.url($$createSimpleProduct.custom_attributes[url_key]$$)}}" stepKey="navigateToSimpleProductPage"/>
        <waitForPageLoad stepKey="waitForSimpleProductPageLoad"/>
        <actionGroup ref="AddToCartFromStorefrontProductPageActionGroup" stepKey="addToCartFromStorefrontProductPage">
            <argument name="productName" value="$$createSimpleProduct.name$$"/>
        </actionGroup>

        <!-- Go to shopping cart -->
        <actionGroup ref="ClickViewAndEditCartFromMiniCartActionGroup" stepKey="goToShoppingCartFromMinicart"/>
        <actionGroup ref="FillShippingZipForm" stepKey="fillShippingZipForm">
            <argument name="address" value="US_Address_CA"/>
        </actionGroup>
        <actionGroup ref="StorefrontClickProceedToCheckoutActionGroup" stepKey="clickProceedToCheckout"/>
        <comment userInput="Adding the comment to replace waitForProceedToCheckout action for preserving Backward Compatibility" stepKey="waitForProceedToCheckout"/>


        <!-- Login as customer on checkout page -->
        <actionGroup ref="LoginAsCustomerOnCheckoutPageActionGroup" stepKey="customerLogin">
            <argument name="customer" value="$createCustomer$"/>
        </actionGroup>

        <!-- Fill customer address data -->
        <waitForElementVisible selector="{{CheckoutShippingSection.shipHereButton(UK_Not_Default_Address.street[0])}}" stepKey="waitForShipHereVisible"/>
        <!-- Change address -->
        <click selector="{{CheckoutShippingSection.shipHereButton(UK_Not_Default_Address.street[0])}}" stepKey="clickShipHere"/>

        <!-- Click next button to open payment section -->
        <actionGroup ref="StorefrontGuestCheckoutProceedToPaymentStepActionGroup" stepKey="clickNext"/>
        <comment userInput="Comment is added to preserve the step key for backward compatibility" stepKey="waitForShipmentPageLoad"/>

        <!-- Select payment solution -->
        <checkOption selector="{{CheckoutPaymentSection.billingAddressNotSameCheckbox}}" stepKey="selectPaymentSolution" />

        <!-- Check order summary in checkout -->
        <comment userInput="BIC workaround" stepKey="waitForPaymentSectionLoaded"/>
        <comment userInput="BIC workaround" stepKey="waitForPlaceOrderButton"/>
        <comment userInput="BIC workaround" stepKey="clickPlaceOrderButton"/>
        <actionGroup ref="ClickPlaceOrderActionGroup" stepKey="placeOrder"/>
        <comment userInput="BIC workaround" stepKey="orderIsSuccessfullyPlaced"/>
        <comment userInput="BIC workaround" stepKey="grabOrderNumber"/>
        <actionGroup ref="StorefrontClickOrderLinkFromCheckoutSuccessPageActionGroup" stepKey="openOrderViewPage"/>
        <grabFromCurrentUrl regex="~/order_id/(\d+)/~" stepKey="grabOrderId"/>

        <!-- Login as admin -->
        <actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>

        <!-- Open created order in backend -->
        <comment userInput="BIC workaround" stepKey="goToOrders"/>
        <comment userInput="BIC workaround" stepKey="filterOrderGridById"/>
        <actionGroup ref="AdminOpenOrderByEntityIdActionGroup" stepKey="filterOrdersGridById">
            <argument name="entityId" value="{$grabOrderId}"/>
        </actionGroup>

        <!-- Assert order total -->
        <scrollTo selector="{{AdminOrderTotalSection.grandTotal}}" stepKey="scrollToOrderTotalSection"/>
        <see selector="{{AdminOrderTotalSection.grandTotal}}" userInput="$565.00" stepKey="checkOrderTotalInBackend"/>

        <!-- Assert order addresses -->
        <see selector="{{AdminShipmentAddressInformationSection.billingAddress}}" userInput="{{UK_Not_Default_Address.street[0]}}" stepKey="seeBillingAddressStreet"/>
        <see selector="{{AdminShipmentAddressInformationSection.billingAddress}}" userInput="{{UK_Not_Default_Address.city}}" stepKey="seeBillingAddressCity"/>
        <see selector="{{AdminShipmentAddressInformationSection.billingAddress}}" userInput="{{UK_Not_Default_Address.postcode}}" stepKey="seeBillingAddressPostcode"/>
        <see selector="{{AdminShipmentAddressInformationSection.shippingAddress}}" userInput="{{UK_Not_Default_Address.street[0]}}" stepKey="seeShippingAddressStreet"/>
        <see selector="{{AdminShipmentAddressInformationSection.shippingAddress}}" userInput="{{UK_Not_Default_Address.city}}" stepKey="seeShippingAddressCity"/>
        <see selector="{{AdminShipmentAddressInformationSection.shippingAddress}}" userInput="{{UK_Not_Default_Address.postcode}}" stepKey="seeShippingAddressPostcode"/>
    </test>
</tests>
