Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ViewWithSwatches.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Mtf\Client\ElementInterface;
11 use Magento\Mtf\Fixture\InjectableFixture;
12 
16 class ViewWithSwatches extends View
17 {
23  private $swatchAttributesSelector = '.swatch-attribute';
24 
30  private $swatchAttributesLabelSelector = '.swatch-attribute-label';
31 
37  private $swatchAttributeOptionsSelector = '.swatch-option';
38 
44  private $selectedSwatchAttributeSelector = '.swatch-attribute.%s .swatch-attribute-selected-option';
45 
51  public function getSwatchAttributesData()
52  {
53  $this->waitForElementVisible($this->swatchAttributesSelector);
54 
55  $swatchAttributesData = [];
56  $swatchAttributes = $this->_rootElement->getElements($this->swatchAttributesSelector);
57  foreach ($swatchAttributes as $swatchAttribute) {
58  $attributeCode = $swatchAttribute->getAttribute('attribute-code');
59  $swatchAttributesData[$attributeCode] = [
60  'attribute_code' => $attributeCode,
61  'attribute_id' => $swatchAttribute->getAttribute('attribute-id'),
62  'label' => $swatchAttribute->find($this->swatchAttributesLabelSelector)->getText(),
63  'options' => $this->getSwatchAttributeOptionsData($swatchAttribute),
64  ];
65  }
66  return $swatchAttributesData;
67  }
68 
75  private function getSwatchAttributeOptionsData(ElementInterface $swatchAttribute)
76  {
77  $optionsData = [];
78  $options = $swatchAttribute->getElements($this->swatchAttributeOptionsSelector);
79  foreach ($options as $option) {
80  $optionId = $option->getAttribute('option-id');
82  'option_id' => $optionId,
83  'label' => $option->getText(),
84  ];
85  }
86  return $optionsData;
87  }
88 
95  public function getSelectedSwatchOptions(InjectableFixture $product)
96  {
97  $checkoutData = $product->getCheckoutData();
98  $availableAttributes = $product->getConfigurableAttributesData();
99  $attributesData = $availableAttributes['attributes_data'];
100  $formData = [];
101  foreach ($checkoutData['options']['configurable_options'] as $item) {
102  $selector = sprintf(
103  $this->selectedSwatchAttributeSelector,
104  $attributesData[$item['title']]['attribute_code']
105  );
106  $this->waitForElementVisible($selector);
107  $selected = $this->_rootElement->find($selector)->getText();
108  $formData[$item['title']] = $selected;
109  }
110 
111  return $formData;
112  }
113 }
$attributesData
$attributeCode
Definition: extend.phtml:12