by_test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 'use strict';
  18. var assert = require('assert');
  19. var by = require('../../lib/by');
  20. describe('by', function() {
  21. describe('By', function() {
  22. describe('className', function() {
  23. it('delegates to By.css', function() {
  24. let locator = by.By.className('foo');
  25. assert.equal('css selector', locator.using);
  26. assert.equal('.foo', locator.value);
  27. });
  28. it('escapes class name', function() {
  29. let locator = by.By.className('foo#bar');
  30. assert.equal('css selector', locator.using);
  31. assert.equal('.foo\\#bar', locator.value);
  32. });
  33. it('translates compound class names', function() {
  34. let locator = by.By.className('a b');
  35. assert.equal('css selector', locator.using);
  36. assert.equal('.a.b', locator.value);
  37. locator = by.By.className(' x y z-1 "g" ');
  38. assert.equal('css selector', locator.using);
  39. assert.equal('.x.y.z-1.\\"g\\"', locator.value);
  40. });
  41. });
  42. describe('id', function() {
  43. it('delegates to By.css', function() {
  44. let locator = by.By.id('foo');
  45. assert.equal('css selector', locator.using);
  46. assert.equal('*[id="foo"]', locator.value);
  47. });
  48. it('escapes the ID', function() {
  49. let locator = by.By.id('foo#bar');
  50. assert.equal('css selector', locator.using);
  51. assert.equal('*[id="foo\\#bar"]', locator.value);
  52. });
  53. });
  54. describe('name', function() {
  55. it('delegates to By.css', function() {
  56. let locator = by.By.name('foo')
  57. assert.equal('css selector', locator.using);
  58. assert.equal('*[name="foo"]', locator.value);
  59. });
  60. it('escapes the name', function() {
  61. let locator = by.By.name('foo"bar"')
  62. assert.equal('css selector', locator.using);
  63. assert.equal('*[name="foo\\"bar\\""]', locator.value);
  64. });
  65. it('escapes the name when it starts with a number', function() {
  66. let locator = by.By.name('123foo"bar"')
  67. assert.equal('css selector', locator.using);
  68. assert.equal('*[name="\\31 23foo\\"bar\\""]', locator.value);
  69. });
  70. it('escapes the name when it starts with a negative number', function() {
  71. let locator = by.By.name('-123foo"bar"')
  72. assert.equal('css selector', locator.using);
  73. assert.equal('*[name="-\\31 23foo\\"bar\\""]', locator.value);
  74. });
  75. });
  76. });
  77. describe('checkedLocator', function() {
  78. it('accepts a By instance', function() {
  79. let original = by.By.name('foo');
  80. let locator = by.checkedLocator(original);
  81. assert.strictEqual(locator, original);
  82. });
  83. it('accepts custom locator functions', function() {
  84. let original = function() {};
  85. let locator = by.checkedLocator(original);
  86. assert.strictEqual(locator, original);
  87. });
  88. // See https://github.com/SeleniumHQ/selenium/issues/3056
  89. it('accepts By-like objects', function() {
  90. let fakeBy = {using: 'id', value: 'foo'};
  91. let locator = by.checkedLocator(fakeBy);
  92. assert.strictEqual(locator.constructor, by.By);
  93. assert.equal(locator.using, 'id');
  94. assert.equal(locator.value, 'foo');
  95. });
  96. it('accepts class name', function() {
  97. let locator = by.checkedLocator({className: 'foo'});
  98. assert.equal('css selector', locator.using);
  99. assert.equal('.foo', locator.value);
  100. });
  101. it('accepts css', function() {
  102. let locator = by.checkedLocator({css: 'a > b'});
  103. assert.equal('css selector', locator.using);
  104. assert.equal('a > b', locator.value);
  105. });
  106. it('accepts id', function() {
  107. let locator = by.checkedLocator({id: 'foobar'});
  108. assert.equal('css selector', locator.using);
  109. assert.equal('*[id="foobar"]', locator.value);
  110. });
  111. it('accepts linkText', function() {
  112. let locator = by.checkedLocator({linkText: 'hello'});
  113. assert.equal('link text', locator.using);
  114. assert.equal('hello', locator.value);
  115. });
  116. it('accepts name', function() {
  117. let locator = by.checkedLocator({name: 'foobar'});
  118. assert.equal('css selector', locator.using);
  119. assert.equal('*[name="foobar"]', locator.value);
  120. });
  121. it('accepts partialLinkText', function() {
  122. let locator = by.checkedLocator({partialLinkText: 'hello'});
  123. assert.equal('partial link text', locator.using);
  124. assert.equal('hello', locator.value);
  125. });
  126. it('accepts tagName', function() {
  127. let locator = by.checkedLocator({tagName: 'div'});
  128. assert.equal('css selector', locator.using);
  129. assert.equal('div', locator.value);
  130. });
  131. it('accepts xpath', function() {
  132. let locator = by.checkedLocator({xpath: '//div[1]'});
  133. assert.equal('xpath', locator.using);
  134. assert.equal('//div[1]', locator.value);
  135. });
  136. });
  137. });