Completed
Pull Request — master (#204)
by Phil
47:07 queued 22:02
created

TextEditorPage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 3
cbo 0
dl 0
loc 154
rs 10

6 Methods

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