Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddProductsToTheCartStep.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Test\Page\Product\CatalogProductView;
10 use Magento\Checkout\Test\Page\CheckoutCart;
11 use Magento\Cms\Test\Page\CmsIndex;
12 use Magento\Mtf\Client\BrowserInterface;
13 use Magento\Mtf\Fixture\FixtureFactory;
14 use Magento\Mtf\TestStep\TestStepInterface;
15 
19 class AddProductsToTheCartStep implements TestStepInterface
20 {
26  private $products;
27 
33  private $catalogProductView;
34 
40  private $checkoutCart;
41 
47  private $cmsIndex;
48 
54  private $browser;
55 
61  private $fixtureFactory;
62 
68  private $loadingSelector = '.loading-mask';
69 
75  private $isValidationFailed;
76 
86  public function __construct(
87  CatalogProductView $catalogProductView,
88  CheckoutCart $checkoutCart,
89  CmsIndex $cmsIndex,
90  BrowserInterface $browser,
91  FixtureFactory $fixtureFactory,
92  array $products,
93  $isValidationFailed = false
94  ) {
95  $this->catalogProductView = $catalogProductView;
96  $this->checkoutCart = $checkoutCart;
97  $this->cmsIndex = $cmsIndex;
98  $this->browser = $browser;
99  $this->fixtureFactory = $fixtureFactory;
100  $this->products = $products;
101  $this->isValidationFailed = $isValidationFailed;
102  }
103 
109  public function run()
110  {
111  // Ensure that shopping cart is empty
112  $this->checkoutCart->open()->getCartBlock()->clearShoppingCart();
113 
114  foreach ($this->products as $product) {
115  $this->browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
116  $this->catalogProductView->getViewBlock()->waitForElementNotVisible($this->loadingSelector);
117  $this->catalogProductView->getViewBlock()->addToCart($product);
118 
119  if ($this->isValidationFailed) {
120  $this->catalogProductView->getCustomOptionsBlock()->waitValidationErrorMessage();
121  } else {
122  $this->catalogProductView->getMessagesBlock()->waitSuccessMessage();
123  }
124  }
125  $cart['data']['items'] = ['products' => $this->products];
126  sleep(10);
127  return ['cart' => $this->fixtureFactory->createByCode('cart', $cart)];
128  }
129 }
__construct(CatalogProductView $catalogProductView, CheckoutCart $checkoutCart, CmsIndex $cmsIndex, BrowserInterface $browser, FixtureFactory $fixtureFactory, array $products, $isValidationFailed=false)