Completed
Push — master ( 026716...d5b3a1 )
by Phil
23:32
created

TextEditorPage::textFileContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 12
nc 4
nop 0
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\Key;
28
29
/**
30
 * Text Editor page.
31
 */
32
class TextEditorPage extends FilesPage {
33
	protected $newTextFileButtonXpath
34
		= './/div[contains(@class, "newFileMenu")]' .
35
			'//a[@data-templatename="New text file.txt"]';
36
	protected $newTextFileNameInputLabel = 'New text file.txt';
37
	protected $newTextFileNameInputXpath
38
		= './/div[contains(@class, "newFileMenu")]' .
39
			'//a[@data-templatename="New text file.txt"]//input';
40
	protected $newTextFileTooltipXpath = ".//*[@class='tooltip-inner']";
41
	protected $textFileEditXpath = "//textarea[contains(@class,'ace_text-input')]";
42
	protected $textFileTextLayerXpath = "//div[contains(@class,'ace_text-layer')]";
43
	protected $textFileLineXpath = ".//div[@class='ace_line']";
44
	protected $textEditorCloseButtonId = "editor_close";
45
46
	/**
47
	 * type in the field that matches the given xpath and optionally press enter.
48
	 * Note: this depends on methods that might only be in the Selenium
49
	 * implementation
50
	 *
51
	 * @param Session $session
52
	 * @param string $xpath
53
	 * @param string $text
54
	 * @param bool $pressEnter
55
	 * @throws ElementNotFoundException
56
	 * @return void
57
	 */
58
	public function typeInField(
59
		Session $session,
60
		$xpath,
61
		$text,
62
		$pressEnter = false
63
	) {
64
		$element = $session->getDriver()->getWebDriverSession()->element(
65
			"xpath", $xpath
66
		);
67
68
		if (is_null($element)) {
69
			throw new ElementNotFoundException(
70
				"could not find element with xpath '" . $xpath . "'"
71
			);
72
		}
73
74
		$keys = preg_split('//u', $text, null, PREG_SPLIT_NO_EMPTY);
75
		if ($pressEnter) {
76
			$keys[] = Key::ENTER;
77
		}
78
		$element->postValue(array('value' => $keys));
79
	}
80
81
	/**
82
	 * create a text file with the given name.
83
	 * If name is not given the default is used.
84
	 * If $useDefaultFileExtension is true, then only the name is entered and the
85
	 * file extension is the default given by the application.
86
	 *
87
	 * @param Session $session
88
	 * @param string $name
89
	 * @param boolean $useDefaultFileExtension
90
	 * @return void
91
	 */
92
	public function createTextFile(
93
		Session $session,
94
		$name = null,
95
		$useDefaultFileExtension = false
96
	) {
97
		$newFileFolderButton
98
			= $this->find("xpath", $this->newFileFolderButtonXpath);
99
100
		if ($newFileFolderButton === null) {
101
			throw new ElementNotFoundException(
102
				"could not find new file/folder button"
103
			);
104
		}
105
106
		$newFileFolderButton->click();
107
108
		$newTextFileButton = $this->find("xpath", $this->newTextFileButtonXpath);
109
110
		if ($newTextFileButton === null) {
111
			throw new ElementNotFoundException(
112
				"could not find new text file button"
113
			);
114
		}
115
116
		$newTextFileButton->click();
117
118
		if (strlen($name)) {
119
			if ($useDefaultFileExtension) {
120
				$this->typeInField(
121
					$session,
122
					$this->newTextFileNameInputXpath,
123
					$name,
124
					true
125
				);
126
			} else {
127
				try {
128
					$this->fillField($this->newTextFileNameInputLabel, $name . "\n");
129
				} catch (\WebDriver\Exception\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...
130
					// this seems to be a bug in MinkSelenium2Driver.
131
					// used to work fine in 1.3.1 but now throws this exception
132
					// actually all that we need does happen,
133
					// so we just don't do anything
134
				}
135
			}
136
		} else {
137
			$this->typeInField(
138
				$session,
139
				$this->newTextFileNameInputXpath,
140
				'',
141
				true
142
			);
143
		}
144
145
		$this->waitForAjaxCallsToStartAndFinish($session);
146
	}
147
148
	/**
149
	 * returns the tooltip that is displayed next to the new text file name box
150
	 *
151
	 * @return string
152
	 */
153
	public function getTooltipOfNewTextFileBox() {
154
		$newTextFileTooltip = $this->find("xpath", $this->newTextFileTooltipXpath);
155
156
		if ($newTextFileTooltip === null) {
157
			throw new ElementNotFoundException(
158
				"could not find new text file box tooltip"
159
			);
160
		}
161
162
		return $newTextFileTooltip->getText();
163
	}
164
165
	/**
166
	 * type text into the text area
167
	 *
168
	 * @param Session $session
169
	 * @param string $text
170
	 * @return void
171
	 */
172
	public function typeIntoTextFile(
173
		Session $session,
174
		$text
175
	) {
176
		$this->typeInField(
177
			$session,
178
			$this->textFileEditXpath,
179
			$text
180
		);
181
	}
182
183
	/**
184
	 * get the content of the open text file
185
	 *
186
	 * @return array of lines of text
187
	 */
188
	public function textFileContent() {
189
		$textLayer = $this->find("xpath", $this->textFileTextLayerXpath);
190
		if ($textLayer === null) {
191
			throw new ElementNotFoundException("could not find text layer");
192
		}
193
		$textLineElements = $textLayer->findAll(
194
			"xpath", $this->textFileLineXpath
195
		);
196
		if ($textLineElements === null) {
197
			throw new ElementNotFoundException("could not find text lines");
198
		}
199
		$textLines = [];
200
		foreach ($textLineElements as $textLineElement) {
201
			$textLines[] = $textLineElement->getText();
202
		}
203
		return $textLines;
204
	}
205
206
	/**
207
	 *
208
	 * @param Session $session
209
	 * @throws ElementNotFoundException
210
	 * @return void
211
	 */
212
	public function closeTheTextEditor(Session $session) {
213
		$this->waitForAjaxCallsToStartAndFinish($session);
214
215
		$closeButton = $this->findById($this->textEditorCloseButtonId);
216
		if ($closeButton === null) {
217
			throw new ElementNotFoundException(
218
				"could not find text editor close button"
219
			);
220
		}
221
		$closeButton->click();
222
	}
223
224
	/**
225
	 * @param int $timeout_msec
226
	 * @return void
227
	 */
228
	public function waitTillEditorIsLoaded(
229
		$timeout_msec = STANDARDUIWAITTIMEOUTMILLISEC
230
	) {
231
		$this->waitTillElementIsNotNull($this->textFileEditXpath, $timeout_msec);
232
	}
233
234
}