Completed
Push — master ( 1137b9...97c77e )
by
unknown
20s queued 11s
created

Test/Common/AbstractMg21Selenium.php (1 issue)

Severity
1
<?php
2
3
namespace DigitalOrigin\Pmt\Test\Common;
4
5
use DigitalOrigin\Pmt\Test\PaylaterMagentoTest;
6
use Facebook\WebDriver\WebDriverBy;
7
use Facebook\WebDriver\WebDriverExpectedCondition;
8
use PagaMasTarde\SeleniumFormUtils\SeleniumHelper;
9
10
/**
11
 * Class AbstractMg21Selenium
12
 * @package DigitalOrigin\Test\Common
13
 */
14
abstract class AbstractMg21Selenium extends PaylaterMagentoTest
15
{
16
    /**
17
     * @throws \Exception
18
     */
19
    public function loginToBackOffice()
20
    {
21
        $this->webDriver->get(self::MAGENTO_URL.self::BACKOFFICE_FOLDER);
22
        $emailElementSearch = WebDriverBy::id('username');
23
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($emailElementSearch);
24
        $this->webDriver->wait()->until($condition);
25
        $this->findById('username')->clear()->sendKeys($this->configuration['backofficeUsername']);
26
        $this->findById('login')->clear()->sendKeys($this->configuration['backofficePassword']);
27
        $this->findById('login-form')->submit();
28
        $emailElementSearch = WebDriverBy::className('page-wrapper');
29
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($emailElementSearch);
30
        $this->webDriver->wait()->until($condition);
31
        $this->assertTrue((bool) $condition);
32
    }
33
34
    /**
35
     * @require loginToBackOffice
36
     *
37
     * @throws \Exception
38
     */
39
    public function getPaylaterBackOffice()
40
    {
41
        $this->webDriver->get(self::MAGENTO_URL.self::BACKOFFICE_FOLDER);
42
        $this->findByLinkText('STORES')->click();
43
44
        $elementSearch = WebDriverBy::linkText('Configuration');
45
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($elementSearch);
46
        $this->webDriver->wait()->until($condition);
47
        $this->findByLinkText('Configuration')->click();
48
49
        //Confirmamos que aparece el menu de tabs
50
        $elementSearch = WebDriverBy::id('system_config_tabs');
51
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($elementSearch);
52
        $this->webDriver->wait()->until($condition);
53
54
        //Buscamos la palabra SALES dentro del div anterior
55
        $menuSearch = WebDriverBy::cssSelector("#system_config_tabs > div > div > strong");
56
        $menuElements = $this->webDriver->findElements($menuSearch);
57
        foreach ($menuElements as $menuElement) {
58
            if (strpos($menuElement->getText(), 'SALES', 0) !== false ||
59
                strpos($menuElement->getText(), 'sales', 0) !== false) {
60
                $menuElement->click();
61
                $this->assertContains('SALES', $menuElement->getText(), $menuElement->getText());
62
                break;
63
            }
64
        }
65
66
        $elementSearch = WebDriverBy::linkText('Payment Methods');
67
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($elementSearch);
68
        $this->webDriver->wait()->until($condition);
69
        $this->findByLinkText('Payment Methods')->click();
70
71
        $elementSearch = WebDriverBy::id('payment_us_other_payment_methods-head');
72
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($elementSearch);
73
        $this->webDriver->wait()->until($condition);
74
        $otherElement = $this->findById('payment_us_other_payment_methods-head');
75
        if ($otherElement->getAttribute('class')!='open') {
76
            $otherElement->click();
77
        }
78
79
        $verify = WebDriverBy::id('payment_us_paylater-head');
80
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($verify);
81
        $this->webDriver->wait()->until($condition);
82
        $this->assertTrue((bool) $condition, "PR4");
83
        $paylaterElement = $this->findById('payment_us_paylater-head');
84
        if ($paylaterElement->getAttribute('class')!='open') {
85
            $paylaterElement->click();
86
        }
87
88
        $verify = WebDriverBy::id('payment_us_paylater_active');
89
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($verify);
90
        $this->webDriver->wait()->until($condition);
91
    }
92
93
    /**
94
     * @throws \Exception
95
     */
96
    public function createAccount()
97
    {
98
        $this->webDriver->get(self::MAGENTO_URL);
99
        $loginButtonSearch = WebDriverBy::linkText('Create an Account');
100
        $condition = WebDriverExpectedCondition::elementToBeClickable($loginButtonSearch);
101
        $this->webDriver->wait()->until($condition);
102
        $this->findByLinkText('Create an Account')->click();
103
104
        $random = rand(0, 1000);
105
        $this->findById('firstname')->clear()->sendKeys($this->configuration['firstname']);
106
        $this->findById('lastname')->sendKeys($this->configuration['lastname']);
107
        $this->findById('email_address')->sendKeys($this->configuration['email']);
108
        $this->findById('password')->sendKeys($this->configuration['password']);
109
        $this->findById('password-confirmation')->sendKeys($this->configuration['password']);
110
        $this->findById('form-validate')->submit();
111
112
        $addressButton = WebDriverBy::partialLinkText('Address Book');
113
        $condition = WebDriverExpectedCondition::elementToBeClickable($addressButton);
114
        $this->webDriver->wait()->until($condition);
115
116
        $this->assertContains(
117
            $this->configuration['firstname'],
118
            $this->findByClass('block-dashboard-info')->getText()
119
        );
120
        $this->findByPartialLinkText('Address Book')->click();
121
122
        $addressLink = WebDriverBy::id('firstname');
123
        $condition = WebDriverExpectedCondition::presenceOfElementLocated($addressLink);
124
        $this->webDriver->wait()->until($condition);
125
126
        $this->findById('telephone')->clear()->sendKeys($this->configuration['phone']);
127
        $this->findById('street_1')->sendKeys($this->configuration['street']);
128
        $this->findById('city')->sendKeys($this->configuration['city']);
129
        $this->findById('zip')->sendKeys($this->configuration['zip']);
130
131
        $this->webDriver->findElement(WebDriverBy::id('country'))
132
                        ->findElement(WebDriverBy::cssSelector("option[value='ES']"))
133
                        ->click();
134
        sleep(1);
135
        $this->webDriver->findElement(WebDriverBy::id('region_id'))
136
                        ->findElement(WebDriverBy::cssSelector("option[value='161']"))
137
                        ->click();
138
        $this->findById('form-validate')->submit();
139
    }
140
141
    /**
142
     * @require createAccount
143
     *
144
     * @throws \Exception
145
     */
146
    public function loginToFrontend()
147
    {
148
        $this->webDriver->get(self::MAGENTO_URL);
149
        $loginButton = WebDriverBy::partialLinkText('Sign In');
150
        $condition = WebDriverExpectedCondition::elementToBeClickable($loginButton);
151
        $this->webDriver->wait()->until($condition);
152
        $this->assertTrue((bool) $condition);
153
        $this->webDriver->findElement($loginButton)->click();
154
155
        $verifyElement = WebDriverBy::id('login-form');
156
        $condition = WebDriverExpectedCondition::elementToBeClickable($verifyElement);
157
        $this->webDriver->wait()->until($condition);
158
        $this->assertTrue((bool) $condition);
159
160
        $this->findById('email')->sendKeys($this->configuration['email']);
161
        $this->findById('pass')->sendKeys($this->configuration['password']);
162
        $this->findById('login-form')->submit();
163
    }
164
165
    /**
166
     *
167
     * @throws \Exception
168
     */
169
    public function logoutFromFrontend()
170
    {
171
        $this->webDriver->get(self::MAGENTO_URL.self::LOGOUT_FOLDER);
172
173
        $validatorSearch = WebDriverBy::className('base');
174
        $actualString = $this->webDriver->findElement($validatorSearch)->getText();
175
        $compareString = (strpos($actualString, "You are signed out")) === false ? false : true;
176
        $this->assertTrue($compareString, "PR1-PR4");
177
    }
178
179
    /**
180
     * Configure product
181
     */
182
    public function checkProductPage()
183
    {
184
        $this->checkSimulator();
185
        $pmtSimElement = WebDriverBy::className('PmtSimulator');
186
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($pmtSimElement);
187
        $this->webDriver->wait()->until($condition);
188
        sleep(2);
189
        $simulatorElement = $this->findByClass('PmtSimulator');
190
        $currentSimulatorPrice = $simulatorElement->getAttribute('data-pmt-amount');
191
        $this->configureProduct(self::PRODUCT_QTY_AFTER);
192
        sleep(10);
193
        $simulatorElement = $this->findByClass('PmtSimulator');
194
        $newPrice = $simulatorElement->getAttribute('data-pmt-amount');
195
        $this->assertNotEmpty($currentSimulatorPrice, $currentSimulatorPrice);
196
        $this->assertNotNull($currentSimulatorPrice, $currentSimulatorPrice);
197
        $newSimulatorPrice = $currentSimulatorPrice * self::PRODUCT_QTY_AFTER;
198
        $this->assertEquals($newPrice, $newSimulatorPrice, "PR22,PR23");
199
200
        $paymentFormElement = WebDriverBy::id('product-addtocart-button');
201
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($paymentFormElement);
202
        $this->webDriver->wait()->until($condition);
203
        $addToCartButton = $this->findById('product-addtocart-button');
204
        $addToCartButton->click();
205
        sleep(5);
206
    }
207
208
    /**
209
     * @param bool $verifySimulator
210
     *
211
     * @throws \Exception
212
     */
213
    public function goToProduct($verifySimulator = true)
0 ignored issues
show
The parameter $verifySimulator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

213
    public function goToProduct(/** @scrutinizer ignore-unused */ $verifySimulator = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
    {
215
        $this->webDriver->get(self::MAGENTO_URL);
216
        $this->findByLinkText(self::PRODUCT_NAME)->click();
217
        $condition = WebDriverExpectedCondition::titleContains(self::PRODUCT_NAME);
218
        $this->webDriver->wait()->until($condition);
219
        $this->assertTrue((bool) $condition);
220
    }
221
222
    /**
223
     * @require goToProduct
224
     *
225
     * @throws \Exception
226
     */
227
    public function addProduct()
228
    {
229
        $addToCartSearch = WebDriverBy::id('product-addtocart-button');
230
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($addToCartSearch);
231
        $this->webDriver->wait()->until($condition);
232
        $this->assertTrue((bool) $condition);
233
        $this->webDriver->findElement($addToCartSearch)->click();
234
235
        $validatorSearch = WebDriverBy::className('messages');
236
        $actualString = $this->webDriver->findElement($validatorSearch)->getText();
237
        $addedMessage = "You added ".self::PRODUCT_NAME." to your shopping cart";
238
        $compareString = (strpos($actualString, $addedMessage)) === false ? false : true;
239
        $this->assertTrue($compareString);
240
241
        //You added Fusion Backpack to your shopping cart.
242
        $shoppingCartSearch = WebDriverBy::id('shopping_cart');
243
        $this->webDriver->findElement($shoppingCartSearch)->click();
244
        $shoppingCartTitle = WebDriverBy::id('cart_title');
245
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($shoppingCartTitle);
246
        $this->webDriver->wait()->until($condition);
247
        $this->assertTrue((bool) $condition);
248
    }
249
250
    public function goToCart()
251
    {
252
        $this->webDriver->get(self::MAGENTO_URL.self::CART_FOLDER);
253
        $this->findByLinkText(self::PRODUCT_NAME)->click();
254
        $condition = WebDriverExpectedCondition::titleContains(self::CART_TITLE);
255
        $this->webDriver->wait()->until($condition);
256
        $this->assertTrue((bool) $condition);
257
    }
258
259
    /**
260
     * Config product quantity
261
     *
262
     * @param $qty
263
     */
264
    public function configureProduct($qty = 1)
265
    {
266
        $qtyElements = $this->webDriver->findElements(WebDriverBy::id('qty'));
267
        foreach ($qtyElements as $qtyElement) {
268
            $qtyElement->clear()->sendKeys($qty);
269
        }
270
    }
271
272
    /**
273
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
274
     * @throws \Facebook\WebDriver\Exception\TimeOutException
275
     */
276
    public function goToCheckout()
277
    {
278
        $this->webDriver->get(self::MAGENTO_URL.self::CHECKOUT_FOLDER);
279
        $condition = WebDriverExpectedCondition::titleContains(self::CHECKOUT_TITLE);
280
        $this->webDriver->wait()->until($condition);
281
        $this->assertTrue((bool)$condition, self::MAGENTO_URL.self::CHECKOUT_FOLDER);
282
    }
283
284
    /**
285
     * @throws \Exception
286
     */
287
    public function verifyUTF8()
288
    {
289
        $paymentFormElement = WebDriverBy::className('FieldsPreview-desc');
290
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($paymentFormElement);
291
        $this->webDriver->wait()->until($condition);
292
        $this->assertTrue((bool) $condition);
293
        $this->assertSame(
294
            $this->configuration['firstname'] . ' ' . $this->configuration['lastname'],
295
            $this->findByClass('FieldsPreview-desc')->getText()
296
        );
297
    }
298
299
    /**
300
     * Verify Paylater
301
     *
302
     * @throws \Exception
303
     */
304
    public function verifyPaylater()
305
    {
306
        $condition = WebDriverExpectedCondition::titleContains(self::PMT_TITLE);
307
        $this->webDriver->wait(300)->until($condition, $this->webDriver->getCurrentURL());
308
        $this->assertTrue((bool)$condition, $this->webDriver->getCurrentURL());
309
310
        SeleniumHelper::finishForm($this->webDriver);
311
    }
312
313
    /**
314
     * Prepare checkout, called from BuyRegistered and BuyUnregistered
315
     */
316
    public function prepareCheckout()
317
    {
318
        $firstnameElement = WebDriverBy::name('firstname');
319
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($firstnameElement);
320
        $this->webDriver->wait(200)->until($condition);
321
        $this->assertTrue((bool) $condition);
322
323
        $countryElement = WebDriverBy::name('country_id');
324
        $condition = WebDriverExpectedCondition::elementToBeClickable($countryElement);
325
        $this->webDriver->wait()->until($condition);
326
        $this->assertTrue((bool) $condition);
327
        $this->webDriver->findElement(WebDriverBy::name('country_id'))
328
                        ->findElement(WebDriverBy::cssSelector("option[value='ES']"))
329
                        ->click();
330
        sleep(1);
331
        $this->webDriver->findElement(WebDriverBy::name('region_id'))
332
                        ->findElement(WebDriverBy::cssSelector("option[value='139']"))
333
                        ->click();
334
335
        $this->findByName('street[0]')->clear()->sendKeys($this->configuration['street']);
336
        $this->findByName('city')->clear()->sendKeys($this->configuration['city']);
337
        $this->findByName('postcode')->clear()->sendKeys($this->configuration['zip']);
338
        $this->findById('customer-email')->clear()->sendKeys($this->configuration['email']);
339
        $this->findByName('firstname')->clear()->sendKeys($this->configuration['firstname']);
340
        $this->findByName('lastname')->clear()->sendKeys($this->configuration['lastname']);
341
        $this->findByName('telephone')->clear()->sendKeys($this->configuration['phone']);
342
343
        $this->goToPayment();
344
    }
345
346
    /**
347
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
348
     * @throws \Facebook\WebDriver\Exception\TimeOutException
349
     */
350
    public function goToPayment()
351
    {
352
        $continueElement = WebDriverBy::name('ko_unique_1');
353
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($continueElement);
354
        $this->webDriver->wait()->until($condition);
355
        $this->assertTrue((bool) $condition);
356
357
        $condition = WebDriverExpectedCondition::elementToBeClickable($continueElement);
358
        $this->webDriver->wait()->until($condition);
359
360
        $this->findByName('ko_unique_1')->click();
361
362
        $continueElement = WebDriverBy::className('continue');
363
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($continueElement);
364
        $this->webDriver->wait()->until($condition);
365
        $this->assertTrue((bool) $condition);
366
        $this->findByClass('continue')->click();
367
    }
368
369
    /**
370
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
371
     * @throws \Facebook\WebDriver\Exception\TimeOutException
372
     */
373
    public function preparePaymentMethod()
374
    {
375
        $paylaterElement = WebDriverBy::id('paylater');
376
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($paylaterElement);
377
        $this->webDriver->wait()->until($condition);
378
        $this->assertTrue((bool) $condition);
379
380
        $condition = WebDriverExpectedCondition::elementToBeClickable($paylaterElement);
381
        $this->webDriver->wait()->until($condition);
382
        $this->assertTrue((bool) $condition);
383
384
        sleep(2);
385
        $this->findById('paylater')->click();
386
        sleep(2);
387
388
        $paylaterElement = WebDriverBy::className('payment-group');
389
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($paylaterElement);
390
        $this->webDriver->wait()->until($condition);
391
        $this->assertTrue((bool) $condition);
392
393
        $menuSearch = WebDriverBy::cssSelector("#checkout-payment-method-load > .payment-methods > .payment-group > ._active > .payment-method-title");
394
        $menuElement = $this->webDriver->findElement($menuSearch);
395
        $actualString = $menuElement->getText();
396
        $compareString = (strstr($actualString, $this->configuration['methodName'])) === false ? false : true;
397
        $this->assertTrue($compareString, $actualString, "PR25,PR26");
398
399
        $this->checkSimulator();
400
401
        $priceSearch = WebDriverBy::className('price');
402
        $priceElements = $this->webDriver->findElements($priceSearch);
403
        $price = $priceElements['6']->getText();
404
405
        $this->assertNotEquals($price, 0, $price);
406
        $this->assertNotEmpty($price);
407
408
        sleep(2);
409
        $checkoutButton = WebDriverBy::cssSelector("#checkout-payment-method-load > .payment-methods > .payment-group > ._active > .payment-method-content > .actions-toolbar > .primary");
410
        $condition = WebDriverExpectedCondition::elementToBeClickable($checkoutButton);
411
        $this->webDriver->wait()->until($condition);
412
413
        $menuElement = $this->webDriver->findElement($checkoutButton);
414
        $menuElement->click();
415
        return $price;
416
    }
417
418
    /**
419
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
420
     * @throws \Facebook\WebDriver\Exception\TimeOutException
421
     */
422
    public function verifyOrder()
423
    {
424
        $condition = WebDriverExpectedCondition::titleContains(self::SUCCESS_TITLE);
425
        $this->webDriver->wait()->until($condition);
426
        $this->assertTrue((bool) $condition);
427
428
        $menuSearch = WebDriverBy::className("base");
429
        $menuElement = $this->webDriver->findElement($menuSearch);
430
        $actualString = $menuElement->getText();
431
        $this->assertContains('Thank you for your purchase!', $actualString, "PR42");
432
    }
433
434
    /**
435
     * @return string
436
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
437
     * @throws \Facebook\WebDriver\Exception\TimeOutException
438
     */
439
    public function verifyOrderInformation()
440
    {
441
        $this->findByClass('order-number')->click();
442
        $condition = WebDriverExpectedCondition::titleContains(self::ORDER_TITLE);
443
        $this->webDriver->wait()->until($condition);
444
        $this->assertTrue((bool) $condition);
445
446
        $menuSearch = WebDriverBy::cssSelector("#my-orders-table > tfoot > .grand_total > .amount > strong > .price");
447
        $menuElement = $this->webDriver->findElement($menuSearch);
448
        return $menuElement->getText();
449
    }
450
451
    /**
452
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
453
     * @throws \Facebook\WebDriver\Exception\TimeOutException
454
     */
455
    public function goToOrder()
456
    {
457
        $condition = WebDriverExpectedCondition::titleContains('Order');
458
        $this->webDriver->wait()->until($condition);
459
        $this->assertTrue((bool) $condition);
460
    }
461
462
    /**
463
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
464
     * @throws \Facebook\WebDriver\Exception\TimeOutException
465
     */
466
    private function checkSimulator()
467
    {
468
        $simulatorElementSearch = WebDriverBy::className('PmtSimulator');
469
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($simulatorElementSearch);
470
        $this->webDriver->wait()->until($condition);
471
        $this->assertTrue((bool) $condition, "PR19//PR28");
472
        $simulatorElement = $this->webDriver->findElement(WebDriverBy::className('PmtSimulator'));
473
        $minInstallments = $simulatorElement->getAttribute('data-pmt-num-quota');
474
        $this->assertEquals($minInstallments, $this->configuration['defaultMinIns'], "PR20//PR29");
475
        $maxInstallments = $simulatorElement->getAttribute('data-pmt-max-ins');
476
        $this->assertEquals($maxInstallments, $this->configuration['defaultMaxIns'], "PR20//PR29");
477
    }
478
}
479