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