Completed
Push — texteditor-ui-tests-01 ( 373aa3...e6f262 )
by Phil
21:02
created

TextEditorPage::closeTheTextEditor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
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 $useDefaultFileType is true, then only the name is entered and the
62
	 * file type is the default given by the application.
63
	 *
64
	 * @param string $name
65
	 * @param boolean $useDefaultFileType
66
	 */
67
	public function createTextFile($name = null, $useDefaultFileType = false)
68
	{
69
		$this->find("xpath", $this->newFileFolderButtonXpath)->click();
70
		$this->find("xpath", $this->newTextFileButtonXpath)->click();
71
		if (strlen($name)) {
72
			if ($useDefaultFileType) {
73
				$this->typeInFieldAndPressEnter(
74
					$this->newTextFileNameInputXpath,
75
					$name
76
				);
77
			} else {
78
				try {
79
					$this->fillField($this->newTextFileNameInputLabel, $name . "\n");
80
				} 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...
81
					//this seems to be a bug in MinkSelenium2Driver.
82
					//used to work fine in 1.3.1 but now throws this exception
83
					//actually all that we need does happen, so we just don't do anything
84
				}
85
			}
86
		} else {
87
			$this->typeInFieldAndPressEnter($this->newTextFileNameInputXpath, '');
88
		}
89
	}
90
91
	/**
92
	 * finds the textarea field to use for editing a text file
93
	 *
94
	 * @throws ElementNotFoundException
95
	 * @return \Behat\Mink\Element\NodeElement
96
	 */
97
	public function findTextFileEditField() {
98
		$textField = $this->find(
99
			"xpath", $this->textFileEditXpath
100
		);
101
		if ($textField === null) {
102
			throw new ElementNotFoundException("could not find textarea field");
103
		}
104
		return $textField;
105
	}
106
107
	/**
108
	 * type text into the text area
109
	 *
110
	 * @param string $text
111
	 * @return void
112
	 */
113
	public function typeIntoTextFile($text) {
114
		$textField = $this->findTextFileEditField();
115
		$textField->setValue($text);
116
	}
117
118
	/**
119
	 *
120
	 * @throws ElementNotFoundException
121
	 * @return void
122
	 */
123
	public function closeTheTextEditor() {
124
		$closeButton = $this->findById($this->textEditorCloseButtonId);
125
		if ($closeButton === null) {
126
			throw new ElementNotFoundException(
127
				"could not find text editor close button"
128
			);
129
		}
130
		$closeButton->click();
131
	}
132
133
	public function waitTillEditorIsLoaded($timeout_msec = STANDARDUIWAITTIMEOUTMILLISEC)
134
	{
135
		$this->waitTillElementIsNotNull($this->textFileEditXpath, $timeout_msec);
136
	}
137
138
}