@@ -197,12 +197,19 @@ test.describe("Forms", () => {
197
197
198
198
"app/routes/blog._index.tsx" : js `
199
199
import { Form } from "react-router";
200
+
201
+ export function loader() {
202
+ return { timestamp: Date.now() }
203
+ }
204
+
200
205
export function action() {
201
206
return { ok: true };
202
207
}
203
- export default function() {
208
+
209
+ export default function Component({ loaderData }) {
204
210
return (
205
211
<>
212
+ <div id="timestamp">{loaderData.timestamp}</div>
206
213
<Form id="${ INDEX_ROUTE_NO_ACTION } ">
207
214
<input type="hidden" name="foo" defaultValue="1" />
208
215
<button>Submit</button>
@@ -525,15 +532,15 @@ test.describe("Forms", () => {
525
532
} ) => {
526
533
let app = new PlaywrightFixture ( appFixture , page ) ;
527
534
await app . goto ( "/get-submission" ) ;
528
- await app . clickElement ( `#${ FORM_WITH_ACTION_INPUT } button` ) ;
529
- await page . waitForSelector ( `pre:has-text("${ EAT } ")` ) ;
535
+ await page . locator ( `#${ FORM_WITH_ACTION_INPUT } button` ) . click ( ) ;
536
+ await page . locator ( `pre:has-text("${ EAT } ")` ) . waitFor ( ) ;
530
537
} ) ;
531
538
532
539
test ( "posts to a loader with button data with click" , async ( { page } ) => {
533
540
let app = new PlaywrightFixture ( appFixture , page ) ;
534
541
await app . goto ( "/get-submission" ) ;
535
- await app . clickElement ( "#buttonWithValue" ) ;
536
- await page . waitForSelector ( `pre:has-text("${ LAKSA } ")` ) ;
542
+ await page . locator ( "#buttonWithValue" ) . click ( ) ;
543
+ await page . locator ( `pre:has-text("${ LAKSA } ")` ) . waitFor ( ) ;
537
544
} ) ;
538
545
539
546
test ( "posts to a loader with button data with keyboard" , async ( {
@@ -553,16 +560,16 @@ test.describe("Forms", () => {
553
560
test ( "posts with the correct checkbox data" , async ( { page } ) => {
554
561
let app = new PlaywrightFixture ( appFixture , page ) ;
555
562
await app . goto ( "/get-submission" ) ;
556
- await app . clickElement ( `#${ CHECKBOX_BUTTON } ` ) ;
557
- await page . waitForSelector ( `pre:has-text("${ LAKSA } ")` ) ;
558
- await page . waitForSelector ( `pre:has-text("${ CHEESESTEAK } ")` ) ;
563
+ await page . locator ( `#${ CHECKBOX_BUTTON } ` ) . click ( ) ;
564
+ await page . locator ( `pre:has-text("${ LAKSA } ")` ) . waitFor ( ) ;
565
+ await page . locator ( `pre:has-text("${ CHEESESTEAK } ")` ) . waitFor ( ) ;
559
566
} ) ;
560
567
561
568
test ( "posts button data from outside the form" , async ( { page } ) => {
562
569
let app = new PlaywrightFixture ( appFixture , page ) ;
563
570
await app . goto ( "/get-submission" ) ;
564
- await app . clickElement ( `#${ ORPHAN_BUTTON } ` ) ;
565
- await page . waitForSelector ( `pre:has-text("${ SQUID_INK_HOTDOG } ")` ) ;
571
+ await page . locator ( `#${ ORPHAN_BUTTON } ` ) . click ( ) ;
572
+ await page . locator ( `pre:has-text("${ SQUID_INK_HOTDOG } ")` ) . waitFor ( ) ;
566
573
} ) ;
567
574
568
575
test (
@@ -793,49 +800,53 @@ test.describe("Forms", () => {
793
800
} ) => {
794
801
let app = new PlaywrightFixture ( appFixture , page ) ;
795
802
803
+ const timestamp = page . locator ( `#timestamp` ) ;
804
+ const form = page . locator ( `#${ INDEX_ROUTE_NO_ACTION } ` ) ;
805
+ const submit = page . locator ( `#${ INDEX_ROUTE_NO_ACTION } button` ) ;
806
+
796
807
// Start with a query param
797
808
await app . goto ( "/blog?junk=1" ) ;
798
- let html = await app . getHtml ( ) ;
799
- let el = getElement ( html , `#${ INDEX_ROUTE_NO_ACTION } ` ) ;
800
- expect ( el . attr ( "action" ) ) . toBe ( "/blog?index&junk=1" ) ;
801
- expect ( app . page . url ( ) ) . toMatch ( / \/ b l o g \? j u n k = 1 $ / ) ;
809
+ const t0 = await timestamp . innerText ( ) ;
810
+ await expect ( form ) . toHaveAttribute ( "action" , "/blog?index&junk=1" ) ;
811
+ expect ( page . url ( ) ) . toMatch ( / \/ b l o g \? j u n k = 1 $ / ) ;
802
812
803
813
// On submission, we replace existing parameters (reflected in the
804
814
// form action) with the values from the form data. We also do not
805
815
// need to preserve the index param in the URL on GET submissions
806
- await app . clickElement ( `#${ INDEX_ROUTE_NO_ACTION } button` ) ;
807
- html = await app . getHtml ( ) ;
808
- el = getElement ( html , `#${ INDEX_ROUTE_NO_ACTION } ` ) ;
809
- expect ( el . attr ( "action" ) ) . toBe ( "/blog?index&foo=1" ) ;
810
- expect ( app . page . url ( ) ) . toMatch ( / \/ b l o g \? f o o = 1 $ / ) ;
816
+ await submit . click ( ) ;
817
+ const t1 = await timestamp . filter ( { hasNotText : t0 } ) . innerText ( ) ;
818
+ await expect ( form ) . toHaveAttribute ( "action" , "/blog?index&foo=1" ) ;
819
+ expect ( page . url ( ) ) . toMatch ( / \/ b l o g \? f o o = 1 $ / ) ;
811
820
812
821
// Does not append duplicate params on re-submissions
813
- await app . clickElement ( `#${ INDEX_ROUTE_NO_ACTION } button` ) ;
814
- html = await app . getHtml ( ) ;
815
- el = getElement ( html , `#${ INDEX_ROUTE_NO_ACTION } ` ) ;
816
- expect ( el . attr ( "action" ) ) . toBe ( "/blog?index&foo=1" ) ;
817
- expect ( app . page . url ( ) ) . toMatch ( / \/ b l o g \? f o o = 1 $ / ) ;
822
+ await submit . click ( ) ;
823
+ await timestamp . filter ( { hasNotText : t1 } ) . innerText ( ) ;
824
+ await expect ( form ) . toHaveAttribute ( "action" , "/blog?index&foo=1" ) ;
825
+ expect ( page . url ( ) ) . toMatch ( / \/ b l o g \? f o o = 1 $ / ) ;
818
826
} ) ;
819
827
820
828
test ( "handles search params correctly on POST submissions" , async ( {
821
829
page,
822
830
} ) => {
823
831
let app = new PlaywrightFixture ( appFixture , page ) ;
824
832
833
+ const timestamp = page . locator ( `#timestamp` ) ;
834
+ const form = page . locator ( `#${ INDEX_ROUTE_NO_ACTION_POST } ` ) ;
835
+ const submit = page . locator ( `#${ INDEX_ROUTE_NO_ACTION_POST } button` ) ;
836
+
825
837
// Start with a query param
826
838
await app . goto ( "/blog?junk=1" ) ;
827
- let html = await app . getHtml ( ) ;
828
- let el = getElement ( html , `#${ INDEX_ROUTE_NO_ACTION_POST } ` ) ;
829
- expect ( el . attr ( "action" ) ) . toBe ( "/blog?index&junk=1" ) ;
830
- expect ( app . page . url ( ) ) . toMatch ( / \/ b l o g \? j u n k = 1 $ / ) ;
839
+ const t0 = await timestamp . innerText ( ) ;
840
+ await expect ( form ) . toHaveAttribute ( "action" , "/blog?index&junk=1" ) ;
841
+ expect ( page . url ( ) ) . toMatch ( / \/ b l o g \? j u n k = 1 $ / ) ;
831
842
832
843
// Form action reflects the current params and change them on submission
833
- await app . clickElement ( `# ${ INDEX_ROUTE_NO_ACTION_POST } button` ) ;
834
- html = await app . getHtml ( ) ;
835
- el = getElement ( html , `# ${ INDEX_ROUTE_NO_ACTION_POST } ` ) ;
836
- expect ( el . attr ( "action" ) ) . toBe ( "/blog?index&junk=1" ) ;
844
+ await submit . click ( ) ;
845
+ await timestamp . filter ( { hasNotText : t0 } ) . innerText ( ) ;
846
+ await expect ( form ) . toHaveAttribute ( "action" , "/blog?index&junk=1" ) ;
847
+
837
848
await page . waitForURL ( / \/ b l o g \? i n d e x & j u n k = 1 $ / ) ;
838
- expect ( app . page . url ( ) ) . toMatch ( / \/ b l o g \? i n d e x & j u n k = 1 $ / ) ;
849
+ expect ( page . url ( ) ) . toMatch ( / \/ b l o g \? i n d e x & j u n k = 1 $ / ) ;
839
850
} ) ;
840
851
} ) ;
841
852
@@ -993,16 +1004,11 @@ test.describe("Forms", () => {
993
1004
994
1005
let app = new PlaywrightFixture ( appFixture , page ) ;
995
1006
await app . goto ( `/form-method?method=${ method } ` , true ) ;
996
- await app . clickElement ( `text= Submit` ) ;
1007
+ await page . getByText ( " Submit" , { exact : true } ) . click ( ) ;
997
1008
if ( method !== "GET" ) {
998
- await page . waitForSelector ( "#action-method" ) ;
999
- expect ( await app . getHtml ( "pre#action-method" ) ) . toBe (
1000
- `<pre id="action-method">${ method } </pre>`
1001
- ) ;
1009
+ await expect ( page . locator ( "#action-method" ) ) . toHaveText ( method ) ;
1002
1010
}
1003
- expect ( await app . getHtml ( "pre#loader-method" ) ) . toBe (
1004
- `<pre id="loader-method">GET</pre>`
1005
- ) ;
1011
+ await expect ( page . locator ( "#loader-method" ) ) . toHaveText ( "GET" ) ;
1006
1012
} ) ;
1007
1013
} ) ;
1008
1014
} ) ;
@@ -1020,16 +1026,13 @@ test.describe("Forms", () => {
1020
1026
`/form-method?method=${ method } &submitterFormMethod=${ overrideMethod } ` ,
1021
1027
true
1022
1028
) ;
1023
- await app . clickElement ( `text=Submit with ${ overrideMethod } ` ) ;
1029
+ await page . locator ( `text=Submit with ${ overrideMethod } ` ) . click ( ) ;
1024
1030
if ( overrideMethod !== "GET" ) {
1025
- await page . waitForSelector ( "#action-method" ) ;
1026
- expect ( await app . getHtml ( "pre#action-method" ) ) . toBe (
1027
- `<pre id="action-method">${ overrideMethod } </pre>`
1031
+ await expect ( page . locator ( "pre#action-method" ) ) . toHaveText (
1032
+ overrideMethod
1028
1033
) ;
1029
1034
}
1030
- expect ( await app . getHtml ( "pre#loader-method" ) ) . toBe (
1031
- `<pre id="loader-method">GET</pre>`
1032
- ) ;
1035
+ await expect ( page . locator ( "pre#loader-method" ) ) . toHaveText ( "GET" ) ;
1033
1036
} ) ;
1034
1037
} ) ;
1035
1038
} ) ;
@@ -1039,33 +1042,33 @@ test.describe("Forms", () => {
1039
1042
} ) => {
1040
1043
let app = new PlaywrightFixture ( appFixture , page ) ;
1041
1044
1045
+ const formData = page . locator ( "#formData" ) ;
1046
+
1042
1047
await app . goto ( "/submitter" ) ;
1043
- await app . clickElement ( "text=Add Task" ) ;
1044
- expect ( ( await app . getElement ( "# formData" ) ) . val ( ) ) . toBe (
1048
+ await page . locator ( "text=Add Task" ) . click ( ) ;
1049
+ await expect ( formData ) . toHaveValue (
1045
1050
"tasks=first&tasks=second&tasks=&tasks=last"
1046
1051
) ;
1047
1052
1048
1053
await app . goto ( "/submitter" ) ;
1049
- await app . clickElement ( "text=No Name" ) ;
1050
- expect ( ( await app . getElement ( "#formData" ) ) . val ( ) ) . toBe (
1051
- "tasks=first&tasks=second&tasks=last"
1052
- ) ;
1054
+ await page . locator ( "text=No Name" ) . click ( ) ;
1055
+ await expect ( formData ) . toHaveValue ( "tasks=first&tasks=second&tasks=last" ) ;
1053
1056
1054
1057
await app . goto ( "/submitter" ) ;
1055
- await app . clickElement ( "[alt='Add Task']" ) ;
1056
- expect ( ( await app . getElement ( "# formData" ) ) . val ( ) ) . toMatch (
1058
+ await page . locator ( "[alt='Add Task']" ) . click ( ) ;
1059
+ await expect ( formData ) . toHaveValue (
1057
1060
/ ^ t a s k s = f i r s t & t a s k s = s e c o n d & t a s k s .x = \d + & t a s k s .y = \d + & t a s k s = l a s t $ /
1058
1061
) ;
1059
1062
1060
1063
await app . goto ( "/submitter" ) ;
1061
- await app . clickElement ( "[alt='No Name']" ) ;
1062
- expect ( ( await app . getElement ( "# formData" ) ) . val ( ) ) . toMatch (
1064
+ await page . locator ( "[alt='No Name']" ) . click ( ) ;
1065
+ await expect ( formData ) . toHaveValue (
1063
1066
/ ^ t a s k s = f i r s t & t a s k s = s e c o n d & x = \d + & y = \d + & t a s k s = l a s t $ /
1064
1067
) ;
1065
1068
1066
1069
await app . goto ( "/submitter" ) ;
1067
- await app . clickElement ( "text=Outside" ) ;
1068
- expect ( ( await app . getElement ( "# formData" ) ) . val ( ) ) . toBe (
1070
+ await page . locator ( "text=Outside" ) . click ( ) ;
1071
+ await expect ( formData ) . toHaveValue (
1069
1072
"tasks=outside&tasks=first&tasks=second&tasks=last"
1070
1073
) ;
1071
1074
} ) ;
@@ -1076,23 +1079,23 @@ test.describe("Forms", () => {
1076
1079
let app = new PlaywrightFixture ( appFixture , page ) ;
1077
1080
let myFile = fixture . projectDir + "/myfile.txt" ;
1078
1081
1082
+ const formData = page . locator ( "#formData" ) ;
1083
+ const submit = page . locator ( "button" ) ;
1084
+
1079
1085
await app . goto ( "/file-upload" ) ;
1080
1086
await app . uploadFile ( `[name=filey]` , myFile ) ;
1081
1087
await app . uploadFile ( `[name=filey2]` , myFile , myFile ) ;
1082
- await app . clickElement ( "button" ) ;
1083
- await page . waitForSelector ( "#formData" ) ;
1084
-
1085
- expect ( ( await app . getElement ( "#formData" ) ) . val ( ) ) . toBe (
1088
+ await submit . click ( ) ;
1089
+ await expect ( formData ) . toHaveValue (
1086
1090
"filey=myfile.txt&filey2=myfile.txt&filey2=myfile.txt&filey3="
1087
1091
) ;
1088
1092
1089
1093
await app . goto ( "/file-upload?method=post" ) ;
1090
1094
await app . uploadFile ( `[name=filey]` , myFile ) ;
1091
1095
await app . uploadFile ( `[name=filey2]` , myFile , myFile ) ;
1092
- await app . clickElement ( "button" ) ;
1093
- await page . waitForSelector ( "#formData" ) ;
1096
+ await submit . click ( ) ;
1094
1097
1095
- expect ( ( await app . getElement ( "# formData" ) ) . val ( ) ) . toBe (
1098
+ await expect ( formData ) . toHaveValue (
1096
1099
"filey=myfile.txt&filey2=myfile.txt&filey2=myfile.txt&filey3="
1097
1100
) ;
1098
1101
} ) ;
@@ -1119,20 +1122,25 @@ test.describe("Forms", () => {
1119
1122
} ) => {
1120
1123
let app = new PlaywrightFixture ( appFixture , page ) ;
1121
1124
await app . goto ( "/pathless-layout-parent/nested" ) ;
1122
- let html = await app . getHtml ( ) ;
1123
- expect ( html ) . toMatch ( "Pathless Layout Parent" ) ;
1124
- expect ( html ) . toMatch ( "Pathless Layout " ) ;
1125
- expect ( html ) . toMatch ( "Pathless Layout Index" ) ;
1126
1125
1127
- let el = getElement ( html , `form` ) ;
1128
- expect ( el . attr ( "action" ) ) . toBe ( "/pathless-layout-parent" ) ;
1126
+ await expect (
1127
+ page . getByText ( "Pathless Layout Parent" , { exact : true } )
1128
+ ) . toBeVisible ( ) ;
1129
+ await expect (
1130
+ page . getByText ( "Pathless Layout" , { exact : true } )
1131
+ ) . toBeVisible ( ) ;
1132
+ await expect (
1133
+ page . getByText ( "Pathless Layout Index" , { exact : true } )
1134
+ ) . toBeVisible ( ) ;
1135
+
1136
+ const form = page . locator ( "form" ) ;
1137
+ await expect ( form ) . toHaveAttribute ( "action" , "/pathless-layout-parent" ) ;
1129
1138
1130
- expect ( await app . getHtml ( ) ) . toMatch ( "Submitted - No" ) ;
1139
+ await expect ( page . getByText ( "Submitted - No" ) ) . toBeVisible ( ) ;
1131
1140
// This submission should ignore the index route and the pathless layout
1132
1141
// route above it and hit the action in routes/pathless-layout-parent.jsx
1133
- await app . clickSubmitButton ( "/pathless-layout-parent" ) ;
1134
- await page . waitForSelector ( "text=Submitted - Yes" ) ;
1135
- expect ( await app . getHtml ( ) ) . toMatch ( "Submitted - Yes" ) ;
1142
+ await page . getByRole ( "button" ) . click ( ) ;
1143
+ await expect ( page . getByText ( "Submitted - Yes" ) ) . toBeVisible ( ) ;
1136
1144
} ) ;
1137
1145
}
1138
1146
} ) ;
0 commit comments