Completed
Pull Request — master (#204)
by Phil
25:17 queued 23:57
created

TextEditorPage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

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

6 Methods

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