Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductPage.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Test\Page\Product\CatalogProductView;
10 use Magento\Mtf\Client\BrowserInterface;
12 use Magento\Mtf\Fixture\FixtureInterface;
13 
19 {
25  protected $productView;
26 
32  protected $product;
33 
37  protected $pageView;
38 
53  public function processAssert(
54  BrowserInterface $browser,
55  CatalogProductView $catalogProductView,
56  FixtureInterface $product
57  ) {
58  $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
59 
60  $this->product = $product;
61  $this->pageView = $catalogProductView;
62  $this->productView = $catalogProductView->getViewBlock();
63 
64  $errors = $this->verify();
65  \PHPUnit\Framework\Assert::assertEmpty(
66  $errors,
67  "\nFound the following errors:\n" . implode(" \n", $errors)
68  );
69  }
70 
76  protected function verify()
77  {
78  $errors = [];
79 
80  $errors[] = $this->verifyName();
81  $errors[] = $this->verifyPrice();
82  $errors[] = $this->verifySpecialPrice();
83  $errors[] = $this->verifySku();
84  $errors[] = $this->verifyDescription();
85  $errors[] = $this->verifyShortDescription();
86 
87  return array_filter($errors);
88  }
89 
95  protected function verifyName()
96  {
97  $expectedName = $this->product->getName();
98  try {
99  $actualName = $this->productView->getProductName();
100  } catch (\PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
101  return "Could not find product '{$this->product->getName()}' name on the page.\n" . $e->getMessage();
102  }
103 
104  if ($expectedName == $actualName) {
105  return null;
106  }
107  return "Product name on Storefront product '{$this->product->getName()}' page is unexpected. "
108  . "Actual: {$actualName}, expected: {$expectedName}.";
109  }
110 
116  protected function verifyPrice()
117  {
118  if ($this->product->hasData('price') == false) {
119  return null;
120  }
121 
122  $priceBlock = $this->productView->getPriceBlock();
123  if (!$priceBlock->isVisible()) {
124  return "Price block for '{$this->product->getName()}' product' is not visible.";
125  }
126  $actualPrice = $priceBlock->isOldPriceVisible() ? $priceBlock->getOldPrice() : $priceBlock->getPrice();
127  $expectedPrice = number_format($this->product->getPrice(), 2, '.', '');
128 
129  if ($expectedPrice != $actualPrice) {
130  return "Displayed product price on Storefront product '{$this->product->getName()}' page is unexpected. "
131  . "Actual: {$actualPrice}, expected: {$expectedPrice}.";
132  }
133  return null;
134  }
135 
141  protected function verifySpecialPrice()
142  {
143  if (!$this->product->hasData('special_price')) {
144  return null;
145  }
146  $expectedSpecialPrice = $this->product->getSpecialPrice();
147  $expectedSpecialPrice = number_format($expectedSpecialPrice, 2);
148  $priceBlock = $this->productView->getPriceBlock($this->product);
149  if (!$priceBlock->isVisible()) {
150  return "Price block for '{$this->product->getName()}' product' is not visible.";
151  }
152  $actualSpecialPrice = $priceBlock->getSpecialPrice();
153  if ($expectedSpecialPrice == $actualSpecialPrice) {
154  return null;
155  }
156  return "Displayed product special price on Storefront product '{$this->product->getName()}' page is unexpected."
157  . "Actual: {$actualSpecialPrice}, expected: {$expectedSpecialPrice}.";
158  }
159 
165  protected function verifySku()
166  {
167  $expectedSku = $this->product->getSku();
168  try {
169  $actualSku = $this->productView->getProductSku();
170  } catch (\PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
171  return "Could not find product {$this->product->getName()}' SKU on the page.\n" . $e->getMessage();
172  }
173 
174  if ($expectedSku === null || $expectedSku == $actualSku) {
175  return null;
176  }
177  return "Displayed product SKU on Storefront product '{$this->product->getName()}' page is unexpected. "
178  . "Actual: {$actualSku}, expected: {$expectedSku}.";
179  }
180 
186  protected function verifyDescription()
187  {
188  $expectedDescription = $this->product->getDescription();
189  $actualDescription = $this->productView->getProductDescription();
190 
191  if ($expectedDescription === null || $expectedDescription == $actualDescription) {
192  return null;
193  }
194  return "Displayed product description on Storefront product '{$this->product->getName()}' page is unexpected. "
195  . "Actual: {$actualDescription}, expected: {$expectedDescription}.";
196  }
197 
203  protected function verifyShortDescription()
204  {
205  $expected = $this->product->getShortDescription();
206  $actual = $this->productView->getProductShortDescription();
207 
208  if ($expected === null || $expected == $actual) {
209  return null;
210  }
211  return "Displayed short description on Storefront product '{$this->product->getName()}' page is unexpected. "
212  . "Actual: {$actual}, expected: {$expected}.";
213  }
214 
220  public function toString()
221  {
222  return 'Product on product view page is correct.';
223  }
224 }
processAssert(BrowserInterface $browser, CatalogProductView $catalogProductView, FixtureInterface $product)
$errors
Definition: overview.phtml:9