TextEditorPage::closeTheTextEditor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * ownCloud
4
 *
5
 * @author Phillip Davis <[email protected]>
6
 * @copyright 2017 Phillip Davis [email protected]
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace Page;
24
25
use Behat\Mink\Session;
26
use SensioLabs\Behat\PageObjectExtension\PageObject\Exception\ElementNotFoundException;
27
use WebDriver\Exception\NoSuchElement;
28
use WebDriver\Exception\StaleElementReference;
29
use WebDriver\Key;
30
31
/**
32
 * Text Editor page.
33
 */
34
class TextEditorPage extends FilesPage {
35
	protected $newTextFileButtonXpath
36
		= './/div[contains(@class, "newFileMenu")]' .
37
			'//a[@data-templatename="New text file.txt"]';
38
	protected $newTextFileNameInputLabel = 'New text file.txt';
39
	protected $newTextFileNameInputXpath
40
		= './/div[contains(@class, "newFileMenu")]' .
41
			'//a[@data-templatename="New text file.txt"]//input';
42
	protected $newTextFileTooltipXpath = ".//*[@class='tooltip-inner']";
43
	protected $textFileEditXpath = "//textarea[contains(@class,'ace_text-input')]";
44
	protected $textFileTextLayerXpath = "//div[contains(@class,'ace_text-layer')]";
45
	protected $textFileLineXpath = ".//div[@class='ace_line']";
46
	protected $textEditorCloseButtonId = "editor_close";
47
48
	/**
49
	 * type in the field that matches the given xpath and optionally press enter.
50
	 * Note: this depends on methods that might only be in the Selenium
51
	 * implementation
52
	 *
53
	 * @param Session $session
54
	 * @param string $xpath
55
	 * @param string $text
56
	 * @param bool $pressEnter
57
	 *
58
	 * @throws ElementNotFoundException
59
	 * @return void
60
	 */
61
	public function typeInField(
62
		Session $session,
63
		$xpath,
64
		$text,
65
		$pressEnter = false
66
	) {
67
		$element = $session->getDriver()->getWebDriverSession()->element(
68
			"xpath", $xpath
69
		);
70
71
		if ($element === null) {
72
			throw new ElementNotFoundException(
73
				"could not find element with xpath '" . $xpath . "'"
74
			);
75
		}
76
77
		$keys = \preg_split('//u', $text, null, PREG_SPLIT_NO_EMPTY);
78
		if ($pressEnter) {
79
			$keys[] = Key::ENTER;
80
		}
81
		$element->postValue(['value' => $keys]);
82
	}
83
84
	/**
85
	 * create a text file with the given name.
86
	 * If name is not given the default is used.
87
	 * If $useDefaultFileExtension is true, then only the name is entered and the
88
	 * file extension is the default given by the application.
89
	 *
90
	 * @param Session $session
91
	 * @param string $name
92
	 * @param boolean $useDefaultFileExtension
93
	 *
94
	 * @return void
95
	 */
96
	public function createTextFile(
97
		Session $session,
98
		$name = null,
99
		$useDefaultFileExtension = false
100
	) {
101
		$newFileFolderButton
102
			= $this->find("xpath", $this->newFileFolderButtonXpath);
103
104
		if ($newFileFolderButton === null) {
105
			throw new ElementNotFoundException(
106
				"could not find new file/folder button"
107
			);
108
		}
109
110
		$newFileFolderButton->click();
111
112
		$newTextFileButton = $this->find("xpath", $this->newTextFileButtonXpath);
113
114
		if ($newTextFileButton === null) {
115
			throw new ElementNotFoundException(
116
				"could not find new text file button"
117
			);
118
		}
119
120
		$newTextFileButton->click();
121
122
		if (\strlen($name)) {
123
			if ($useDefaultFileExtension) {
124
				$this->typeInField(
125
					$session,
126
					$this->newTextFileNameInputXpath,
127
					$name,
128
					true
129
				);
130
			} else {
131
				try {
132
					$this->fillField($this->newTextFileNameInputLabel, $name . "\n");
133
				} catch (NoSuchElement $e) {
0 ignored issues
show
Bug introduced by
The class WebDriver\Exception\NoSuchElement does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
134
					// this seems to be a bug in MinkSelenium2Driver.
135
					// used to work fine in 1.3.1 but now throws this exception
136
					// actually all that we need does happen,
137
					// so we just don't do anything
138
				} catch (StaleElementReference $e) {
0 ignored issues
show
Bug introduced by
The class WebDriver\Exception\StaleElementReference does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
139
					// At the end of processing setValue, MinkSelenium2Driver
140
					// tries to blur away from the element. But we pressed
141
					// enter which has already made the element go away.
142
					// So we do not care about this exception.
143
					// This issue started happening due to:
144
					// https://github.com/minkphp/MinkSelenium2Driver/pull/286
145
				}
146
			}
147
		} else {
148
			$this->typeInField(
149
				$session,
150
				$this->newTextFileNameInputXpath,
151
				'',
152
				true
153
			);
154
		}
155
156
		$this->waitForAjaxCallsToStartAndFinish($session);
157
	}
158
159
	/**
160
	 * returns the tooltip that is displayed next to the new text file name box
161
	 *
162
	 * @return string
163
	 */
164
	public function getTooltipOfNewTextFileBox() {
165
		$newTextFileTooltip = $this->find("xpath", $this->newTextFileTooltipXpath);
166
167
		if ($newTextFileTooltip === null) {
168
			throw new ElementNotFoundException(
169
				"could not find new text file box tooltip"
170
			);
171
		}
172
173
		return $newTextFileTooltip->getText();
174
	}
175
176
	/**
177
	 * type text into the text area
178
	 *
179
	 * @param Session $session
180
	 * @param string $text
181
	 *
182
	 * @return void
183
	 */
184
	public function typeIntoTextFile(
185
		Session $session,
186
		$text
187
	) {
188
		$this->typeInField(
189
			$session,
190
			$this->textFileEditXpath,
191
			$text
192
		);
193
	}
194
195
	/**
196
	 * get the content of the open text file
197
	 *
198
	 * @return array of lines of text
199
	 */
200
	public function textFileContent() {
201
		$textLayer = $this->find("xpath", $this->textFileTextLayerXpath);
202
		if ($textLayer === null) {
203
			throw new ElementNotFoundException("could not find text layer");
204
		}
205
		$textLineElements = $textLayer->findAll(
206
			"xpath", $this->textFileLineXpath
207
		);
208
		if ($textLineElements === null) {
209
			throw new ElementNotFoundException("could not find text lines");
210
		}
211
		$textLines = [];
212
		foreach ($textLineElements as $textLineElement) {
213
			$textLines[] = $textLineElement->getText();
214
		}
215
		return $textLines;
216
	}
217
218
	/**
219
	 *
220
	 * @param Session $session
221
	 *
222
	 * @throws ElementNotFoundException
223
	 * @return void
224
	 */
225
	public function closeTheTextEditor(Session $session) {
226
		$this->waitForAjaxCallsToStartAndFinish($session);
227
228
		$closeButton = $this->findById($this->textEditorCloseButtonId);
229
		if ($closeButton === null) {
230
			throw new ElementNotFoundException(
231
				"could not find text editor close button"
232
			);
233
		}
234
		$closeButton->click();
235
	}
236
237
	/**
238
	 * @param int $timeout_msec
239
	 *
240
	 * @return void
241
	 */
242
	public function waitTillEditorIsLoaded(
243
		$timeout_msec = STANDARDUIWAITTIMEOUTMILLISEC
244
	) {
245
		$this->waitTillElementIsNotNull($this->textFileEditXpath, $timeout_msec);
246
	}
247
}
248