Completed
Pull Request — master (#204)
by Phil
57:14 queued 56:02
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 $textEditorCloseButtonId = "editor_close";
42
43
	/**
44
	 * type in the field that matches the given xpath and press enter.
45
	 * Note: this depends on methods that might only be in the Selenium
46
	 * implementation
47
	 *
48
	 * @param string $xpath
49
	 * @param string $text
50
	 * @param Session $session
51
	 * @throws \SensioLabs\Behat\PageObjectExtension\PageObject\Exception\ElementNotFoundException
52
	 * @return void
53
	 */
54
	public function typeInFieldAndPressEnter($xpath, $text, Session $session) {
55
		$element
56
			= $session->getDriver()->getWebDriverSession()->element("xpath", $xpath);
57
58
		if (is_null($element)) {
59
			throw new ElementNotFoundException(
60
				"could not find element with xpath '" . $xpath . "'"
61
			);
62
		}
63
64
		$keys = preg_split('//u', $text, null, PREG_SPLIT_NO_EMPTY);
65
		$keys[] = Key::ENTER;
66
		$element->postValue(array('value' => $keys));
67
	}
68
69
	/**
70
	 * create a text file with the given name.
71
	 * If name is not given the default is used.
72
	 * If $useDefaultFileExtension is true, then only the name is entered and the
73
	 * file extension is the default given by the application.
74
	 *
75
	 * @param Session $session
76
	 * @param string $name
77
	 * @param boolean $useDefaultFileExtension
78
	 * @return void
79
	 */
80
	public function createTextFile(
81
		Session $session,
82
		$name = null,
83
		$useDefaultFileExtension = false
84
	) {
85
		$newFileFolderButton
86
			= $this->find("xpath", $this->newFileFolderButtonXpath);
87
88
		if ($newFileFolderButton === null) {
89
			throw new ElementNotFoundException(
90
				"could not find new file/folder button"
91
			);
92
		}
93
94
		$newFileFolderButton->click();
95
96
		$newTextFileButton = $this->find("xpath", $this->newTextFileButtonXpath);
97
98
		if ($newTextFileButton === null) {
99
			throw new ElementNotFoundException(
100
				"could not find new text file button"
101
			);
102
		}
103
104
		$newTextFileButton->click();
105
106
		if (strlen($name)) {
107
			if ($useDefaultFileExtension) {
108
				$this->typeInFieldAndPressEnter(
109
					$this->newTextFileNameInputXpath,
110
					$name,
111
					$session
112
				);
113
			} else {
114
				try {
115
					$this->fillField($this->newTextFileNameInputLabel, $name . "\n");
116
				} 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...
117
					// this seems to be a bug in MinkSelenium2Driver.
118
					// used to work fine in 1.3.1 but now throws this exception
119
					// actually all that we need does happen,
120
					// so we just don't do anything
121
				}
122
			}
123
		} else {
124
			$this->typeInFieldAndPressEnter(
125
				$this->newTextFileNameInputXpath,
126
				'',
127
				$session
128
			);
129
		}
130
	}
131
132
	/**
133
	 * finds the textarea field to use for editing a text file
134
	 *
135
	 * @throws ElementNotFoundException
136
	 * @return \Behat\Mink\Element\NodeElement
137
	 */
138
	public function findTextFileEditField() {
139
		$textField = $this->find(
140
			"xpath", $this->textFileEditXpath
141
		);
142
		if ($textField === null) {
143
			throw new ElementNotFoundException("could not find textarea field");
144
		}
145
		return $textField;
146
	}
147
148
	/**
149
	 * type text into the text area
150
	 *
151
	 * @param string $text
152
	 * @return void
153
	 */
154
	public function typeIntoTextFile($text) {
155
		$textField = $this->findTextFileEditField();
156
		$textField->setValue($text);
157
	}
158
159
	/**
160
	 *
161
	 * @throws ElementNotFoundException
162
	 * @return void
163
	 */
164
	public function closeTheTextEditor() {
165
		$closeButton = $this->findById($this->textEditorCloseButtonId);
166
		if ($closeButton === null) {
167
			throw new ElementNotFoundException(
168
				"could not find text editor close button"
169
			);
170
		}
171
		$closeButton->click();
172
	}
173
174
	/**
175
	 * @param int $timeout_msec
176
	 * @return void
177
	 */
178
	public function waitTillEditorIsLoaded(
179
		$timeout_msec = STANDARDUIWAITTIMEOUTMILLISEC
180
	) {
181
		$this->waitTillElementIsNotNull($this->textFileEditXpath, $timeout_msec);
182
	}
183
184
}