Project

General

Profile

1
/*
2
 * By default, Selenium looks for a file called "user-extensions.js", and loads and javascript
3
 * code found in that file. This file is a sample of what that file could look like.
4
 *
5
 * user-extensions.js provides a convenient location for adding extensions to Selenium, like
6
 * new actions, checks and locator-strategies.
7
 * By default, this file does not exist. Users can create this file and place their extension code
8
 * in this common location, removing the need to modify the Selenium sources, and hopefully assisting
9
 * with the upgrade process.
10
 *
11
 * You can find contributed extensions at http://wiki.openqa.org/display/SEL/Contributed%20User-Extensions
12
 */
13

    
14
// The following examples try to give an indication of how Selenium can be extended with javascript.
15

    
16
// All do* methods on the Selenium prototype are added as actions.
17
// Eg add a typeRepeated action to Selenium, which types the text twice into a text box.
18
// The typeTwiceAndWait command will be available automatically
19
Selenium.prototype.doTypeRepeated = function(locator, text) {
20
    // All locator-strategies are automatically handled by "findElement"
21
    var element = this.page().findElement(locator);
22

    
23
    // Create the text to type
24
    var valueToType = text + text;
25

    
26
    // Replace the element text with the new text
27
    this.page().replaceText(element, valueToType);
28
};
29

    
30
// All assert* methods on the Selenium prototype are added as checks.
31
// Eg add a assertValueRepeated check, that makes sure that the element value
32
// consists of the supplied text repeated.
33
// The verify version will be available automatically.
34
Selenium.prototype.assertValueRepeated = function(locator, text) {
35
    // All locator-strategies are automatically handled by "findElement"
36
    var element = this.page().findElement(locator);
37

    
38
    // Create the text to verify
39
    var expectedValue = text + text;
40

    
41
    // Get the actual element value
42
    var actualValue = element.value;
43

    
44
    // Make sure the actual value matches the expected
45
    Assert.matches(expectedValue, actualValue);
46
};
47

    
48
// All get* methods on the Selenium prototype result in
49
// store, assert, assertNot, verify, verifyNot, waitFor, and waitForNot commands.
50
// E.g. add a getTextLength method that returns the length of the text
51
// of a specified element.
52
// Will result in support for storeTextLength, assertTextLength, etc.
53
Selenium.prototype.getTextLength = function(locator) {
54
	return this.getText(locator).length;
55
};
56

    
57
// All locateElementBy* methods are added as locator-strategies.
58
// Eg add a "valuerepeated=" locator, that finds the first element with the supplied value, repeated.
59
// The "inDocument" is a the document you are searching.
60
PageBot.prototype.locateElementByValueRepeated = function(text, inDocument) {
61
    // Create the text to search for
62
    var expectedValue = text + text;
63

    
64
    // Loop through all elements, looking for ones that have a value === our expected value
65
    var allElements = inDocument.getElementsByTagName("*");
66
    for (var i = 0; i < allElements.length; i++) {
67
        var testElement = allElements[i];
68
        if (testElement.value && testElement.value === expectedValue) {
69
            return testElement;
70
        }
71
    }
72
    return null;
73
};
74

    
75

    
(19-19/20)