Skip to content

Commit 552f034

Browse files
committed
refactor: replace constant with inline string for selector prefix in Selector class
1 parent 718c6ea commit 552f034

2 files changed

Lines changed: 37 additions & 57 deletions

File tree

src/Support/Selector.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,28 @@
66

77
final class Selector
88
{
9-
public const string SELECTOR_PREFIX = 'internal:';
10-
119
/**
1210
* Get selector by attribute text.
1311
*/
1412
public static function getByAttributeTextSelector(string $attrName, string $text, bool $exact = false): string
1513
{
16-
return self::SELECTOR_PREFIX."attr=[{$attrName}=".self::escapeForAttributeSelectorOrRegex($text, $exact).']';
14+
return 'internal:'."attr=[{$attrName}=".self::escapeForAttributeSelectorOrRegex($text, $exact).']';
1715
}
1816

1917
/**
2018
* Get selector by test ID.
2119
*/
2220
public static function getByTestIdSelector(string $testIdAttributeName, string $testId): string
2321
{
24-
return self::SELECTOR_PREFIX."testid=[{$testIdAttributeName}=".self::escapeForAttributeSelectorOrRegex($testId, true).']';
22+
return 'internal:'."testid=[{$testIdAttributeName}=".self::escapeForAttributeSelectorOrRegex($testId, true).']';
2523
}
2624

2725
/**
2826
* Get selector by label.
2927
*/
3028
public static function getByLabelSelector(string $text, bool $exact): string
3129
{
32-
return self::SELECTOR_PREFIX.'label='.self::escapeForTextSelector($text, $exact);
30+
return 'internal:'.'label='.self::escapeForTextSelector($text, $exact);
3331
}
3432

3533
/**
@@ -61,7 +59,7 @@ public static function getByPlaceholderSelector(string $text, bool $exact): stri
6159
*/
6260
public static function getByTextSelector(string $text, bool $exact): string
6361
{
64-
return self::SELECTOR_PREFIX.'text='.self::escapeForTextSelector($text, $exact);
62+
return 'internal:'.'text='.self::escapeForTextSelector($text, $exact);
6563
}
6664

6765
/**
@@ -148,6 +146,6 @@ public static function getByRoleSelector(string $role, array $options = []): str
148146
$propsStr .= '['.$k.'='.$v.']';
149147
}
150148

151-
return self::SELECTOR_PREFIX."role={$role}{$propsStr}";
149+
return 'internal:'."role={$role}{$propsStr}";
152150
}
153151
}

tests/Unit/Support/SelectorTest.php

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,109 +8,97 @@
88
it('returns correct selector without exact match', function (): void {
99
$selector = Selector::getByAttributeTextSelector('data-test', 'example', false);
1010

11-
expect($selector)->toBe('attr=[data-test="example"i]');
12-
});
13-
14-
it('returns correct selector with exact match', function (): void {
11+
expect($selector)->toBe('internal:attr=[data-test="example"i]');
12+
}); it('returns correct selector with exact match', function (): void {
1513
$selector = Selector::getByAttributeTextSelector('data-test', 'example', true);
1614

17-
expect($selector)->toBe('attr=[data-test="example"]');
18-
});
19-
20-
it('escapes special characters in attribute values', function (): void {
15+
expect($selector)->toBe('internal:attr=[data-test="example"]');
16+
}); it('escapes special characters in attribute values', function (): void {
2117
$selector = Selector::getByAttributeTextSelector('data-test', 'example "quoted" text', false);
2218

23-
expect($selector)->toBe('attr=[data-test="example \"quoted\" text"i]');
24-
});
25-
26-
it('escapes backslashes in attribute values', function (): void {
19+
expect($selector)->toBe('internal:attr=[data-test="example \"quoted\" text"i]');
20+
}); it('escapes backslashes in attribute values', function (): void {
2721
$selector = Selector::getByAttributeTextSelector('data-test', 'example\\path', false);
2822

29-
expect($selector)->toBe('attr=[data-test="example\\\\path"i]');
23+
expect($selector)->toBe('internal:attr=[data-test="example\\\\path"i]');
3024
});
3125
});
3226

33-
describe('getByTestIdSelector', function (): void {
34-
it('returns correct selector for test ID', function (): void {
27+
describe('getByTestIdSelector', function (): void { it('returns correct selector for test ID', function (): void {
3528
$selector = Selector::getByTestIdSelector('data-testid', 'login-button');
3629

37-
expect($selector)->toBe('testid=[data-testid="login-button"]');
38-
});
39-
40-
it('escapes special characters in test ID', function (): void {
30+
expect($selector)->toBe('internal:testid=[data-testid="login-button"]');
31+
}); it('escapes special characters in test ID', function (): void {
4132
$selector = Selector::getByTestIdSelector('data-testid', 'button"with"quotes');
4233

43-
expect($selector)->toBe('testid=[data-testid="button\"with\"quotes"]');
34+
expect($selector)->toBe('internal:testid=[data-testid="button\"with\"quotes"]');
4435
});
4536
});
4637

47-
describe('getByLabelSelector', function (): void {
48-
it('returns correct selector without exact match', function (): void {
38+
describe('getByLabelSelector', function (): void { it('returns correct selector without exact match', function (): void {
4939
$selector = Selector::getByLabelSelector('Email address', false);
5040

51-
expect($selector)->toBe('label="Email address"i');
52-
});
53-
54-
it('returns correct selector with exact match', function (): void {
41+
expect($selector)->toBe('internal:label="Email address"i');
42+
}); it('returns correct selector with exact match', function (): void {
5543
$selector = Selector::getByLabelSelector('Email address', true);
5644

57-
expect($selector)->toBe('label="Email address"s');
45+
expect($selector)->toBe('internal:label="Email address"s');
5846
});
5947
});
6048

6149
describe('getByAltTextSelector', function (): void {
6250
it('returns correct selector without exact match', function (): void {
6351
$selector = Selector::getByAltTextSelector('Logo image', false);
6452

65-
expect($selector)->toBe('attr=[alt="Logo image"i]');
53+
expect($selector)->toBe('internal:attr=[alt="Logo image"i]');
6654
});
6755

6856
it('returns correct selector with exact match', function (): void {
6957
$selector = Selector::getByAltTextSelector('Logo image', true);
7058

71-
expect($selector)->toBe('attr=[alt="Logo image"]');
59+
expect($selector)->toBe('internal:attr=[alt="Logo image"]');
7260
});
7361
});
7462

7563
describe('getByTitleSelector', function (): void {
7664
it('returns correct selector without exact match', function (): void {
7765
$selector = Selector::getByTitleSelector('Information', false);
7866

79-
expect($selector)->toBe('attr=[title="Information"i]');
67+
expect($selector)->toBe('internal:attr=[title="Information"i]');
8068
});
8169

8270
it('returns correct selector with exact match', function (): void {
8371
$selector = Selector::getByTitleSelector('Information', true);
8472

85-
expect($selector)->toBe('attr=[title="Information"]');
73+
expect($selector)->toBe('internal:attr=[title="Information"]');
8674
});
8775
});
8876

8977
describe('getByPlaceholderSelector', function (): void {
9078
it('returns correct selector without exact match', function (): void {
9179
$selector = Selector::getByPlaceholderSelector('Search...', false);
9280

93-
expect($selector)->toBe('attr=[placeholder="Search..."i]');
81+
expect($selector)->toBe('internal:attr=[placeholder="Search..."i]');
9482
});
9583

9684
it('returns correct selector with exact match', function (): void {
9785
$selector = Selector::getByPlaceholderSelector('Search...', true);
9886

99-
expect($selector)->toBe('attr=[placeholder="Search..."]');
87+
expect($selector)->toBe('internal:attr=[placeholder="Search..."]');
10088
});
10189
});
10290

10391
describe('getByTextSelector', function (): void {
10492
it('returns correct selector without exact match', function (): void {
10593
$selector = Selector::getByTextSelector('Submit form', false);
10694

107-
expect($selector)->toBe('text="Submit form"i');
95+
expect($selector)->toBe('internal:text="Submit form"i');
10896
});
10997

11098
it('returns correct selector with exact match', function (): void {
11199
$selector = Selector::getByTextSelector('Submit form', true);
112100

113-
expect($selector)->toBe('text="Submit form"s');
101+
expect($selector)->toBe('internal:text="Submit form"s');
114102
});
115103
});
116104

@@ -178,7 +166,7 @@
178166
it('returns basic role selector without options', function (): void {
179167
$selector = Selector::getByRoleSelector('button');
180168

181-
expect($selector)->toBe('role=button');
169+
expect($selector)->toBe('internal:role=button');
182170
});
183171

184172
it('handles boolean options correctly', function (): void {
@@ -187,15 +175,13 @@
187175
'disabled' => false,
188176
]);
189177

190-
expect($selector)->toBe('role=checkbox[checked=true][disabled=false]');
191-
});
192-
193-
it('handles name option without exact flag', function (): void {
178+
expect($selector)->toBe('internal:role=checkbox[checked=true][disabled=false]');
179+
}); it('handles name option without exact flag', function (): void {
194180
$selector = Selector::getByRoleSelector('button', [
195181
'name' => 'Submit form',
196182
]);
197183

198-
expect($selector)->toBe('role=button[name="Submit form"i]');
184+
expect($selector)->toBe('internal:role=button[name="Submit form"i]');
199185
});
200186

201187
it('handles name option with exact flag', function (): void {
@@ -204,10 +190,8 @@
204190
'exact' => true,
205191
]);
206192

207-
expect($selector)->toBe('role=button[name="Submit form"]');
208-
});
209-
210-
it('handles all possible options', function (): void {
193+
expect($selector)->toBe('internal:role=button[name="Submit form"]');
194+
}); it('handles all possible options', function (): void {
211195
$selector = Selector::getByRoleSelector('combobox', [
212196
'checked' => true,
213197
'disabled' => false,
@@ -219,14 +203,12 @@
219203
'pressed' => false,
220204
]);
221205

222-
expect($selector)->toBe('role=combobox[checked=true][disabled=false][selected=true][expanded=true][include-hidden=false][level=2][name="Country selector"i][pressed=false]');
223-
});
224-
225-
it('escapes special characters in name option', function (): void {
206+
expect($selector)->toBe('internal:role=combobox[checked=true][disabled=false][selected=true][expanded=true][include-hidden=false][level=2][name="Country selector"i][pressed=false]');
207+
}); it('escapes special characters in name option', function (): void {
226208
$selector = Selector::getByRoleSelector('button', [
227209
'name' => 'Button "with" quotes\\backslashes',
228210
]);
229211

230-
expect($selector)->toBe('role=button[name="Button \"with\" quotes\\\\backslashes"i]');
212+
expect($selector)->toBe('internal:role=button[name="Button \"with\" quotes\\\\backslashes"i]');
231213
});
232214
});

0 commit comments

Comments
 (0)