773172646007ee268dfb45c44ccb704a453029cb
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Acceptance / Support / Helper / Formhandler.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Acceptance\Support\Helper;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Facebook\WebDriver\Remote\RemoteWebElement;
18 use Facebook\WebDriver\WebDriverKeys;
19
20 /**
21 * Helper to interact with formhandler fields
22 */
23 class Formhandler
24 {
25 /**
26 * Selector to select one formengine section
27 * @var string
28 */
29 public static $selectorFormSection = '.form-section';
30
31 /**
32 * @var \AcceptanceTester
33 */
34 protected $tester;
35
36 /**
37 * @param \AcceptanceTester $I
38 */
39 public function __construct(\AcceptanceTester $I)
40 {
41 $this->tester = $I;
42 }
43
44 /**
45 * @param string $fieldName
46 * @return RemoteWebElement
47 */
48 protected function getContextForFormhandlerField(string $fieldName)
49 {
50 $I = $this->tester;
51 $I->comment('Get context for field "' . $fieldName . '"');
52
53 return $I->executeInSelenium(function (\Facebook\WebDriver\Remote\RemoteWebDriver $webdriver) use ($fieldName) {
54 return $webdriver->findElement(
55 \WebDriverBy::xpath('(//label[contains(text(),"' . $fieldName . '")])[1]/ancestor::fieldset[@class="form-section"][1]')
56 );
57 });
58 }
59
60 /**
61 * @param string $fieldLabel
62 * @param array $testData An array of arrays that contains the data to validate.
63 * * First value is the input value
64 * * second value is the value that is expected after the validation
65 * * optional third value is the "internal" value like required for date fields (value is internally
66 * represented by a timestamp). If this value is not defined the second value will be used.
67 * Example for field with alpha validation: [['foo', 'foo'], ['bar1'], ['bar']]
68 * Example for field with date validation: [['29-01-2016', '29-01-2016', '1454025600']]
69 */
70 public function fillSeeSaveAndClearInputField($fieldLabel, array $testData)
71 {
72 $fieldContext = $this->getContextForFormhandlerField($fieldLabel);
73 $I = $this->tester;
74 $I->wantTo('Fill field, check the fieldvalue after evaluation and delete the value.');
75
76 $visibleFieldXpath = './/*/input[@data-formengine-input-name]';
77 $clearButtonXpath = '(//label[contains(text(),"' . $fieldLabel . '")])[1]/parent::*//*/button[@class="close"]';
78 $initializedInputFieldXpath = '(//label[contains(text(),"' . $fieldLabel . '")])[1]/parent::*//*/input[@data-formengine-input-name][@data-formengine-input-initialized]';
79 $I->waitForElement($initializedInputFieldXpath, 30);
80 $inputField = $fieldContext->findElement(\WebDriverBy::xpath($visibleFieldXpath));
81 $internalInputFieldXpath = '(//label[contains(text(),"' . $fieldLabel . '")])[1]/parent::*//*/input[@name="' . $inputField->getAttribute('data-formengine-input-name') . '"]';
82
83 $I->waitForElement($internalInputFieldXpath, 30);
84 $I->waitForElement($clearButtonXpath, 30);
85
86 // the internal field name will not change during this function execution
87 $internalFieldXpath = './/*/input[@name="' . $inputField->getAttribute('data-formengine-input-name') . '"]';
88 $internalInputField = $fieldContext->findElement(\WebDriverBy::xpath($internalFieldXpath));
89
90 foreach ($testData['tests'] as $testValue) {
91 if (isset($testValue[4])) {
92 $I->comment($testValue[4]);
93 }
94 $I->comment('Fill the field and switch focus to trigger validation.');
95 $I->fillField($inputField, $testValue[0]);
96 // change the focus to trigger validation
97 $inputField->sendKeys(WebDriverKeys::TAB);
98 // click on the div so that any opened popup (potentially from the field below) is closed
99 $fieldContext->click();
100
101 $I->comment('Test value of "visible" field');
102 $I->canSeeInField($inputField, $testValue[1]);
103 $I->comment('Test value of the internal field');
104 $I->canSeeInField($internalInputField, (isset($testValue[2]) ? $testValue[2] : $testValue[1]));
105
106 // save the change
107 $saveButtonLink = '//*/button[@name="_savedok"][1]';
108 $I->waitForElement($saveButtonLink, 30);
109 if (isset($testValue[3]) && $testValue[3]) {
110 $I->click($saveButtonLink);
111 $I->switchToWindow();
112 $notificationCloseXpath = '//*[@class="modal-title"][contains(text(),"Alert")]/parent::*/button[@class="close"]';
113 $I->waitForElement($notificationCloseXpath, 30);
114 $I->click($notificationCloseXpath);
115 return;
116 } else {
117 $I->click($saveButtonLink);
118 }
119
120 // wait for the save to be completed
121 $I->waitForElement('//*/button[@name="_savedok"][not(@disabled)][1]', 30);
122 $I->waitForElement($initializedInputFieldXpath, 30);
123 $I->waitForElement($internalInputFieldXpath, 30);
124 $I->waitForElement($clearButtonXpath, 30);
125
126 // find the input fields again
127 $fieldContext = $this->getContextForFormhandlerField($fieldLabel);
128 $inputField = $fieldContext->findElement(\WebDriverBy::xpath($visibleFieldXpath));
129 $internalInputField = $fieldContext->findElement(\WebDriverBy::xpath($internalFieldXpath));
130
131 // validate that the save was successfull
132 $I->comment('Test value of "visible" field after the save');
133 $I->canSeeInField($inputField, $testValue[1]);
134 $I->comment('Test value of the internal field after the save');
135 $I->canSeeInField($internalInputField, isset($testValue[2]) ? $testValue[2] : $testValue[1]);
136 }
137
138 // clear the field
139 $I->waitForElement($clearButtonXpath, 30);
140 $I->click($clearButtonXpath);
141 $I->canSeeInField($inputField, '');
142
143 // save the change again
144 $saveButtonLink = '//*/button[@name="_savedok"][1]';
145 $I->waitForElement($saveButtonLink, 30);
146 $I->click($saveButtonLink);
147
148 // wait for the save to be completed
149 $I->waitForElement('//*/button[@name="_savedok"][not(@disabled)][1]', 30);
150 $I->waitForElement($initializedInputFieldXpath, 30);
151 $I->waitForElement($internalInputFieldXpath, 30);
152 $I->waitForElement($clearButtonXpath, 30);
153
154 // find the input fields again
155 $fieldContext = $this->getContextForFormhandlerField($fieldLabel);
156 $inputField = $fieldContext->findElement(\WebDriverBy::xpath($visibleFieldXpath));
157 $internalInputField = $fieldContext->findElement(\WebDriverBy::xpath($internalFieldXpath));
158
159 // validate that the save was successfull
160 $I->canSeeInField($inputField, isset($testData['cleared'][1]) ? $testData['cleared'][1] : '');
161 $I->canSeeInField($internalInputField, isset($testData['cleared'][0]) ? $testData['cleared'][0] : '');
162 }
163 }