Button.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Button {
  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 button element locator
  33. */
  34. get element(): Locator {
  35. return this.locator;
  36. }
  37. /**
  38. * Clicks the button
  39. * @param options - Optional click options
  40. */
  41. async click(options?: {
  42. timeout?: number;
  43. force?: boolean;
  44. delay?: number;
  45. button?: 'left' | 'right' | 'middle';
  46. }): Promise<void> {
  47. await this.element.click(options);
  48. }
  49. /**
  50. * Gets the button text content
  51. */
  52. async getText(): Promise<string> {
  53. return (await this.element.textContent()) ?? '';
  54. }
  55. /**
  56. * Gets a specific attribute value from the button
  57. * @param attribute - The attribute name to retrieve
  58. */
  59. async getAttribute(attribute: string): Promise<string | null> {
  60. return this.element.getAttribute(attribute);
  61. }
  62. /**
  63. * Checks if the button is visible
  64. */
  65. async isVisible(): Promise<boolean> {
  66. return this.element.isVisible();
  67. }
  68. /**
  69. * Checks if the button is enabled
  70. */
  71. async isEnabled(): Promise<boolean> {
  72. return this.element.isEnabled();
  73. }
  74. /**
  75. * Checks if the button is disabled
  76. */
  77. async isDisabled(): Promise<boolean> {
  78. return this.element.isDisabled();
  79. }
  80. /**
  81. * Hovers over the button
  82. * @param options - Optional hover options
  83. */
  84. async hover(options?: { timeout?: number; force?: boolean }): Promise<void> {
  85. await this.element.hover(options);
  86. }
  87. /**
  88. * Focuses on the button
  89. */
  90. async focus(): Promise<void> {
  91. await this.element.focus();
  92. }
  93. /**
  94. * Double clicks the button
  95. * @param options - Optional click options
  96. */
  97. async doubleClick(options?: {
  98. timeout?: number;
  99. force?: boolean;
  100. delay?: number;
  101. }): Promise<void> {
  102. await this.element.dblclick(options);
  103. }
  104. }