test-autotest-vlid-resolution.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { TestRunner } from './src/tools/autotest/test-runner.js';
  2. function assert(condition, message) {
  3. if (!condition) throw new Error(message);
  4. }
  5. class FakeLocator {
  6. constructor(key, counts) {
  7. this.key = key;
  8. this.counts = counts;
  9. }
  10. first() {
  11. return this;
  12. }
  13. locator(childSelector) {
  14. return new FakeLocator(`${this.key} ${childSelector}`, this.counts);
  15. }
  16. filter({ hasText }) {
  17. return new FakeLocator(`${this.key}|text=${hasText}`, this.counts);
  18. }
  19. async count() {
  20. return this.counts[this.key] || 0;
  21. }
  22. async waitFor() {
  23. if (await this.count()) return;
  24. throw new Error('not attached');
  25. }
  26. }
  27. console.log('\n── AutoTest vlid Resolution ──');
  28. const runner = new TestRunner({ baseUrl: 'https://example.test/app' });
  29. runner.page = {
  30. locator(selector) {
  31. return new FakeLocator(selector, {
  32. '[data-vl-id="submitForm"] button': 1,
  33. '[data-vl-id="submitForm"]': 1,
  34. '[data-vl-id="navItem"]|text=课程管理': 1,
  35. });
  36. },
  37. getByRole() {
  38. return new FakeLocator('__missing__', {});
  39. },
  40. getByText() {
  41. return new FakeLocator('__missing__', {});
  42. },
  43. getByPlaceholder() {
  44. return new FakeLocator('__missing__', {});
  45. },
  46. getByLabel() {
  47. return new FakeLocator('__missing__', {});
  48. },
  49. };
  50. const submitLocator = await runner._resolveSelector('vlid:submitForm');
  51. assert(submitLocator.key === '[data-vl-id="submitForm"] button', 'vlid button should resolve to the clickable button child before the wrapper');
  52. const navLocator = await runner._resolveSelector('vlid:navItem[text="课程管理"]');
  53. assert(navLocator.key === '[data-vl-id="navItem"]|text=课程管理', 'hybrid vlid selector should preserve text disambiguation for repeated ids');
  54. const strictRunner = new TestRunner({ baseUrl: 'https://example.test/app' });
  55. strictRunner.page = {
  56. locator(selector) {
  57. return new FakeLocator(selector, {
  58. 'input:visible, textarea:visible': 1,
  59. });
  60. },
  61. getByRole() {
  62. return new FakeLocator('__missing__', {});
  63. },
  64. getByText() {
  65. return new FakeLocator('__missing__', {});
  66. },
  67. getByPlaceholder() {
  68. return new FakeLocator('__missing__', {});
  69. },
  70. getByLabel() {
  71. return new FakeLocator('__missing__', {});
  72. },
  73. };
  74. const strictInputLocator = await strictRunner._resolveSelector('vlid:formDescInput input');
  75. assert(
  76. strictInputLocator.key === '[data-vl-id="formDescInput"] input, [data-vl-id="formDescInput"] textarea, [data-vl-id="formDescInput"] [contenteditable="true"]',
  77. 'missing vlid input should stay strict on data-vl-id instead of degrading to a generic visible input'
  78. );
  79. const directInputRunner = new TestRunner({ baseUrl: 'https://example.test/app' });
  80. directInputRunner.page = {
  81. locator(selector) {
  82. return new FakeLocator(selector, {
  83. '[data-vl-id="formTitleInput"]': 1,
  84. });
  85. },
  86. getByRole() {
  87. return new FakeLocator('__missing__', {});
  88. },
  89. getByText() {
  90. return new FakeLocator('__missing__', {});
  91. },
  92. getByPlaceholder() {
  93. return new FakeLocator('__missing__', {});
  94. },
  95. getByLabel() {
  96. return new FakeLocator('__missing__', {});
  97. },
  98. };
  99. const directInputLocator = await directInputRunner._resolveSelector('vlid:formTitleInput input');
  100. assert(
  101. directInputLocator.key === '[data-vl-id="formTitleInput"]',
  102. 'vlid input selector should fall back to the base element when the data-vl-id is attached directly to the fillable element'
  103. );
  104. console.log('PASS test-autotest-vlid-resolution.js');