LiselectstoreElement.php 5.13 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Mtf\Client\Element;

use Magento\Mtf\Client\Locator;

/**
 * Class LiselectstoreElement
 * Typified element class for lists selectors
 */
class LiselectstoreElement extends SimpleElement
{
    /**
     * Template for each element of option
     *
     * @var string
     */
    protected $optionMaskElement = 'li[*[contains(text(), "%s")]]';

    /**
     * Additional part for each child element of option
     *
     * @var string
     */
    protected $optionMaskFollowing = '/following-sibling::';

    /**
     * Website selector
     *
     * @var string
     */
    protected $websiteSelector = '(.//li[a[@data-role="website-id"]])[%d]';

    /**
     * Store selector
     *
     * @var string
     */
    protected $storeSelector = '(.//li[@class = "store-switcher-store disabled"])[%d]';

    /**
     * StoreView selector
     *
     * @var string
     */
    protected $storeViewSelector = './/li[a[@data-role="store-view-id"]]';

    /**
     * Toggle element selector
     *
     * @var string
     */
    protected $toggleSelector = '.admin__action-dropdown[data-toggle="dropdown"]';

    /**
     * Select value in liselect dropdown
     *
     * @param string $value
     * @throws \Exception
     */
    public function setValue($value)
    {
        $this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
        $this->context->find($this->toggleSelector)->click();

        $value = explode('/', $value);
        $optionSelector = [];
        foreach ($value as $key => $option) {
            $optionSelector[] = sprintf($this->optionMaskElement, $value[$key]);
        }
        $optionSelector = './/' . implode($this->optionMaskFollowing, $optionSelector) . '/a';

        $option = $this->context->find($optionSelector, Locator::SELECTOR_XPATH);
        if (!$option->isVisible()) {
            throw new \Exception('[' . implode('/', $value) . '] option is not visible in store switcher.');
        }
        $option->click();
    }

    /**
     * Get all li elements from dropdown
     *
     * @return array
     */
    protected function getLiElements()
    {
        $this->find($this->toggleSelector)->click();
        $elements = $this->driver->getElements($this, 'li', Locator::SELECTOR_TAG_NAME);
        $dropdownData = [];
        foreach ($elements as $element) {
            $class = $element->getAttribute('class');
            $dropdownData[] = [
                'element' => $element,
                'storeView' => $this->isSubstring($class, "store-switcher-store-view"),
                'store' => $this->isSubstring($class, "store-switcher-store "),
                'website' => $this->isSubstring($class, "store-switcher-website"),
                'current' => $this->isSubstring($class, "current"),
                'default_config' => $this->isSubstring($class, "store-switcher-all"),
            ];
        }
        return $dropdownData;
    }

    /**
     * Get all available store views
     *
     * @return array
     */
    public function getValues()
    {
        $dropdownData = $this->getLiElements();
        $data = [];
        foreach ($dropdownData as $key => $dropdownElement) {
            if ($dropdownElement['storeView']) {
                $data[] = $this->findNearestElement('website', $key, $dropdownData) . "/"
                    . $this->findNearestElement('store', $key, $dropdownData) . "/"
                    . $dropdownElement['element']->getText();
            }
        }
        return $data;
    }

    /**
     * Check if string contains substring
     *
     * @param string $haystack
     * @param string $pattern
     * @return bool
     */
    protected function isSubstring($haystack, $pattern)
    {
        return preg_match("/$pattern/", $haystack) != 0 ? true : false;
    }

    /**
     * Return nearest elements name according to criteria
     *
     * @param string $criteria
     * @param string $key
     * @param array $elements
     * @return bool
     */
    protected function findNearestElement($criteria, $key, array $elements)
    {
        $elementText = false;
        while ($elementText == false) {
            $elementText = $elements[$key][$criteria] == true ? $elements[$key]['element']->getText() : false;
            $key--;
        }
        return $elementText;
    }

    /**
     * Get selected store value
     *
     * @throws \Exception
     * @return string
     */
    public function getValue()
    {
        $this->eventManager->dispatchEvent(['get_value'], [$this->getAbsoluteSelector()]);
        $elements = $this->getLiElements();
        foreach ($elements as $key => $element) {
            if ($element['current'] == true) {
                if ($element['default_config'] == true) {
                    return $element['element']->getText();
                }
                $path = $this->findNearestElement('website', $key, $elements) . "/"
                    . $this->findNearestElement('store', $key, $elements) . "/"
                    . $element['element']->getText();
                return $path;
            }
        }

        return '';
    }
}