Input.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. 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,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { Locator, Page } from '@playwright/test';
  20. export class Input {
  21. private readonly locator: Locator;
  22. constructor(page: Page, selector: string);
  23. constructor(page: Page, locator: Locator);
  24. constructor(page: Page, selectorOrLocator: string | Locator) {
  25. if (typeof selectorOrLocator === 'string') {
  26. this.locator = page.locator(selectorOrLocator);
  27. } else {
  28. this.locator = selectorOrLocator;
  29. }
  30. }
  31. /**
  32. * Gets the input element locator
  33. */
  34. get element(): Locator {
  35. return this.locator;
  36. }
  37. /**
  38. * Fast fill - clears the input and sets the value directly
  39. * @param value - The value to fill
  40. * @param options - Optional fill options
  41. */
  42. async fill(
  43. value: string,
  44. options?: { timeout?: number; force?: boolean },
  45. ): Promise<void> {
  46. await this.element.fill(value, options);
  47. }
  48. /**
  49. * Types text character by character (simulates real typing)
  50. * @param text - The text to type
  51. * @param options - Optional typing options
  52. */
  53. async type(text: string, options?: { delay?: number }): Promise<void> {
  54. await this.element.type(text, options);
  55. }
  56. /**
  57. * Types text sequentially with more control over timing
  58. * @param text - The text to type
  59. * @param options - Optional sequential typing options
  60. */
  61. async pressSequentially(
  62. text: string,
  63. options?: { delay?: number },
  64. ): Promise<void> {
  65. await this.element.pressSequentially(text, options);
  66. }
  67. /**
  68. * Gets the current value of the input
  69. */
  70. async getValue(): Promise<string> {
  71. return this.element.inputValue();
  72. }
  73. /**
  74. * Clears the input field
  75. */
  76. async clear(): Promise<void> {
  77. await this.element.clear();
  78. }
  79. /**
  80. * Checks if the input is visible
  81. */
  82. async isVisible(): Promise<boolean> {
  83. return this.element.isVisible();
  84. }
  85. /**
  86. * Checks if the input is enabled
  87. */
  88. async isEnabled(): Promise<boolean> {
  89. return this.element.isEnabled();
  90. }
  91. /**
  92. * Focuses on the input field
  93. */
  94. async focus(): Promise<void> {
  95. await this.element.focus();
  96. }
  97. }