Form.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import { Input } from './Input';
  21. import { Button } from './Button';
  22. export class Form {
  23. private readonly page: Page;
  24. private readonly locator: Locator;
  25. constructor(page: Page, selector: string);
  26. constructor(page: Page, locator: Locator);
  27. constructor(page: Page, selectorOrLocator: string | Locator) {
  28. this.page = page;
  29. if (typeof selectorOrLocator === 'string') {
  30. this.locator = page.locator(selectorOrLocator);
  31. } else {
  32. this.locator = selectorOrLocator;
  33. }
  34. }
  35. /**
  36. * Gets the form element locator
  37. */
  38. get element(): Locator {
  39. return this.locator;
  40. }
  41. /**
  42. * Gets an input field within the form (properly scoped)
  43. * @param inputSelector - Selector for the input field
  44. */
  45. getInput(inputSelector: string): Input {
  46. const scopedLocator = this.locator.locator(inputSelector);
  47. return new Input(this.page, scopedLocator);
  48. }
  49. /**
  50. * Gets a button within the form (properly scoped)
  51. * @param buttonSelector - Selector for the button
  52. */
  53. getButton(buttonSelector: string): Button {
  54. const scopedLocator = this.locator.locator(buttonSelector);
  55. return new Button(this.page, scopedLocator);
  56. }
  57. /**
  58. * Checks if the form is visible
  59. */
  60. async isVisible(): Promise<boolean> {
  61. return this.locator.isVisible();
  62. }
  63. /**
  64. * Submits the form (triggers submit event)
  65. */
  66. async submit(): Promise<void> {
  67. await this.locator.evaluate((form: HTMLElement) => {
  68. if (form instanceof HTMLFormElement) {
  69. form.submit();
  70. }
  71. });
  72. }
  73. /**
  74. * Waits for the form to be visible
  75. * @param options - Optional wait options
  76. */
  77. async waitForVisible(options?: { timeout?: number }): Promise<void> {
  78. await this.locator.waitFor({ state: 'visible', ...options });
  79. }
  80. /**
  81. * Gets all form data as key-value pairs
  82. * Useful for validation and debugging
  83. */
  84. async getFormData(): Promise<Record<string, string>> {
  85. return this.locator.evaluate((form: HTMLElement) => {
  86. if (form instanceof HTMLFormElement) {
  87. const formData = new FormData(form);
  88. const result: Record<string, string> = {};
  89. formData.forEach((value, key) => {
  90. result[key] = value.toString();
  91. });
  92. return result;
  93. }
  94. return {};
  95. });
  96. }
  97. }