Completed
Pull Request — master (#204)
by Phil
25:15 queued 24:03
created

TextEditorPage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
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 125
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A typeInFieldAndPressEnter() 0 13 2
B createTextFile() 0 37 6
A findTextFileEditField() 0 9 2
A typeIntoTextFile() 0 4 1
A closeTheTextEditor() 0 9 2
A waitTillEditorIsLoaded() 0 4 1
1
<?php
2
/**
3
* ownCloud
4
*
5
* @author Phillip Davis
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 SensioLabs\Behat\PageObjectExtension\PageObject\Exception\ElementNotFoundException;
26
use WebDriver\Key;
27
28
class TextEditorPage extends FilesPage
29
{
30
	protected $newTextFileButtonXpath = './/div[contains(@class, "newFileMenu")]//a[@data-templatename="New text file.txt"]';
31
	protected $newTextFileNameInputLabel = 'New text file.txt';
32
	protected $newTextFileNameInputXpath = ".//div[contains(@class, \"newFileMenu\")]//a[@data-templatename=\"New text file.txt\"]//input";
33
	protected $textFileEditXpath = "//textarea[contains(@class,'ace_text-input')]";
34
	protected $textEditorCloseButtonId = "editor_close";
35
36
	/**
37
	 * type in the field that matches the given xpath and press enter.
38
	 * Note: this depends on methods that might only be in the Selenium implementation
39
	 *
40
	 * @param string $xpath
41
	 * @param string $text
42
	 * @throws \SensioLabs\Behat\PageObjectExtension\PageObject\Exception\ElementNotFoundException
43
	 */
44
	public function typeInFieldAndPressEnter($xpath, $text)
45
	{
46
		$element = $this->getSession()->getDriver()->getWebDriverSession()->element("xpath", $xpath);
47
		if (is_null($element)) {
48
			throw new ElementNotFoundException(
49
				"could not find element with xpath '" . $xpath . "'"
50
			);
51
		}
52
53
		$keys = preg_split('//u', $text, null, PREG_SPLIT_NO_EMPTY);
54
		$keys[] = Key::ENTER;
55
		$element->postValue(array('value' => $keys));
56
	}
57
58
	/**
59
	 * create a text file with the given name.
60
	 * If name is not given the default is used.
61
	 * If $useDefaultFileExtension is true, then only the name is entered and the
62
	 * file extension is the default given by the application.
63
	 *
64
	 * @param string $name
65
	 * @param boolean $useDefaultFileExtension
66
	 */
67
	public function createTextFile($name = null, $useDefaultFileExtension = false)
68
	{
69
		$newFileFolderButton = $this->find("xpath", $this->newFileFolderButtonXpath);
70
71
		if ($newFileFolderButton === null) {
72
			throw new ElementNotFoundException("could not find new file/folder button");
73
		}
74
75
		$newFileFolderButton->click();
76
77
		$newTextFileButton = $this->find("xpath", $this->newTextFileButtonXpath);
78
79
		if ($newTextFileButton === null) {
80
			throw new ElementNotFoundException("could not find new text file button");
81
		}
82
83
		$newTextFileButton->click();
84
85
		if (strlen($name)) {
86
			if ($useDefaultFileExtension) {
87
				$this->typeInFieldAndPressEnter(
88
					$this->newTextFileNameInputXpath,
89
					$name
90
				);
91
			} else {
92
				try {
93
					$this->fillField($this->newTextFileNameInputLabel, $name . "\n");
94
				} 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...
95
					//this seems to be a bug in MinkSelenium2Driver.
96
					//used to work fine in 1.3.1 but now throws this exception
97
					//actually all that we need does happen, so we just don't do anything
98
				}
99
			}
100
		} else {
101
			$this->typeInFieldAndPressEnter($this->newTextFileNameInputXpath, '');
102
		}
103
	}
104
105
	/**
106
	 * finds the textarea field to use for editing a text file
107
	 *
108
	 * @throws ElementNotFoundException
109
	 * @return \Behat\Mink\Element\NodeElement
110
	 */
111
	public function findTextFileEditField() {
112
		$textField = $this->find(
113
			"xpath", $this->textFileEditXpath
114
		);
115
		if ($textField === null) {
116
			throw new ElementNotFoundException("could not find textarea field");
117
		}
118
		return $textField;
119
	}
120
121
	/**
122
	 * type text into the text area
123
	 *
124
	 * @param string $text
125
	 * @return void
126
	 */
127
	public function typeIntoTextFile($text) {
128
		$textField = $this->findTextFileEditField();
129
		$textField->setValue($text);
130
	}
131
132
	/**
133
	 *
134
	 * @throws ElementNotFoundException
135
	 * @return void
136
	 */
137
	public function closeTheTextEditor() {
138
		$closeButton = $this->findById($this->textEditorCloseButtonId);
139
		if ($closeButton === null) {
140
			throw new ElementNotFoundException(
141
				"could not find text editor close button"
142
			);
143
		}
144
		$closeButton->click();
145
	}
146
147
	public function waitTillEditorIsLoaded($timeout_msec = STANDARDUIWAITTIMEOUTMILLISEC)
148
	{
149
		$this->waitTillElementIsNotNull($this->textFileEditXpath, $timeout_msec);
150
	}
151
152
}