selectors.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.SelectorsOwner = exports.Selectors = void 0;
  6. var _clientHelper = require("./clientHelper");
  7. var _channelOwner = require("./channelOwner");
  8. var _locator = require("./locator");
  9. /**
  10. * Copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. class Selectors {
  25. constructor() {
  26. this._channels = new Set();
  27. this._registrations = [];
  28. }
  29. async register(name, script, options = {}) {
  30. const source = await (0, _clientHelper.evaluationScript)(script, undefined, false);
  31. const params = {
  32. ...options,
  33. name,
  34. source
  35. };
  36. for (const channel of this._channels) await channel._channel.register(params);
  37. this._registrations.push(params);
  38. }
  39. setTestIdAttribute(attributeName) {
  40. (0, _locator.setTestIdAttribute)(attributeName);
  41. for (const channel of this._channels) channel._channel.setTestIdAttributeName({
  42. testIdAttributeName: attributeName
  43. }).catch(() => {});
  44. }
  45. _addChannel(channel) {
  46. this._channels.add(channel);
  47. for (const params of this._registrations) {
  48. // This should not fail except for connection closure, but just in case we catch.
  49. channel._channel.register(params).catch(() => {});
  50. channel._channel.setTestIdAttributeName({
  51. testIdAttributeName: (0, _locator.testIdAttributeName)()
  52. }).catch(() => {});
  53. }
  54. }
  55. _removeChannel(channel) {
  56. this._channels.delete(channel);
  57. }
  58. }
  59. exports.Selectors = Selectors;
  60. class SelectorsOwner extends _channelOwner.ChannelOwner {
  61. static from(browser) {
  62. return browser._object;
  63. }
  64. }
  65. exports.SelectorsOwner = SelectorsOwner;