Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/atoms/test/text_util.js
4500 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
27
28
function getTestContainer() {
29
function createTestContainer() {
30
var testContainer = goog.dom.createDom('DIV', {
31
id: 'test-container',
32
style: 'border: 1px dotted silver'
33
}, goog.dom.createDom('DIV', {
34
style: 'font-weight:bold;' +
35
'text-decoration:underline;' +
36
'font-style:italic;' +
37
'margin: 0.5em'
38
}, 'Test Container'));
39
document.body.appendChild(testContainer);
40
return testContainer;
41
}
42
43
return goog.dom.getElement('test-container') || createTestContainer();
44
}
45
46
47
/**
48
* Verifies that the visible text for the test DOM structure.
49
* @param {!Object} assert The QUnit assert object.
50
* @param {string} testName The name of the current test.
51
* @param {!Element} element The element to check the text of.
52
* @param {...string} var_args Variadic args for the expected lines
53
* of visible text.
54
*/
55
function assertTextIs(assert, testName, element, var_args) {
56
if (!element.parentNode ||
57
// IE sets the parentNode of unattached elements to a document fragment.
58
element.parentNode.nodeType != goog.dom.NodeType.ELEMENT) {
59
var testContainer = getTestContainer();
60
testContainer.appendChild(createTestDom(element));
61
}
62
63
var expected = goog.array.slice(arguments, 3).join('\n');
64
var actual = bot.dom.getVisibleText(element);
65
66
assert.strictEqual(actual, expected,
67
'Expected: ' + escapeText(expected) +
68
'\n but was: ' + escapeText(actual) +
69
'\n raw html:\n' + element.innerHTML +
70
'\n------\n');
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
testName),
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