"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/javascript/atoms/test/locator_test.html" (17 Feb 2023, 15775 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:


The requested HTML page contains a <FORM> tag that is unusable on "Fossies" in "automatic" (rendered) mode so that page is shown as HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <!DOCTYPE html>
    2 <!--
    3 Copyright 2010 WebDriver committers
    4 Copyright 2010 Google Inc.
    5 
    6 Licensed under the Apache License, Version 2.0 (the "License");
    7 you may not use this file except in compliance with the License.
    8 You may obtain a copy of the License at
    9 
   10      http://www.apache.org/licenses/LICENSE-2.0
   11 
   12 Unless required by applicable law or agreed to in writing, software
   13 distributed under the License is distributed on an "AS IS" BASIS,
   14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15 See the License for the specific language governing permissions and
   16 limitations under the License.
   17 -->
   18 <html>
   19 <head>
   20   <title>locator_test.html</title>
   21   <script src="test_bootstrap.js"></script>
   22   <script type="text/javascript">
   23     goog.require('bot');
   24     goog.require('bot.ErrorCode');
   25     goog.require('bot.locators');
   26     goog.require('goog.events.EventType');
   27     goog.require('goog.testing.PropertyReplacer');
   28     goog.require('goog.testing.jsunit');
   29   </script>
   30 
   31   <script type="text/javascript">
   32     function testCanFindById() {
   33       var e = bot.locators.findElement({id: 'x'});
   34 
   35       assertEquals('para', e.getAttribute('name'));
   36     }
   37 
   38     function testCanFindByIdWhenElementIsAfterOneWithSameName() {
   39       var e = bot.locators.findElement({id: 'after'});
   40 
   41       assertEquals('right', e.getAttribute('name'));
   42     }
   43 
   44     function testFindByIdDoesNotMatchSearchContextRoot() {
   45       var root = bot.locators.findElement({id: 'x'});
   46       assertNull(bot.locators.findElement({id: 'x'}, root));
   47       assertEquals(0, bot.locators.findElements({id: 'x'}, root).length);
   48     }
   49 
   50     function testFindingByNameIgnoresComments() {
   51       // This element is outside a form, forcing IE to fall back to using
   52       // doc.getElementsByTagName. There are comments on this page that are
   53       // returned in this collection. We access the "attributes" array of
   54       // each element, which is clearly missing from comments. A roundabout way
   55       // of doing this, but it works
   56 
   57       try {
   58         var element = bot.locators.findElement({name: 'para'});
   59         assertNotNull(element); // this is expected
   60       } catch (e) {
   61         fail(e);  // this is not.
   62       }
   63     }
   64 
   65     function testCanFindByNameOutsideOfAForm() {
   66       var e = bot.locators.findElement({name: 'para'});
   67 
   68       assertEquals('x', e.getAttribute('id'));
   69     }
   70 
   71     function testWillNotFindAnElementByNameWhereNoMatchShouldBeFound() {
   72       assertNull(bot.locators.findElement({name: 'foobar'}));
   73     }
   74 
   75     function testCanFindByClassName() {
   76       var dogs = bot.locators.findElement({className: 'dogs'});
   77       assertNotNull(dogs);
   78       assertEquals('after', dogs.id);
   79     }
   80 
   81     function testCannotSearchWithCompoundClassNames() {
   82       assertThrows(goog.bind(bot.locators.findElement, null,
   83           {className: 'feline cats'}));
   84     }
   85 
   86     function testFindByClassNameReturnsFirstMatchingElement() {
   87       var cats = bot.locators.findElement({className: 'cats'});
   88       assertNotNull(cats);
   89       assertEquals('wrong', cats.id);
   90     }
   91 
   92     function testCanFindByAnyOfAnElementsClassNames() {
   93       var felines = bot.locators.findElement({className: 'feline'});
   94       var cats = bot.locators.findElement({className: 'cats'});
   95 
   96       assertEquals(felines, cats);
   97       assertEquals('wrong', cats.id);
   98     }
   99 
  100     function testFindByClassNameReturnsNullIfNoMatchIsFound() {
  101       assertNull(bot.locators.findElement({className: 'catsAndDogs'}));
  102     }
  103 
  104     function testFindByClassNameDoesNotMatchSearchContextRoot() {
  105       var root = bot.locators.findElement({className: 'cats'});
  106       assertNull(bot.locators.findElement({className: 'cats'}, root));
  107       assertEquals(0,
  108           bot.locators.findElements({className: 'cats'}, root).length);
  109     }
  110 
  111     /** @bug http://code.google.com/p/selenium/issues/detail?id=1918 */
  112     function testFindSingleByClassName_targetClassHasDots() {
  113       var found = bot.locators.findElement({className: 'name.with.dots'});
  114       assertEquals(goog.dom.$('dotted_1'), found);
  115     }
  116 
  117     /** @bug http://code.google.com/p/selenium/issues/detail?id=1918 */
  118     function testFindManyByClassName_targetClassHasDot() {
  119       var found = bot.locators.findElements({className: 'name.with.dots'});
  120       assertEquals(2, found.length);
  121       assertEquals(goog.dom.$('dotted_1'), found[0]);
  122       assertEquals(goog.dom.$('dotted_2'), found[1]);
  123     }
  124 
  125     function testCanFindElementByCssSelector() {
  126       if (!document['querySelectorAll']) {
  127         return;  // Skip this until we get selectors working on all browsers
  128       }
  129 
  130       var after = bot.locators.findElement({css: '#wrong'});
  131 
  132       assertNotNull(after);
  133       assertEquals('wrong', after.id);
  134     }
  135 
  136     function testShouldReturnNullIfNoCssMatchIsFound() {
  137       if (!document['querySelectorAll']) {
  138         return;  // Skip this until we get selectors working on all browsers
  139       }
  140 
  141       assertNull(bot.locators.findElement({css: '#uglyfish'}));
  142     }
  143 
  144     function testFindByCssSelectorDoesNotMatchSearchContextRoot() {
  145       var root = bot.locators.findElement({css: '#x'});
  146       assertNull(bot.locators.findElement({css: '#x'}, root));
  147       assertEquals(0, bot.locators.findElements({css: '#x'}, root).length);
  148     }
  149 
  150     function testShouldFindElementByMultipleSelectors() {
  151       if (!document['querySelectorAll']) {
  152           return;  // Skip this until we get selectors working on all browsers
  153       }
  154 
  155       var found = bot.locators.findElement({ css: '.dogs, #x' });
  156       assertNotNull(found);
  157       assertEquals('x', found.id);
  158     }
  159 
  160     function testShouldFindElementsByMultipleSelectors() {
  161       if (!document['querySelectorAll']) {
  162         return;  // Skip this until we get selectors working on all browsers
  163       }
  164 
  165       var found = bot.locators.findElements({ css: '.dogs, #x' });
  166       assertEquals(2, found.length);
  167       assertEquals('x', found[0].id);
  168       assertEquals('dogs', found[1].getAttribute('class'));
  169     }
  170 
  171     function testShouldFindElementWithCommaInAttribute() {
  172       assertNotNull(goog.bind(bot.locators.findElement, null,
  173           {css: 'comma-in-alt[alt="has, a comma"]'}));
  174     }
  175 
  176     function testCanLocateElementsUsingXPath() {
  177       var doggies = bot.locators.findElement({xpath: "//*[@id = 'after']"});
  178 
  179       assertNotNull(doggies);
  180       assertEquals('after', doggies.id);
  181     }
  182 
  183     function testCanLocateElementsUsingXPathInIframe() {
  184       var frameDoc = window.frames[0].document;
  185       var frameElement = bot.locators.findElement({xpath: '//body/h1'}, frameDoc);
  186       assertNotNull(frameElement);
  187       assertEquals('H1', frameElement.tagName);
  188     }
  189 
  190     function testWillReturnNullIfNoMatchUsingXPathIsFound() {
  191       assertNull(bot.locators.findElement({xpath: '//fish'}));
  192     }
  193 
  194     function testFindByXPathDescendantSelectorDoesNotMatchSearchContextRoot() {
  195       var root = bot.locators.findElement({xpath: '//p[@id = "x"]'});
  196       assertNull(bot.locators.findElement({xpath: './p'}, root));
  197       assertEquals(0, bot.locators.findElements({xpath: './p'}, root).length);
  198     }
  199 
  200     function testShouldThrowInvalidSelectorErrorWhenXPathIsSyntacticallyInvalidInSingle() {
  201       try {
  202         bot.locators.findElement({xpath: 'this][isnot][valid'});
  203         fail('Should not have succeeded because the xpath expression is ' +
  204                     'syntactically not correct');
  205       } catch (ex) {
  206         //We expect an InvalidSelectorException because the xpath expression is
  207         //syntactically invalid.
  208         assertEquals(bot.ErrorCode.INVALID_SELECTOR_ERROR, ex.code);
  209       }
  210     }
  211 
  212     function testShouldThrowInvalidSelectorErrorWhenXPathReturnsAWrongTypeInSingle() {
  213       try {
  214         bot.locators.findElement({xpath: 'count(//fish)'});
  215         fail('Should not have succeeded because the xpath expression does ' +
  216                     'not select an element.');
  217       } catch (ex) {
  218         // We expect an exception because the XPath expression
  219         // results in a number, not in an element.
  220         assertEquals(bot.ErrorCode.INVALID_SELECTOR_ERROR, ex.code);
  221       }
  222     }
  223 
  224     function testCanFindMoreThanOneElementByName() {
  225       var allFoos = bot.locators.findElements({name: 'foo'});
  226 
  227       assertEquals(2, allFoos.length);
  228     }
  229 
  230     function testCanFindManyElementsUsingCss() {
  231       if (!document['querySelectorAll']) {
  232         return;  // Skip this until we get selectors working on all browsers
  233       }
  234 
  235       var cats = bot.locators.findElements({css: '.cats'});
  236 
  237       assertEquals(3, cats.length);
  238     }
  239 
  240     function testCanFindManyElementsUsingClassName() {
  241       var cats = bot.locators.findElements({className: 'cats'});
  242 
  243       assertEquals(3, cats.length);
  244     }
  245 
  246     function testCanFindManyElementsUsingAnId() {
  247       var bad = bot.locators.findElements({id: 'illegal'});
  248 
  249       assertEquals(4, bad.length);
  250     }
  251 
  252     function testCanFindByIdWithSpecialCharacters() {
  253       function findById(id, expectedName) {
  254         var els = bot.locators.findElements({id: id});
  255         assertEquals("Expected exactly one element with ID " + id, 1, els.length);
  256         assertEquals(expectedName, els[0].getAttribute('name'));
  257 
  258         var el = bot.locators.findElement({id: id});
  259         assertNotNullNorUndefined("Expected element with ID " + id, el);
  260         assertEquals(expectedName, el.getAttribute('name'));
  261       }
  262 
  263       findById("'", 'apóstrofo');
  264       findById('"', 'comilla');
  265       findById('\\', 'barra_invertida');
  266       findById('s p a c e', 'space');
  267       findById('#.:;,!?+<>=~*^$|%&@`{}-/[]()', 'remaining_css_special_symbols');
  268     }
  269 
  270     function testCanFindManyElementsViaXPath() {
  271       var bad = bot.locators.findElements({xpath: '//*[@name = "after"]'});
  272 
  273       assertEquals(2, bad.length);
  274     }
  275 
  276     function testShouldThrowInvalidSelectorErrorWhenXPathIsSyntacticallyInvalidInMany() {
  277       try {
  278         bot.locators.findElements({xpath: 'this][isnot][valid'});
  279         fail('Should not have succeeded because the xpath expression is ' +
  280             'syntactically not correct.');
  281       } catch (ex) {
  282         // We expect an InvalidSelectorException because the xpath expression is
  283         // syntactically invalid.
  284         assertEquals(bot.ErrorCode.INVALID_SELECTOR_ERROR, ex.code);
  285       }
  286     }
  287 
  288     function testShouldThrowInvalidSelectorErrorWhenXPathReturnsAWrongTypeInMany() {
  289       try {
  290         bot.locators.findElements({xpath: 'count(//fish)'});
  291         fail('Should not have succeeded because the xpath expression does ' +
  292             'not select an element.');
  293       } catch (ex) {
  294         // We expect an exception because the XPath expression
  295         // results in a number, not in an element.
  296         assertEquals(bot.ErrorCode.INVALID_SELECTOR_ERROR, ex.code);
  297       }
  298     }
  299 
  300     function testCanFindElementByLinkText() {
  301       var link = bot.locators.findElement({linkText: 'this is a link'});
  302 
  303       assertEquals('link', bot.dom.getAttribute(link, 'id'));
  304     }
  305 
  306     function testCanFindElementsByLinkText() {
  307       var links = bot.locators.findElements({linkText: 'this is a link'});
  308 
  309       assertEquals(5, links.length);
  310     }
  311 
  312     function testShouldBeAbleToFindLinksWithNoText() {
  313       var link = bot.locators.findElement({linkText: ''});
  314       assertNotNull(link);
  315       assertEquals('empty-link', link.id);
  316 
  317       var links = bot.locators.findElements({linkText: ''});
  318       assertEquals(1, links.length);
  319       assertEquals('empty-link', links[0].id);
  320     }
  321 
  322     function testCanFindElementByPartialLinkText() {
  323       var link = bot.locators.findElement({partialLinkText: 'is a'});
  324 
  325       assertEquals('link', bot.dom.getAttribute(link, 'id'));
  326     }
  327 
  328     function testCanFindElementsByPartialLinkText() {
  329       var links = bot.locators.findElements({partialLinkText: 'a lin'});
  330 
  331       assertEquals(5, links.length);
  332     }
  333 
  334     function testShouldMatchFirstLinkWhenLinkTextIsTheEmptyString() {
  335       var link = bot.locators.findElement({partialLinkText: ''});
  336       assertNotNull(link);
  337       assertEquals('link', link.id);
  338     }
  339 
  340     function testShouldFindEveryLinkWhenLinkTextIsTheEmptyString() {
  341       var links = bot.locators.findElements({partialLinkText: ''});
  342       assertEquals(8, links.length);
  343     }
  344 
  345     function testShouldBeAbleToFindElementByTagName() {
  346       var link = bot.locators.findElement({tagName: 'A'});
  347       assertNotNull(link);
  348       assertEquals('link', link.id);
  349     }
  350 
  351     function testShouldBeAbleToFindElementsByTagName() {
  352       var links = bot.locators.findElements({tagName: 'A'});
  353       assertEquals(8, links.length);
  354     }
  355 
  356     function testShouldThrowInvalidSelectorErrorWhenEmptyStringPassedIn() {
  357       try {
  358         bot.locators.findElements({tagName: ''});
  359         fail('Should not have succeeded because the tagName is ""');
  360       } catch (ex) {
  361         // We expect an InvalidSelectorException because the xpath expression is
  362         // syntactically invalid.
  363         assertEquals(bot.ErrorCode.INVALID_SELECTOR_ERROR, ex.code);
  364       }
  365     }
  366 
  367     function testShouldThrowOnUnrecognizedLocatingStrategy() {
  368       try {
  369         bot.locators.findElement({cheese: 'brie'});
  370         fail('Should not have succeeded because the locating strategy is unknown');
  371       } catch (ex) {
  372         assertEquals(bot.ErrorCode.INVALID_ARGUMENT, ex.code);
  373       }
  374       try {
  375         bot.locators.findElements({cheese: 'brie'});
  376         fail('Should not have succeeded because the locating strategy is unknown');
  377       } catch (ex) {
  378         assertEquals(bot.ErrorCode.INVALID_ARGUMENT, ex.code);
  379       }
  380     }
  381 
  382     function testCanAddANewElementLocatingStrategy() {
  383       var expected = goog.dom.$('lion');
  384       bot.locators.add('fixed', {
  385         single: function() {
  386           return expected;
  387         }
  388       });
  389 
  390       var seen = bot.locators.findElement({fixed: 'ignored'});
  391 
  392       assertEquals(expected, seen);
  393     }
  394 
  395     function testShouldSurviveObjectPrototypeBeingModified() {
  396       try {
  397         Object.prototype.cheese = 'brie';
  398         Object.prototype.Inherits = function() { /* does nothing */ };
  399 
  400         var first = bot.locators.findElement({name: 'tiger'});
  401         assertEquals('tiger', first.getAttribute('name'));
  402 
  403         var locator = new Object();
  404         locator.name = 'tiger';
  405 
  406         var second = bot.locators.findElement(locator);
  407         assertEquals('tiger', second.getAttribute('name'));
  408       } finally {
  409         delete Object.prototype.cheese;
  410         delete Object.prototype.Inherits;
  411       }
  412     }
  413 
  414 </script>
  415 </head>
  416 <body>
  417   <p id="x" name="para">Para</p>
  418 
  419   <div name="after" id="wrong" class="feline cats">nope</div>
  420   <div name="right" id="after" class="dogs">yup</div>
  421   <div name="lion" class="cats">simba</div>
  422   <div name="tiger" class="cats">shere khan</div>
  423   <div id="dotted_1" class="name.with.dots">dotted class</div>
  424   <div id="dotted_2" class="name.with.dots">another dotted class</div>
  425 
  426   <form action="#">
  427     <input name="after" /><br />
  428     <input name="foo" />
  429   </form>
  430 
  431   <!-- This comment should be ignored -->
  432 
  433   <span name="foo">Furrfu</span>
  434 
  435   <ul>
  436     <li id="illegal">item
  437     <li id="illegal">item
  438     <li id="illegal">item
  439     <li id="illegal">item
  440   </ul>
  441   <div name="apóstrofo" id="'">apóstrofo</div>
  442   <div name="comilla" id='"'>comilla</div>
  443   <div name="barra_invertida" id="\">barra invertida</div>
  444   <div name="space" id="s p a c e">space</div>
  445   <div name="remaining_css_special_symbols" id="#.:;,!?+<>=~*^$|%&@`{}-/[]()">queso!</div>
  446 
  447   <a id="link" href="#">this is a link</a>
  448   <a name="fishsticks">this is a link</a>
  449   <a href="#">this is a link</a>
  450   <a href="#">this is a link</a>
  451   <a href="#">this is a link</a>
  452 
  453   <a href="#">unrelated</a>
  454   <a href="#" id="empty-link"></a>
  455   <a href="#" id="comma-in-alt" alt="has, a comma">has, a comma</a>
  456 
  457   <iframe src="./testdata/iframe_page.html"></iframe>
  458 </body>
  459 </html>