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

TextEditorPage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 0
loc 95
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A typeInFieldAndPressEnter() 0 13 2
B createTextFile() 0 23 4
A findTextFileEditField() 0 9 2
A typeIntoTextFile() 0 4 1
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
35
	/**
36
	 * type in the field that matches the given xpath and press enter.
37
	 * Note: this depends on methods that might only be in the Selenium implementation
38
	 *
39
	 * @param string $xpath
40
	 * @param string $text
41
	 * @throws \SensioLabs\Behat\PageObjectExtension\PageObject\Exception\ElementNotFoundException
42
	 */
43
	public function typeInFieldAndPressEnter($xpath, $text)
44
	{
45
		$element = $this->getSession()->getDriver()->getWebDriverSession()->element("xpath", $xpath);
46
		if (is_null($element)) {
47
			throw new ElementNotFoundException(
48
				"could not find element with xpath '" . $xpath . "'"
49
			);
50
		}
51
52
		$keys = preg_split('//u', $text, null, PREG_SPLIT_NO_EMPTY);
53
		$keys[] = Key::ENTER;
54
		$element->postValue(array('value' => $keys));
55
	}
56
57
	/**
58
	 * create a text file with the given name.
59
	 * If name is not given the default is used.
60
	 * If $useDefaultFileType is true, then only the name is entered and the
61
	 * file type is the default given by the application.
62
	 *
63
	 * @param string $name
64
	 * @param boolean $useDefaultFileType
65
	 */
66
	public function createTextFile($name = null, $useDefaultFileType = false)
67
	{
68
		$this->find("xpath", $this->newFileFolderButtonXpath)->click();
69
		$this->find("xpath", $this->newTextFileButtonXpath)->click();
70
		if (strlen($name)) {
71
			if ($useDefaultFileType) {
72
				$this->typeInFieldAndPressEnter(
73
					$this->newTextFileNameInputXpath,
74
					$name
75
				);
76
			} else {
77
				try {
78
					$this->fillField($this->newTextFileNameInputLabel, $name . "\n");
79
				} 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...
80
					//this seems to be a bug in MinkSelenium2Driver.
81
					//used to work fine in 1.3.1 but now throws this exception
82
					//actually all that we need does happen, so we just don't do anything
83
				}
84
			}
85
		} else {
86
			$this->typeInFieldAndPressEnter($this->newTextFileNameInputXpath, '');
87
		}
88
	}
89
90
	/**
91
	 * finds the textarea field to use for editing a text file
92
	 *
93
	 * @throws ElementNotFoundException
94
	 * @return \Behat\Mink\Element\NodeElement
95
	 */
96
	public function findTextFileEditField() {
97
		$textField = $this->find(
98
			"xpath", $this->textFileEditXpath
99
		);
100
		if ($textField === null) {
101
			throw new ElementNotFoundException("could not find textarea field");
102
		}
103
		return $textField;
104
	}
105
106
	/**
107
	 * type text into the text area
108
	 *
109
	 * @param string $text
110
	 * @return void
111
	 */
112
	public function typeIntoTextFile($text) {
113
		$textField = $this->findTextFileEditField();
114
		$textField->setValue($text);
115
	}
116
117
	public function waitTillEditorIsLoaded($timeout_msec = STANDARDUIWAITTIMEOUTMILLISEC)
118
	{
119
		$this->waitTillElementIsNotNull($this->textFileEditXpath, $timeout_msec);
120
	}
121
122
}