@@ -21,9 +21,14 @@ module [
21
21
maximizeWindow,
22
22
minimizeWindow,
23
23
fullScreenWindow,
24
+ executeJs,
25
+ executeJsWithOutput,
26
+ executeJsWithArgs,
27
+
24
28
]
25
29
26
30
import Effect
31
+ import CommonBrowser
27
32
import Internal exposing [Browser , Element ]
28
33
29
34
## Opens a new `Browser` window.
@@ -524,3 +529,83 @@ fullScreenWindow = \browser ->
524
529
[xVal, yVal, widthVal, heightVal] -> { x: xVal, y: yVal, width: widthVal |> Num . toU32 , height: heightVal |> Num . toU32 }
525
530
_ -> crash " the contract with host should not fail"
526
531
|> Task . mapErr WebDriverError
532
+
533
+ ## Execute JavaScript in the `Browser`.
534
+ ##
535
+ ## ```
536
+ ## browser |> Browser.executeJs! "console.log('wow')"
537
+ ## ```
538
+ executeJs : Browser , Str -> Task {} [WebDriverError Str , JsReturnTypeError Str ] where a implements Decoding
539
+ executeJs = \browser, script ->
540
+ _output : Str
541
+ _output = CommonBrowser . executeJs ! browser script
542
+ Task . ok {}
543
+
544
+ ## Execute JavaScript in the `Browser` and get the response.
545
+ ##
546
+ ## This function can be used with types like: `Bool`, `Str`, `I64`, `F64`, etc.
547
+ ## R2E will try to cast the browser response to the choosen type.
548
+ ##
549
+ ## When the response is empty e.g. property does not exist, then the default value of the choosen type will be used:
550
+ ## - `Str` - ""
551
+ ## - `Bool` - Bool.false
552
+ ## - `Num` - 0
553
+ ##
554
+ ## The output will be casted to expected Roc type:
555
+ ##
556
+ ## ```
557
+ ## response = browser |> Browser.executeJsWithOutput! "return 50 + 5;"
558
+ ## response |> Assert.shouldBe! 55
559
+ ##
560
+ ## response = browser |> Browser.executeJsWithOutput! "return 50.5 + 5;"
561
+ ## response |> Assert.shouldBe! 55.5
562
+ ##
563
+ ## response = browser |> Browser.executeJsWithOutput! "return 50.5 + 5;"
564
+ ## response |> Assert.shouldBe! "55.5"
565
+ ##
566
+ ## response = browser |> Browser.executeJsWithOutput! "return true"
567
+ ## response |> Assert.shouldBe! "true"
568
+ ##
569
+ ## response = browser |> Browser.executeJsWithOutput! "return true"
570
+ ## response |> Assert.shouldBe! Bool.true
571
+ ## ```
572
+ ##
573
+ ## The function can return a `Promise`.
574
+ executeJsWithOutput : Browser , Str -> Task a [WebDriverError Str , JsReturnTypeError Str ] where a implements Decoding
575
+ executeJsWithOutput = \browser, script ->
576
+ CommonBrowser . executeJs browser script
577
+
578
+ JsValue : [String Str , Number F64 , Boolean Bool , Null ]
579
+
580
+ ## Execute JavaScript in the `Browser` with arguments and get the response.
581
+ ##
582
+ ## This function can be used with types like: `Bool`, `Str`, `I64`, `F64`, etc.
583
+ ## R2E will try to cast the browser response to the choosen type.
584
+ ##
585
+ ## The arguments is a list of:
586
+ ##
587
+ ## ```
588
+ ## JsValue : [String Str, Number F64, Boolean Bool, Null]
589
+ ## ```
590
+ ##
591
+ ## When the response is empty e.g. property does not exist, then the default value of the choosen type will be used:
592
+ ## - `Str` - ""
593
+ ## - `Bool` - Bool.false
594
+ ## - `Num` - 0
595
+ ##
596
+ ## Args can only be used using the `arguments` array in js.
597
+ ##
598
+ ## The output will be casted to expected Roc type:
599
+ ##
600
+ ## ```
601
+ ## response = browser |> Browser.executeJsWithArgs! "return 50 + 5;" []
602
+ ## response |> Assert.shouldBe! 55
603
+ ##
604
+ ## response = browser |> Browser.executeJsWithArgs! "return 50.5 + 5;" [Number 55.5, String "5"]
605
+ ## response |> Assert.shouldBe! 55.5
606
+ ## ```
607
+ ##
608
+ ## The function can return a `Promise`.
609
+ executeJsWithArgs : Browser , Str , List JsValue -> Task a [WebDriverError Str , JsReturnTypeError Str ] where a implements Decoding
610
+ executeJsWithArgs = \browser, script, arguments ->
611
+ CommonBrowser . executeJsWithArgs browser script arguments
0 commit comments