Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductAddedToCartFromSearchResults.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Constraint\AbstractConstraint;
10 use Magento\CatalogSearch\Test\Page\AdvancedResult;
11 use Magento\Catalog\Test\Page\Product\CatalogProductView;
13 
17 class AssertProductAddedToCartFromSearchResults extends AbstractConstraint
18 {
22  const SUCCESS_MESSAGE = 'You added %s to your shopping cart.';
23 
32  public function processAssert(
33  CatalogSearchQuery $catalogSearch,
34  AdvancedResult $resultPage,
35  CatalogProductView $catalogProductView
36  ) {
37  $product = $catalogSearch->getDataFieldConfig('query_text')['source']->getFirstProduct();
38 
39  do {
40  $isProductVisible = $resultPage->getListProductBlock()->getProductItem($product)->isVisible();
41  } while (!$isProductVisible && $resultPage->getBottomToolbar()->nextPage());
42 
43  $productName = $product->getName();
44 
45  \PHPUnit\Framework\Assert::assertTrue($isProductVisible, "A product with name $productName was not found.");
46  $resultPage->getListProductBlock()->getProductItem($product)->clickAddToCart();
47  $catalogProductView->getViewBlock()->waitLoader();
48  if (isset($product->getCheckoutData()['options'])) {
49  $catalogProductView->getViewBlock()->addToCart($product);
50  $message = $catalogProductView->getMessagesBlock()->getSuccessMessage();
51  } else {
52  $message = $resultPage->getMessagesBlock()->getSuccessMessage();
53  }
54 
55  \PHPUnit\Framework\Assert::assertEquals(
56  sprintf(self::SUCCESS_MESSAGE, $productName),
57  $message
58  );
59  }
60 
66  public function toString()
67  {
68  return 'Product was successfully added to cart from the search results page.';
69  }
70 }
processAssert(CatalogSearchQuery $catalogSearch, AdvancedResult $resultPage, CatalogProductView $catalogProductView)
$message