Completed
Push — texteditor-ui-tests-01 ( 7611b8...21dcf1 )
by Phil
23:50
created

TextEditorPage::findTextFileEditField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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