Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test SDK loading before widget tag #30

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions sdktest/render/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ func (r *TestCaseHandler) HandleTestCasePage(res http.ResponseWriter, req *http.
Name: params.Name,
Title: fmt.Sprintf("%s | sdktest", params.Name),

HTMLLang: params.Config.Language,
FriendlyCaptchaAPIEndpoint: params.Config.APIEndpoint,
HTMLLang: params.Config.Language,

Head: rd.Head,
Body: rd.Body,
Expand Down
10 changes: 10 additions & 0 deletions sdktest/sdktestlib/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ export class AssertLib {
});
}

async widgetInits(widget: WidgetHandle) {
return new Promise<FRCWidgetStateChangeEventData>((resolve) => {
widget.addEventListener("frc:widget.statechange", (ev) => {
if (ev.detail.state === "unactivated") {
resolve(ev.detail);
}
});
});
}

async widgetCompletes(widget: WidgetHandle) {
return new Promise<FRCWidgetCompleteEventData>((resolve) => {
widget.addEventListener("frc:widget.complete", (ev) => {
Expand Down
3 changes: 1 addition & 2 deletions sdktest/template/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ type TestCaseTemplateData struct {
Title string
Name string

HTMLLang string
FriendlyCaptchaAPIEndpoint string
HTMLLang string

Head []byte
Body []byte
Expand Down
1 change: 0 additions & 1 deletion sdktest/template/test.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="UTF-8">
<title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="frc-api-endpoint" content="{{ .FriendlyCaptchaAPIEndpoint }}"/>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🧪</text></svg>">
<link rel="stylesheet" href="/static/public/simple.css">
<link rel="stylesheet" href="/static/public/sdktestlib.css">
Expand Down
7 changes: 4 additions & 3 deletions sdktest/test/invalid_api_endpoint/body.tmpl.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<main>
<form>
<input type="textarea"/>
<div class="frc-captcha" data-sitekey="{{ .Config.Sitekey }}"></div>
<input type="submit"/>
<input type="textarea" />
<div class="frc-captcha" data-sitekey="{{ .Config.Sitekey }}" data-api-endpoint="{{ .Config.APIEndpoint }}">
</div>
<input type="submit" />
</form>
</main>

Expand Down
13 changes: 13 additions & 0 deletions sdktest/test/sdk_loaded_before_widget_tag/body.tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not execute async to force loading before the site content -->
<script src="{{ .SiteJSPath }}"></script>

<main>
<form>
<input type="textarea" />
<div class="frc-captcha" data-sitekey="{{ .Config.Sitekey }}" data-api-endpoint="{{ .Config.APIEndpoint }}">
</div>
<input type="submit" />
</form>
</main>

<script defer src="main.tmpl.ts"></script>
1 change: 1 addition & 0 deletions sdktest/test/sdk_loaded_before_widget_tag/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
api_endpoint: eu
25 changes: 25 additions & 0 deletions sdktest/test/sdk_loaded_before_widget_tag/main.tmpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*!
* Copyright (c) Friendly Captcha GmbH 2025.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { sdktest } from "../../sdktestlib/sdk.js";

sdktest.description(
"The site.js script is loaded before the widget tag that contains the EU API endpoint. After the widget tag is loaded, the EU agent should load and the widget should function normally.",
);

sdktest.test({ name: "one widget present" }, async (t) => {
t.require.numberOfWidgets(1);
});

sdktest.test({ name: "widget completes after focusing form" }, async (t) => {
const w = t.getWidget()!;
const completePromise = t.assert.widgetCompletes(w);

const ta: HTMLTextAreaElement = document.querySelector('input[type="textarea"]')!;
ta.focus();

await completePromise;
});
7 changes: 2 additions & 5 deletions sdktest/test/start_mode_auto/main.tmpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
import { sdktest } from "../../sdktestlib/sdk.js";


sdktest.test({ name: "one widget present" }, async (t) => {
t.require.numberOfWidgets(1);
});
Expand All @@ -17,8 +16,6 @@ sdktest.test({ name: "auto widget completes automatically" }, async (t) => {
if (w.getState() === "completed") {
// Already completed
} else {
const completePromise = t.assert.widgetCompletes(w)
await completePromise
}
await t.assert.widgetCompletes(w);
}
});

10 changes: 6 additions & 4 deletions sdktest/test/start_mode_none/main.tmpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
*/
import { sdktest } from "../../sdktestlib/sdk.js";


sdktest.test({ name: "one widget present" }, async (t) => {
t.require.numberOfWidgets(1);
});

sdktest.test({ name: "none widget is not triggered by focus" }, async (t) => {
const w = t.getWidget()!;

const ta: HTMLTextAreaElement = document.querySelector("input[type=\"textarea\"]")!;
const ta: HTMLTextAreaElement = document.querySelector('input[type="textarea"]')!;
ta.focus();

t.assert.equal(w.getState(), "unactivated")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was flaky for me and the state was in init a lot of times.

if (w.getState() === "unactivated") {
// Already unactivated (init done)
} else {
await t.assert.widgetInits(w);
}
});

Loading