Best K6 code snippet using js.Hint
webview.go
Source:webview.go
...45func init() {46 // Ensure that main.main is called from the main thread47 runtime.LockOSThread()48}49// Hints are used to configure window sizing and resizing50type Hint int51const (52 // Width and height are default size53 HintNone = C.WEBVIEW_HINT_NONE54 // Window size can not be changed by a user55 HintFixed = C.WEBVIEW_HINT_FIXED56 // Width and height are minimum bounds57 HintMin = C.WEBVIEW_HINT_MIN58 // Width and height are maximum bounds59 HintMax = C.WEBVIEW_HINT_MAX60)61type WebView interface {62 // Run runs the main loop until it's terminated. After this function exits -63 // you must destroy the webview.64 Run()65 // Terminate stops the main loop. It is safe to call this function from66 // a background thread.67 Terminate()68 //show webview windows69 Show()70 //hide webview windows71 Hide()72 // Dispatch posts a function to be executed on the main thread. You normally73 // do not need to call this function, unless you want to tweak the native74 // window.75 Dispatch(f func())76 // Destroy destroys a webview and closes the native window.77 Destroy()78 // Window returns a native window handle pointer. When using GTK backend the79 // pointer is GtkWindow pointer, when using Cocoa backend the pointer is80 // NSWindow pointer, when using Win32 backend the pointer is HWND pointer.81 Window() unsafe.Pointer82 // SetTitle updates the title of the native window. Must be called from the UI83 // thread.84 SetTitle(title string)85 // SetSize updates native window size. See Hint constants.86 SetSize(w int, h int, hint Hint)87 // SetIcon.88 SetIcon(iconBytes []byte)89 // Navigate navigates webview to the given URL. URL may be a data URI, i.e.90 // "data:text/text,<html>...</html>". It is often ok not to url-encode it91 // properly, webview will re-encode it for you.92 Navigate(url string)93 // Init injects JavaScript code at the initialization of the new page. Every94 // time the webview will open a the new page - this initialization code will95 // be executed. It is guaranteed that code is executed before window.onload.96 Init(js string)97 // Eval evaluates arbitrary JavaScript code. Evaluation happens asynchronously,98 // also the result of the expression is ignored. Use RPC bindings if you want99 // to receive notifications about the results of the evaluation.100 Eval(js string)101 // Bind binds a callback function so that it will appear under the given name102 // as a global JavaScript function. Internally it uses webview_init().103 // Callback receives a request string and a user-provided argument pointer.104 // Request string is a JSON array of all the arguments passed to the105 // JavaScript function.106 //107 // f must be a function108 // f must return either value and error or just error109 Bind(name string, f interface{}) error110}111type webview struct {112 w C.webview_t113}114var (115 m sync.Mutex116 index uintptr117 dispatch = map[uintptr]func(){}118 bindings = map[uintptr]func(id, req string) (interface{}, error){}119)120func boolToInt(b bool) C.int {121 if b {122 return 1123 }124 return 0125}126// New calls NewWindow to create a new window and a new webview instance. If debug127// is non-zero - developer tools will be enabled (if the platform supports them).128func New(width int, height int, hide bool, debug bool) WebView {129 w := &webview{}130 w.w = C.webview_create(C.int(width), C.int(height), boolToInt(hide), boolToInt(debug))131 return w132}133func (w *webview) Destroy() {134 C.webview_destroy(w.w)135}136func (w *webview) Run() {137 C.webview_run(w.w)138}139func (w *webview) Terminate() {140 C.webview_terminate(w.w)141}142func (w *webview) Show() {143 C.webview_show(w.w)144}145func (w *webview) Hide() {146 C.webview_hide(w.w)147}148func (w *webview) Window() unsafe.Pointer {149 return C.webview_get_window(w.w)150}151func (w *webview) Navigate(url string) {152 s := C.CString(url)153 defer C.free(unsafe.Pointer(s))154 C.webview_navigate(w.w, s)155}156func (w *webview) SetTitle(title string) {157 s := C.CString(title)158 defer C.free(unsafe.Pointer(s))159 C.webview_set_title(w.w, s)160}161func (w *webview) SetSize(width int, height int, hint Hint) {162 C.webview_set_size(w.w, C.int(width), C.int(height), C.int(hint))163}164func (w *webview) SetIcon(iconBytes []byte) {165 C.webview_set_icon(w.w, unsafe.Pointer(&iconBytes[0]), C.int(len(iconBytes)))166}167func (w *webview) Init(js string) {168 s := C.CString(js)169 defer C.free(unsafe.Pointer(s))170 C.webview_init(w.w, s)171}172func (w *webview) Eval(js string) {173 s := C.CString(js)174 defer C.free(unsafe.Pointer(s))175 C.webview_eval(w.w, s)...
main.go
Source:main.go
1// Copyright 2022 The o11y toolkit Authors2// spdx-license-identifier: apache-2.03//4// Licensed under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at:7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.15//go:build js && wasm16// +build js,wasm17package main18import (19 "fmt"20 "strings"21 "syscall/js"22 "github.com/prometheus/client_golang/prometheus/testutil/promlint"23)24func main() {25 c := make(chan struct{}, 0)26 js.Global().Set("metricslint", js.FuncOf(metriclint))27 js.Global().Set("loadexample", js.FuncOf(loadexample))28 jsDoc := js.Global().Get("document")29 jsDoc.Call("getElementById", "runButton").Set("disabled", false)30 jsDoc.Call("getElementById", "exampleButton").Set("disabled", false)31 jsDoc.Call("getElementById", "loadingWarning").Get("style").Set("display", "none")32 <-c33}34func loadexample(this js.Value, args []js.Value) interface{} {35 jsDoc := js.Global().Get("document")36 res := jsDoc.Call("getElementById", "metricInput")37 res.Set("value", `# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.38# TYPE process_cpu_seconds_total counter39process_cpu_seconds_total 203674.0540# HELP process_max_fds Maximum number of open file descriptors.41# TYPE process_max_fds gauge42process_max_fds 102443# HELP process_open_fds Number of open file descriptors.44# TYPE process_open_fds gauge45process_open_fds 1046`)47 return nil48}49func metriclint(this js.Value, args []js.Value) interface{} {50 jsDoc := js.Global().Get("document")51 res := jsDoc.Call("getElementById", "resultDiv")52 metrics := strings.NewReader(args[0].String() + "\n")53 l := promlint.New(metrics)54 problems, err := l.Lint()55 if err != nil {56 res.Set("innerHTML", fmt.Sprintf(`57 <blockquote class="gdoc-hint danger">58 <div class="gdoc-hint__title flex align-center">59 <svg class="gdoc-icon gdoc_dangerous"><use xlink:href="#gdoc_dangerous"></use></svg>60 <span>Parsing error</span>61 </div>62 <div class="gdoc-hint__text">63 %s64 </div>65 </blockquote>66 `, err.Error()))67 return nil68 }69 if strings.TrimSpace(args[0].String()) == "" {70 res.Set("innerHTML", `71 <blockquote class="gdoc-hint important">72 <div class="gdoc-hint__title flex align-center">73 <svg class="gdoc-icon gdoc_error_outline"><use xlink:href="#gdoc_error_outline"></use></svg>74 <span>No input</span>75 </div>76 <div class="gdoc-hint__text">77 The input provided is empty. Please paste metrics into the text area.78 </div>79 </blockquote>80 `)81 return nil82 }83 if len(problems) == 0 {84 res.Set("innerHTML", `85 <blockquote class="gdoc-hint tip">86 <div class="gdoc-hint__title flex align-center">87 <svg class="gdoc-icon gdoc_check"><use xlink:href="#gdoc_check"></use></svg>88 <span>Success</span>89 </div>90 <div class="gdoc-hint__text">91 Input has been parsed successfully.92 </div>93 </blockquote>94 `)95 return nil96 }97 var pbs string98 for _, p := range problems {99 pbs += fmt.Sprintf("<li><code>%s</code>: %s</li>", p.Metric, p.Text)100 }101 res.Set("innerHTML", fmt.Sprintf(`102 <blockquote class="gdoc-hint important">103 <div class="gdoc-hint__title flex align-center">104 <svg class="gdoc-icon gdoc_fire"><use xlink:href="#gdoc_fire"></use></svg>105 <span>Issues found</span>106 </div>107 <div class="gdoc-hint__text">108 The input can be parsed but there are linting issues:109 <ul>%s</ul>110 </div>111 </blockquote>112 `, pbs))113 return nil114}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!