<?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="StorefrontGoToDetailsPageWhenAddingToCartTest">
        <annotations>
            <features value="Bundle"/>
            <stories value="Bundle products list on Storefront"/>
            <title value="Customer should be taken to bundle product details page when clicking “Add to Cart” button"/>
            <description value="Customer should be taken to bundle product details page when clicking “Add to Cart” button"/>
            <severity value="CRITICAL"/>
            <testCaseId value="MC-229"/>
            <group value="Bundle"/>
        </annotations>
        <before>
            <actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
            <createData entity="SimpleProduct2" stepKey="simpleProduct1"/>
            <createData entity="SimpleProduct2" stepKey="simpleProduct2"/>
            <createData entity="_defaultCategory" stepKey="createCategory"/>
            <actionGroup ref="CliIndexerReindexActionGroup" stepKey="runCronIndex">
                <argument name="indices" value=""/>
            </actionGroup>
        </before>
        <after>
            <deleteData createDataKey="simpleProduct1" stepKey="deleteSimpleProduct1"/>
            <deleteData createDataKey="simpleProduct2" stepKey="deleteSimpleProduct2"/>
            <deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
            <actionGroup ref="DeleteProductBySkuActionGroup" stepKey="deleteBundleProduct">
                <argument name="sku" value="{{BundleProduct.sku}}"/>
            </actionGroup>
            <actionGroup ref="ResetAdminDataGridToDefaultViewActionGroup" stepKey="clearFilter"/>
            <actionGroup ref="AdminLogoutActionGroup" stepKey="amOnLogoutPage"/>
        </after>
        <!--Go to bundle product creation page-->
        <actionGroup ref="AdminOpenNewProductFormPageActionGroup" stepKey="goToBundleProductCreationPage">
            <argument name="attributeSetId" value="{{BundleProduct.set}}"/>
            <argument name="productType" value="{{BundleProduct.type}}"/>
        </actionGroup>
        <comment userInput="Comment is added to preserve the step key for backward compatibility" stepKey="waitForBundleProductCreatePageToLoad"/>

        <!--Categories-->
        <click selector="{{AdminProductFormBundleSection.categoriesDropDown}}" stepKey="dropDownCategories"/>
        <fillField selector="{{AdminProductFormBundleSection.searchForCategory}}" userInput="$$createCategory.name$$" stepKey="searchForCategory"/>
        <click selector="{{AdminProductFormBundleSection.selectCategory}}" stepKey="selectCategory"/>
        <click selector="{{AdminProductFormBundleSection.categoriesLabel}}" stepKey="clickOnCategoriesLabelToCloseOptions"/>

        <!--Create bundle product-->
        <conditionalClick selector="{{AdminProductFormBundleSection.bundleItemsToggle}}" dependentSelector="{{AdminProductFormBundleSection.bundleItemsToggle}}" visible="false" stepKey="conditionallyOpenSectionBundleItems"/>
        <actionGroup ref="AdminClickAddOptionForBundleItemsActionGroup" stepKey="clickAddOption"/>
        <waitForElementVisible selector="{{AdminProductFormBundleSection.bundleOptionXTitle('0')}}" stepKey="waitForBundleOptions"/>
        <fillField selector="{{AdminProductFormBundleSection.bundleOptionXTitle('0')}}" userInput="{{BundleProduct.optionTitle1}}" stepKey="fillOptionTitle"/>
        <selectOption selector="{{AdminProductFormBundleSection.bundleOptionXInputType('0')}}" userInput="{{BundleProduct.optionInputType1}}" stepKey="selectInputType"/>
        <actionGroup ref="AdminClickAddProductToOptionActionGroup" stepKey="clickAddProductsToOption"/>
        <actionGroup ref="FilterProductGridBySkuActionGroup" stepKey="filterBundleProductOptions">
            <argument name="product" value="$$simpleProduct1$$"/>
        </actionGroup>
        <actionGroup ref="AdminCheckFirstCheckboxInAddProductsToOptionPanelGridActionGroup" stepKey="selectFirstGridRow"/>
        <actionGroup ref="FilterProductGridBySkuActionGroup" stepKey="filterBundleProductOptions2">
            <argument name="product" value="$$simpleProduct2$$"/>
        </actionGroup>
        <actionGroup ref="AdminCheckFirstCheckboxInAddProductsToOptionPanelGridActionGroup" stepKey="selectFirstGridRow2"/>
        <click selector="{{AdminAddProductsToOptionPanel.addSelectedProducts}}" stepKey="clickAddSelectedBundleProducts"/>
        <actionGroup ref="AdminFillBundleItemQtyActionGroup" stepKey="fillProductDefaultQty1">
            <argument name="optionIndex" value="0"/>
            <argument name="productIndex" value="0"/>
            <argument name="qty" value="{{BundleProduct.defaultQuantity}}"/>
        </actionGroup>

        <actionGroup ref="AdminFillBundleItemQtyActionGroup" stepKey="fillProductDefaultQty2">
            <argument name="optionIndex" value="0"/>
            <argument name="productIndex" value="1"/>
            <argument name="qty" value="{{BundleProduct.defaultQuantity}}"/>
        </actionGroup>

        <actionGroup ref="AncillaryPrepBundleProductActionGroup" stepKey="createBundledProductForTwoSimpleProducts"/>

        <!--Save the product-->
        <actionGroup ref="AdminProductFormSaveActionGroup" stepKey="clickSaveButton"/>
        <seeElement selector="{{AdminCategoryMessagesSection.SuccessMessage}}" stepKey="messageYouSavedTheProductIsShown"/>

        <!--Go to category page-->
        <actionGroup ref="StorefrontOpenHomePageActionGroup" stepKey="goToHomePage"/>
        <click selector="{{StorefrontHeaderSection.NavigationCategoryByName($$createCategory.name$$)}}" stepKey="cartClickCategory"/>

        <!--Click add to cart-->
        <actionGroup ref="StorefrontHoverProductOnCategoryPageActionGroup" stepKey="hoverProduct"/>
        <actionGroup ref="StorefrontClickAddToCartButtonActionGroup" stepKey="addProductToCart"/>

        <!--Check for details page-->
        <seeInCurrentUrl url="{{BundleProduct.sku}}" stepKey="seeBundleProductDetailsPage"/>
    </test>
</tests>
