Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/atoms/test/text_util.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
/**
19
* @fileoverview Utilities for testing {@code bot.dom.getVisibleText}.
20
*/
21
22
goog.require('bot.dom');
23
goog.require('goog.array');
24
goog.require('goog.dom');
25
goog.require('goog.dom.NodeType');
26
goog.require('goog.testing.TestCase');
27
28
29
function getTestContainer() {
30
function createTestContainer() {
31
var testContainer = goog.dom.createDom('DIV', {
32
id: 'test-container',
33
style: 'border: 1px dotted silver'
34
}, goog.dom.createDom('DIV', {
35
style: 'font-weight:bold;' +
36
'text-decoration:underline;' +
37
'font-style:italic;' +
38
'margin: 0.5em'
39
}, 'Test Container'));
40
document.body.appendChild(testContainer);
41
return testContainer;
42
}
43
44
return goog.dom.getElement('test-container') || createTestContainer();
45
}
46
47
48
/**
49
* Verifies that the visible text for the test DOM structure.
50
* @param {!Element} element The element to check the text of.
51
* @param {...string} var_args Variadic args for the expected lines
52
* of visible text.
53
*/
54
function assertTextIs(element, var_args) {
55
if (!element.parentNode ||
56
// IE sets the parentNode of unattached elements to a document fragment.
57
element.parentNode.nodeType != goog.dom.NodeType.ELEMENT) {
58
var testContainer = getTestContainer();
59
testContainer.appendChild(createTestDom(element));
60
}
61
62
var expected = goog.array.slice(arguments, 1).join('\n');
63
var actual = bot.dom.getVisibleText(element);
64
65
assertEquals(
66
'Expected: ' + escapeText(expected) +
67
'\n but was: ' + escapeText(actual) +
68
'\n raw html:\n' + element.innerHTML +
69
'\n------\n',
70
expected, actual);
71
72
function createTestDom(element) {
73
return goog.dom.createDom('div', {'style': 'width: 25em;'},
74
goog.dom.createDom('div',
75
{'style': [
76
'margin: 0 0.5em;',
77
'padding: 0;',
78
'font-style: italic;',
79
'color: gray;'
80
].join('')},
81
goog.testing.TestCase.currentTestName),
82
goog.dom.createDom('div', {
83
'style': 'margin-bottom: 0.5em'
84
}, element));
85
}
86
87
function escapeText(text) {
88
return text.replace(/\n/g, '\\n');//.replace(/\s/g, '\\s');
89
}
90
}
91
92