Best K6 code snippet using html.getTestRuntimeWithDoc
html_test.go
Source:html_test.go
...84 require.NoError(t, err)85 require.IsType(t, Selection{}, rt.Get("doc").Export())86 return rt, mi87}88func getTestRuntimeWithDoc(t testing.TB, html string) *goja.Runtime {89 t.Helper()90 rt, _ := getTestRuntimeAndModuleInstanceWithDoc(t, html)91 return rt92}93func TestParseHTML(t *testing.T) {94 t.Parallel()95 t.Run("Find", func(t *testing.T) {96 t.Parallel()97 rt := getTestRuntimeWithDoc(t, testHTML)98 v, err := rt.RunString(`doc.find("h1")`)99 if assert.NoError(t, err) && assert.IsType(t, Selection{}, v.Export()) {100 sel := v.Export().(Selection).sel101 assert.Equal(t, 1, sel.Length())102 assert.Equal(t, "Lorem ipsum", sel.Text())103 }104 })105 t.Run("Add", func(t *testing.T) {106 t.Parallel()107 rt := getTestRuntimeWithDoc(t, testHTML)108 t.Run("Selector", func(t *testing.T) {109 v, err := rt.RunString(`doc.find("h1").add("footer")`)110 if assert.NoError(t, err) && assert.IsType(t, Selection{}, v.Export()) {111 sel := v.Export().(Selection).sel112 assert.Equal(t, 2, sel.Length())113 assert.Equal(t, "Lorem ipsumThis is the footer.", sel.Text())114 }115 })116 t.Run("Selection", func(t *testing.T) {117 v, err := rt.RunString(`doc.find("h1").add(doc.find("footer"))`)118 if assert.NoError(t, err) && assert.IsType(t, Selection{}, v.Export()) {119 sel := v.Export().(Selection).sel120 assert.Equal(t, 2, sel.Length())121 assert.Equal(t, "Lorem ipsumThis is the footer.", sel.Text())122 }123 })124 })125 t.Run("Text", func(t *testing.T) {126 t.Parallel()127 rt := getTestRuntimeWithDoc(t, testHTML)128 v, err := rt.RunString(`doc.find("h1").text()`)129 if assert.NoError(t, err) {130 assert.Equal(t, "Lorem ipsum", v.Export())131 }132 })133 t.Run("Attr", func(t *testing.T) {134 t.Parallel()135 rt := getTestRuntimeWithDoc(t, testHTML)136 v, err := rt.RunString(`doc.find("h1").attr("id")`)137 if assert.NoError(t, err) {138 assert.Equal(t, "top", v.Export())139 }140 t.Run("Default", func(t *testing.T) {141 v, err := rt.RunString(`doc.find("h1").attr("id", "default")`)142 if assert.NoError(t, err) {143 assert.Equal(t, "top", v.Export())144 }145 })146 t.Run("Unset", func(t *testing.T) {147 v, err := rt.RunString(`doc.find("h1").attr("class")`)148 if assert.NoError(t, err) {149 assert.True(t, goja.IsUndefined(v), "v is not undefined: %v", v)150 }151 t.Run("Default", func(t *testing.T) {152 v, err := rt.RunString(`doc.find("h1").attr("class", "default")`)153 if assert.NoError(t, err) {154 assert.Equal(t, "default", v.Export())155 }156 })157 })158 })159 t.Run("Html", func(t *testing.T) {160 t.Parallel()161 rt := getTestRuntimeWithDoc(t, testHTML)162 v, err := rt.RunString(`doc.find("h1").html()`)163 if assert.NoError(t, err) {164 assert.Equal(t, "Lorem ipsum", v.Export())165 }166 })167 t.Run("Val", func(t *testing.T) {168 t.Parallel()169 rt := getTestRuntimeWithDoc(t, testHTML)170 t.Run("Input", func(t *testing.T) {171 v, err := rt.RunString(`doc.find("#text_input").val()`)172 if assert.NoError(t, err) {173 assert.Equal(t, "input-text-value", v.Export())174 }175 })176 t.Run("Select option[selected]", func(t *testing.T) {177 v, err := rt.RunString(`doc.find("#select_one option[selected]").val()`)178 if assert.NoError(t, err) {179 assert.Equal(t, "yes this option", v.Export())180 }181 })182 t.Run("Select Option Attr", func(t *testing.T) {183 v, err := rt.RunString(`doc.find("#select_one").val()`)184 if assert.NoError(t, err) {185 assert.Equal(t, "yes this option", v.Export())186 }187 })188 t.Run("Select Option Text", func(t *testing.T) {189 v, err := rt.RunString(`doc.find("#select_text").val()`)190 if assert.NoError(t, err) {191 assert.Equal(t, "yes text", v.Export())192 }193 })194 t.Run("Select Option Multiple", func(t *testing.T) {195 v, err := rt.RunString(`doc.find("#select_multi").val()`)196 var opts []string197 if assert.NoError(t, err) && rt.ExportTo(v, &opts) == nil {198 assert.Equal(t, 2, len(opts))199 assert.Equal(t, "option 2", opts[0])200 assert.Equal(t, "option 3", opts[1])201 }202 })203 t.Run("TextArea", func(t *testing.T) {204 v, err := rt.RunString(`doc.find("#textarea").val()`)205 if assert.NoError(t, err) {206 assert.Equal(t, "Lorem ipsum dolor sit amet", v.Export())207 }208 })209 })210 t.Run("Children", func(t *testing.T) {211 t.Parallel()212 rt := getTestRuntimeWithDoc(t, testHTML)213 t.Run("All", func(t *testing.T) {214 v, err := rt.RunString(`doc.find("head").children()`)215 if assert.NoError(t, err) {216 sel := v.Export().(Selection).sel217 assert.Equal(t, 1, sel.Length())218 assert.True(t, sel.Is("title"))219 }220 })221 t.Run("With selector", func(t *testing.T) {222 v, err := rt.RunString(`doc.find("body").children("p")`)223 if assert.NoError(t, err) {224 sel := v.Export().(Selection).sel225 assert.Equal(t, 2, sel.Length())226 assert.Equal(t, "Nullam id nisi", sel.Last().Text()[0:14])227 }228 })229 })230 t.Run("Closest", func(t *testing.T) {231 t.Parallel()232 rt := getTestRuntimeWithDoc(t, testHTML)233 v, err := rt.RunString(`doc.find("textarea").closest("form").attr("id")`)234 if assert.NoError(t, err) {235 assert.Equal(t, "form1", v.Export())236 }237 })238 t.Run("Contents", func(t *testing.T) {239 t.Parallel()240 rt := getTestRuntimeWithDoc(t, testHTML)241 v, err := rt.RunString(`doc.find("head").contents()`)242 if assert.NoError(t, err) {243 sel := v.Export().(Selection).sel244 assert.Equal(t, 3, sel.Length())245 assert.Equal(t, "\n\t", sel.First().Text())246 }247 })248 t.Run("Each", func(t *testing.T) {249 t.Parallel()250 rt := getTestRuntimeWithDoc(t, testHTML)251 t.Run("Func arg", func(t *testing.T) {252 v, err := rt.RunString(`{ var elems = []; doc.find("#select_multi option").each(function(idx, elem) { elems[idx] = elem.innerHTML(); }); elems }`)253 var elems []string254 if assert.NoError(t, err) && rt.ExportTo(v, &elems) == nil {255 assert.Equal(t, 3, len(elems))256 assert.Equal(t, "option 1", elems[0])257 assert.Equal(t, "option 2", elems[1])258 }259 })260 t.Run("Invalid arg", func(t *testing.T) {261 _, err := rt.RunString(`doc.find("#select_multi option").each("");`)262 if assert.Error(t, err) {263 assert.IsType(t, &goja.Exception{}, err)264 assert.Contains(t, err.Error(), "must be a function")265 }266 })267 })268 t.Run("Is", func(t *testing.T) {269 t.Parallel()270 rt := getTestRuntimeWithDoc(t, testHTML)271 t.Run("String selector", func(t *testing.T) {272 v, err := rt.RunString(`doc.find("h1").is("h1")`)273 if assert.NoError(t, err) {274 assert.Equal(t, true, v.Export())275 }276 })277 t.Run("Function selector", func(t *testing.T) {278 v, err := rt.RunString(`doc.find("h1").is(function(idx, val){ return val.text() == "Lorem ipsum" })`)279 if assert.NoError(t, err) {280 assert.Equal(t, true, v.Export())281 }282 })283 t.Run("Selection selector", func(t *testing.T) {284 v, err := rt.RunString(`doc.find("body").children().first().is(doc.find("h1"))`)285 if assert.NoError(t, err) {286 assert.Equal(t, true, v.Export())287 }288 })289 })290 t.Run("Filter", func(t *testing.T) {291 t.Parallel()292 rt := getTestRuntimeWithDoc(t, testHTML)293 t.Run("String", func(t *testing.T) {294 v, err := rt.RunString(`doc.find("body").children().filter("p")`)295 if assert.NoError(t, err) {296 sel := v.Export().(Selection).sel297 assert.Equal(t, 2, sel.Length())298 }299 })300 t.Run("Function", func(t *testing.T) {301 v, err := rt.RunString(`doc.find("body").children().filter(function(idx, val){ return val.is("p") })`)302 if assert.NoError(t, err) {303 sel := v.Export().(Selection).sel304 assert.Equal(t, 2, sel.Length())305 }306 })307 t.Run("Selection", func(t *testing.T) {308 v, err := rt.RunString(`doc.find("body").children().filter(doc.find("p"))`)309 if assert.NoError(t, err) {310 sel := v.Export().(Selection).sel311 assert.Equal(t, 2, sel.Length())312 }313 })314 })315 t.Run("End", func(t *testing.T) {316 t.Parallel()317 rt := getTestRuntimeWithDoc(t, testHTML)318 v, err := rt.RunString(`doc.find("body").children().filter("p").end()`)319 if assert.NoError(t, err) {320 sel := v.Export().(Selection).sel321 assert.Equal(t, 5, sel.Length())322 }323 })324 t.Run("Eq", func(t *testing.T) {325 t.Parallel()326 rt := getTestRuntimeWithDoc(t, testHTML)327 v, err := rt.RunString(`doc.find("body").children().eq(3).attr("id")`)328 if assert.NoError(t, err) {329 assert.Equal(t, "form1", v.Export())330 }331 })332 t.Run("First", func(t *testing.T) {333 t.Parallel()334 rt := getTestRuntimeWithDoc(t, testHTML)335 v, err := rt.RunString(`doc.find("body").children().first().attr("id")`)336 if assert.NoError(t, err) {337 assert.Equal(t, "top", v.Export())338 }339 })340 t.Run("Last", func(t *testing.T) {341 t.Parallel()342 rt := getTestRuntimeWithDoc(t, testHTML)343 v, err := rt.RunString(`doc.find("body").children().last().text()`)344 if assert.NoError(t, err) {345 assert.Equal(t, "This is the footer.", v.Export())346 }347 })348 t.Run("Has", func(t *testing.T) {349 t.Parallel()350 rt := getTestRuntimeWithDoc(t, testHTML)351 t.Run("String selector", func(t *testing.T) {352 v, err := rt.RunString(`doc.find("body").children().has("input").size()`)353 if assert.NoError(t, err) {354 assert.Equal(t, int64(1), v.Export())355 }356 })357 t.Run("Selection selector", func(t *testing.T) {358 v, err := rt.RunString(`doc.find("body").children().has(doc.find("input")).size()`)359 if assert.NoError(t, err) {360 assert.Equal(t, int64(1), v.Export())361 }362 })363 })364 t.Run("Map", func(t *testing.T) {365 t.Parallel()366 rt := getTestRuntimeWithDoc(t, testHTML)367 t.Run("Valid", func(t *testing.T) {368 v, err := rt.RunString(`doc.find("#select_multi option").map(function(idx, val) { return val.text() })`)369 if assert.NoError(t, err) {370 mapped, ok := v.Export().([]string)371 assert.True(t, ok)372 assert.Equal(t, 3, len(mapped))373 assert.Equal(t, []string{"option 1", "option 2", "option 3"}, mapped)374 }375 })376 t.Run("Invalid arg", func(t *testing.T) {377 _, err := rt.RunString(`doc.find("#select_multi option").map("");`)378 if assert.Error(t, err) {379 assert.IsType(t, &goja.Exception{}, err)380 assert.Contains(t, err.Error(), "must be a function")381 }382 })383 t.Run("Map with attr must return string", func(t *testing.T) {384 v, err := rt.RunString(`doc.find("#select_multi").map(function(idx, val) { return val.attr("name") })`)385 if assert.NoError(t, err) {386 mapped, ok := v.Export().([]string)387 assert.True(t, ok)388 assert.Equal(t, 1, len(mapped))389 assert.Equal(t, []string{"select_multi"}, mapped)390 }391 })392 })393 t.Run("Next", func(t *testing.T) {394 t.Parallel()395 rt := getTestRuntimeWithDoc(t, testHTML)396 t.Run("No arg", func(t *testing.T) {397 v, err := rt.RunString(`doc.find("h1").next()`)398 if assert.NoError(t, err) {399 sel := v.Export().(Selection).sel400 assert.Equal(t, 1, sel.Length())401 assert.True(t, sel.Is("p"))402 }403 })404 t.Run("Filter arg", func(t *testing.T) {405 v, err := rt.RunString(`doc.find("p").next("form")`)406 if assert.NoError(t, err) {407 sel := v.Export().(Selection).sel408 assert.Equal(t, 1, sel.Length())409 }410 })411 })412 t.Run("NextAll", func(t *testing.T) {413 t.Parallel()414 rt := getTestRuntimeWithDoc(t, testHTML)415 t.Run("No arg", func(t *testing.T) {416 v, err := rt.RunString(`doc.find("h1").nextAll()`)417 if assert.NoError(t, err) {418 sel := v.Export().(Selection).sel419 assert.Equal(t, 4, sel.Length())420 }421 })422 t.Run("Filter arg", func(t *testing.T) {423 v, err := rt.RunString(`doc.find("h1").nextAll("p")`)424 if assert.NoError(t, err) {425 sel := v.Export().(Selection).sel426 assert.Equal(t, 2, sel.Length())427 }428 })429 })430 t.Run("Prev", func(t *testing.T) {431 t.Parallel()432 rt := getTestRuntimeWithDoc(t, testHTML)433 t.Run("No arg", func(t *testing.T) {434 v, err := rt.RunString(`doc.find("footer").prev()`)435 if assert.NoError(t, err) {436 sel := v.Export().(Selection).sel437 assert.True(t, sel.Is("form"))438 }439 })440 t.Run("Filter arg", func(t *testing.T) {441 v, err := rt.RunString(`doc.find("footer").prev("form")`)442 if assert.NoError(t, err) {443 sel := v.Export().(Selection).sel444 assert.Equal(t, 1, sel.Length())445 }446 })447 })448 t.Run("PrevAll", func(t *testing.T) {449 t.Parallel()450 rt := getTestRuntimeWithDoc(t, testHTML)451 t.Run("No arg", func(t *testing.T) {452 v, err := rt.RunString(`doc.find("form").prevAll()`)453 if assert.NoError(t, err) {454 sel := v.Export().(Selection).sel455 assert.Equal(t, 3, sel.Length())456 }457 })458 t.Run("Filter arg", func(t *testing.T) {459 v, err := rt.RunString(`doc.find("form").prevAll("p")`)460 if assert.NoError(t, err) {461 sel := v.Export().(Selection).sel462 assert.Equal(t, 2, sel.Length())463 }464 })465 })466 t.Run("PrevUntil", func(t *testing.T) {467 t.Parallel()468 rt := getTestRuntimeWithDoc(t, testHTML)469 t.Run("String", func(t *testing.T) {470 v, err := rt.RunString(`doc.find("footer").prevUntil("h1").size()`)471 if assert.NoError(t, err) {472 assert.Equal(t, int64(3), v.Export())473 }474 })475 t.Run("Query", func(t *testing.T) {476 v, err := rt.RunString(`doc.find("footer").prevUntil(doc.find("h1")).size()`)477 if assert.NoError(t, err) {478 assert.Equal(t, int64(3), v.Export())479 }480 })481 t.Run("String filtered", func(t *testing.T) {482 v, err := rt.RunString(`doc.find("form").prevUntil("h1", "p").size()`)483 if assert.NoError(t, err) {484 assert.Equal(t, int64(2), v.Export())485 }486 })487 t.Run("Query filtered", func(t *testing.T) {488 v, err := rt.RunString(`doc.find("form").prevUntil(doc.find("h1"), "p").size()`)489 if assert.NoError(t, err) {490 assert.Equal(t, int64(2), v.Export())491 }492 })493 t.Run("All", func(t *testing.T) {494 v, err := rt.RunString(`doc.find("footer").prevUntil().size()`)495 if assert.NoError(t, err) {496 assert.Equal(t, int64(4), v.Export())497 }498 })499 t.Run("All filtered", func(t *testing.T) {500 v, err := rt.RunString(`doc.find("footer").prevUntil(null, "p").size()`)501 if assert.NoError(t, err) {502 assert.Equal(t, int64(2), v.Export())503 }504 })505 })506 t.Run("NextUntil", func(t *testing.T) {507 t.Parallel()508 rt := getTestRuntimeWithDoc(t, testHTML)509 t.Run("String", func(t *testing.T) {510 v, err := rt.RunString(`doc.find("h1").nextUntil("footer").size()`)511 if assert.NoError(t, err) {512 assert.Equal(t, int64(3), v.Export())513 }514 })515 t.Run("Query", func(t *testing.T) {516 v, err := rt.RunString(`doc.find("h1").nextUntil(doc.find("footer")).size()`)517 if assert.NoError(t, err) {518 assert.Equal(t, int64(3), v.Export())519 }520 })521 t.Run("String filtered", func(t *testing.T) {522 v, err := rt.RunString(`doc.find("h1").nextUntil("footer", "p").size()`)523 if assert.NoError(t, err) {524 assert.Equal(t, int64(2), v.Export())525 }526 })527 t.Run("Query filtered", func(t *testing.T) {528 v, err := rt.RunString(`doc.find("h1").nextUntil(doc.find("footer"), "p").size()`)529 if assert.NoError(t, err) {530 assert.Equal(t, int64(2), v.Export())531 }532 })533 t.Run("All", func(t *testing.T) {534 v, err := rt.RunString(`doc.find("h1").nextUntil().size()`)535 if assert.NoError(t, err) {536 assert.Equal(t, int64(4), v.Export())537 }538 })539 t.Run("All filtered", func(t *testing.T) {540 v, err := rt.RunString(`doc.find("h1").nextUntil(null, "p").size()`)541 if assert.NoError(t, err) {542 assert.Equal(t, int64(2), v.Export())543 }544 })545 })546 t.Run("Parent", func(t *testing.T) {547 t.Parallel()548 rt := getTestRuntimeWithDoc(t, testHTML)549 t.Run("No filter", func(t *testing.T) {550 v, err := rt.RunString(`doc.find("textarea").parent().attr("id")`)551 if assert.NoError(t, err) {552 assert.Equal(t, "form1", v.Export())553 }554 })555 t.Run("Filtered", func(t *testing.T) {556 v, err := rt.RunString(`doc.find("textarea").parent("form").attr("id")`)557 if assert.NoError(t, err) {558 assert.Equal(t, "form1", v.Export())559 }560 })561 })562 t.Run("Parents", func(t *testing.T) {563 t.Parallel()564 rt := getTestRuntimeWithDoc(t, testHTML)565 t.Run("No filter", func(t *testing.T) {566 v, err := rt.RunString(`doc.find("textarea").parents().size()`)567 if assert.NoError(t, err) {568 assert.Equal(t, int64(3), v.Export())569 }570 })571 t.Run("Filtered", func(t *testing.T) {572 v, err := rt.RunString(`doc.find("textarea").parents("body").size()`)573 if assert.NoError(t, err) {574 assert.Equal(t, int64(1), v.Export())575 }576 })577 })578 t.Run("ParentsUntil", func(t *testing.T) {579 t.Parallel()580 rt := getTestRuntimeWithDoc(t, testHTML)581 t.Run("String", func(t *testing.T) {582 v, err := rt.RunString(`doc.find("textarea").parentsUntil("html").size()`)583 if assert.NoError(t, err) {584 assert.Equal(t, int64(2), v.Export())585 }586 })587 t.Run("Query", func(t *testing.T) {588 v, err := rt.RunString(`doc.find("textarea").parentsUntil(doc.find("html")).size()`)589 if assert.NoError(t, err) {590 assert.Equal(t, int64(2), v.Export())591 }592 })593 t.Run("String filtered", func(t *testing.T) {594 v, err := rt.RunString(`doc.find("textarea").parentsUntil("html", "body").size()`)595 if assert.NoError(t, err) {596 assert.Equal(t, int64(1), v.Export())597 }598 })599 t.Run("Query filtered", func(t *testing.T) {600 v, err := rt.RunString(`doc.find("textarea").parentsUntil(doc.find("html"), "body").size()`)601 if assert.NoError(t, err) {602 assert.Equal(t, int64(1), v.Export())603 }604 })605 t.Run("All", func(t *testing.T) {606 v, err := rt.RunString(`doc.find("textarea").parentsUntil().size()`)607 if assert.NoError(t, err) {608 assert.Equal(t, int64(3), v.Export())609 }610 })611 t.Run("All filtered", func(t *testing.T) {612 v, err := rt.RunString(`doc.find("textarea").parentsUntil(null, "body").size()`)613 if assert.NoError(t, err) {614 assert.Equal(t, int64(1), v.Export())615 }616 })617 })618 t.Run("Not", func(t *testing.T) {619 t.Parallel()620 rt := getTestRuntimeWithDoc(t, testHTML)621 t.Run("String selector", func(t *testing.T) {622 v, err := rt.RunString(`doc.find("body").children().not("p").size()`)623 if assert.NoError(t, err) {624 assert.Equal(t, int64(3), v.Export())625 }626 })627 t.Run("Selection selector", func(t *testing.T) {628 v, err := rt.RunString(`doc.find("body").children().not(doc.find("p")).size()`)629 if assert.NoError(t, err) {630 assert.Equal(t, int64(3), v.Export())631 }632 })633 t.Run("Function selector", func(t *testing.T) {634 v, err := rt.RunString(`doc.find("body").children().not(function(idx, val){ return val.is("p") }).size()`)635 if assert.NoError(t, err) {636 assert.Equal(t, int64(3), v.Export())637 }638 })639 })640 t.Run("Siblings", func(t *testing.T) {641 t.Parallel()642 rt := getTestRuntimeWithDoc(t, testHTML)643 t.Run("No filter", func(t *testing.T) {644 v, err := rt.RunString(`doc.find("form").siblings().size()`)645 if assert.NoError(t, err) {646 assert.Equal(t, int64(4), v.Export())647 }648 })649 t.Run("Filtered", func(t *testing.T) {650 v, err := rt.RunString(`doc.find("form").siblings("p").size()`)651 if assert.NoError(t, err) {652 assert.Equal(t, int64(2), v.Export())653 }654 })655 })656 t.Run("Slice", func(t *testing.T) {657 t.Parallel()658 rt := getTestRuntimeWithDoc(t, testHTML)659 t.Run("No filter", func(t *testing.T) {660 v, err := rt.RunString(`doc.find("body").children().slice(1, 2)`)661 if assert.NoError(t, err) {662 sel := v.Export().(Selection).sel663 assert.Equal(t, 1, sel.Length())664 assert.True(t, sel.Is("p"))665 assert.Contains(t, sel.Text(), "Lorem ipsum dolor")666 }667 })668 t.Run("Filtered", func(t *testing.T) {669 v, err := rt.RunString(`doc.find("body").children().slice(3)`)670 if assert.NoError(t, err) {671 sel := v.Export().(Selection).sel672 assert.Equal(t, 2, sel.Length())673 assert.Equal(t, true, sel.Eq(0).Is("form"))674 }675 })676 })677 t.Run("Get", func(t *testing.T) {678 t.Parallel()679 rt := getTestRuntimeWithDoc(t, testHTML)680 t.Run("No args", func(t *testing.T) {681 v, err := rt.RunString(`doc.find("body").children().get()`)682 if assert.NoError(t, err) {683 elems, ok := v.Export().([]goja.Value)684 assert.True(t, ok)685 assert.Equal(t, "h1", elems[0].Export().(Element).NodeName())686 assert.Equal(t, "p", elems[1].Export().(Element).NodeName())687 assert.Equal(t, "footer", elems[4].Export().(Element).NodeName())688 }689 })690 t.Run("+ve index", func(t *testing.T) {691 v, err := rt.RunString(`doc.find("body").children().get(1)`)692 if assert.NoError(t, err) {693 elem, _ := v.Export().(Element)694 assert.Contains(t, elem.InnerHTML(), "Lorem ipsum dolor sit amet")695 }696 })697 t.Run("-ve index", func(t *testing.T) {698 v, err := rt.RunString(`doc.find("body").children().get(-1)`)699 if assert.NoError(t, err) {700 elem, _ := v.Export().(Element)701 assert.Equal(t, "This is the footer.", elem.InnerHTML().String())702 }703 })704 })705 t.Run("ToArray", func(t *testing.T) {706 t.Parallel()707 rt := getTestRuntimeWithDoc(t, testHTML)708 v, err := rt.RunString(`doc.find("p").toArray()`)709 if assert.NoError(t, err) {710 arr, ok := v.Export().([]Selection)711 assert.True(t, ok)712 assert.Equal(t, 2, len(arr))713 assert.Equal(t, 1, arr[0].sel.Length())714 assert.Equal(t, 1, arr[1].sel.Length())715 assert.Contains(t, arr[0].sel.Text(), "Lorem ipsum dolor sit amet")716 assert.Contains(t, arr[1].sel.Text(), "Nullam id nisi eget")717 }718 })719 t.Run("Index", func(t *testing.T) {720 t.Parallel()721 rt := getTestRuntimeWithDoc(t, testHTML)722 t.Run("No args", func(t *testing.T) {723 v, err := rt.RunString(`doc.find("p").index()`)724 if assert.NoError(t, err) {725 assert.Equal(t, int64(1), v.Export())726 }727 })728 t.Run("String selector", func(t *testing.T) {729 v, err := rt.RunString(`doc.find("form").index("body > *")`)730 if assert.NoError(t, err) {731 assert.Equal(t, int64(3), v.Export())732 }733 })734 t.Run("Selection selector", func(t *testing.T) {735 v, err := rt.RunString(`doc.find("body").children().index(doc.find("footer"))`)736 if assert.NoError(t, err) {737 assert.Equal(t, int64(4), v.Export())738 }739 })740 })741 t.Run("Data <h1>", func(t *testing.T) {742 t.Parallel()743 rt := getTestRuntimeWithDoc(t, testHTML)744 t.Run("string attr", func(t *testing.T) {745 v, err := rt.RunString(`doc.find("h1").data("test")`)746 if assert.NoError(t, err) {747 assert.Equal(t, "dataval", v.Export())748 }749 })750 t.Run("numeric attr 1", func(t *testing.T) {751 v, err := rt.RunString(`doc.find("h1").data("num-a")`)752 if assert.NoError(t, err) {753 assert.Equal(t, int64(123), v.Export())754 }755 })756 t.Run("numeric attr 2", func(t *testing.T) {757 v, err := rt.RunString(`doc.find("h1").data("num-b")`)758 if assert.NoError(t, err) {759 assert.Equal(t, float64(1.5), v.Export())760 }761 })762 t.Run("not numeric attr 1", func(t *testing.T) {763 v, err := rt.RunString(`doc.find("h1").data("not-num-a")`)764 if assert.NoError(t, err) {765 assert.Equal(t, "1.50", v.Export())766 }767 })768 t.Run("not numeric attr 2", func(t *testing.T) {769 v, err := rt.RunString(`doc.find("h1").data("not-num-b")`)770 if assert.NoError(t, err) {771 assert.Equal(t, "1.1e02", v.Export())772 }773 })774 t.Run("dataset", func(t *testing.T) {775 v, err := rt.RunString(`doc.find("h1").data()`)776 if assert.NoError(t, err) {777 data, ok := v.Export().(map[string]interface{})778 assert.True(t, ok)779 assert.Equal(t, "dataval", data["test"])780 assert.Equal(t, float64(123), data["numA"])781 }782 })783 })784 t.Run("Data <p>", func(t *testing.T) {785 t.Parallel()786 rt := getTestRuntimeWithDoc(t, testHTML)787 t.Run("boolean attr", func(t *testing.T) {788 v, err := rt.RunString(`doc.find("p").data("test-b")`)789 if assert.NoError(t, err) {790 assert.Equal(t, true, v.Export())791 }792 })793 t.Run("snakeCase attr name", func(t *testing.T) {794 v, err := rt.RunString(`doc.find("p").data("testB")`)795 if assert.NoError(t, err) {796 assert.Equal(t, true, v.Export())797 }798 })799 t.Run("empty string", func(t *testing.T) {800 v, err := rt.RunString(`doc.find("p").data("test-empty")`)...
element_test.go
Source:element_test.go
...48`49func TestElement(t *testing.T) {50 t.Run("NodeName", func(t *testing.T) {51 t.Parallel()52 rt := getTestRuntimeWithDoc(t, testHTMLElem)53 v, err := rt.RunString(`doc.find("#top").get(0).nodeName()`)54 if assert.NoError(t, err) {55 assert.Equal(t, "h1", v.Export())56 }57 })58 t.Run("NodeType", func(t *testing.T) {59 t.Parallel()60 rt := getTestRuntimeWithDoc(t, testHTMLElem)61 v, err := rt.RunString(`doc.find("#top").get(0).nodeType()`)62 if assert.NoError(t, err) {63 assert.Equal(t, "1", v.String())64 }65 })66 t.Run("NodeValue", func(t *testing.T) {67 t.Parallel()68 rt := getTestRuntimeWithDoc(t, testHTMLElem)69 v, err := rt.RunString(`doc.find("#top").get(0).firstChild().nodeValue()`)70 if assert.NoError(t, err) {71 assert.Equal(t, "Lorem ipsum", v.String())72 }73 })74 t.Run("InnerHtml", func(t *testing.T) {75 t.Parallel()76 rt := getTestRuntimeWithDoc(t, testHTMLElem)77 v, err := rt.RunString(`doc.find("span").get(0).innerHTML()`)78 if assert.NoError(t, err) {79 assert.Equal(t, "<b>test content</b>", v.String())80 }81 })82 t.Run("TextContent", func(t *testing.T) {83 t.Parallel()84 rt := getTestRuntimeWithDoc(t, testHTMLElem)85 v, err := rt.RunString(`doc.find("b").get(0).textContent()`)86 if assert.NoError(t, err) {87 assert.Equal(t, "test content", v.String())88 }89 })90 t.Run("OwnerDocument", func(t *testing.T) {91 t.Parallel()92 rt := getTestRuntimeWithDoc(t, testHTMLElem)93 v, err := rt.RunString(`doc.find("body").get(0).ownerDocument().nodeName()`)94 if assert.NoError(t, err) {95 assert.Equal(t, "#document", v.String())96 }97 })98 t.Run("Attributes", func(t *testing.T) {99 t.Parallel()100 rt := getTestRuntimeWithDoc(t, testHTMLElem)101 v, err := rt.RunString(`doc.find("div").get(0).attributes()`)102 if assert.NoError(t, err) {103 attrs, ok := v.Export().(map[string]Attribute)104 assert.True(t, ok)105 assert.Equal(t, "div_elem", attrs["id"].Value)106 }107 })108 t.Run("FirstChild", func(t *testing.T) {109 t.Parallel()110 rt := getTestRuntimeWithDoc(t, testHTMLElem)111 v, err := rt.RunString(`doc.find("div").get(0).firstChild().nodeValue()`)112 if assert.NoError(t, err) {113 assert.Contains(t, v.Export(), "innerfirst")114 }115 })116 t.Run("LastChild", func(t *testing.T) {117 t.Parallel()118 rt := getTestRuntimeWithDoc(t, testHTMLElem)119 v, err := rt.RunString(`doc.find("div").get(0).lastChild().nodeValue()`)120 if assert.NoError(t, err) {121 assert.Contains(t, v.Export(), "innerlast")122 }123 })124 t.Run("ChildElementCount", func(t *testing.T) {125 t.Parallel()126 rt := getTestRuntimeWithDoc(t, testHTMLElem)127 v, err := rt.RunString(`doc.find("body").get(0).childElementCount()`)128 if assert.NoError(t, err) {129 assert.Equal(t, int64(6), v.Export())130 }131 })132 t.Run("FirstElementChild", func(t *testing.T) {133 t.Parallel()134 rt := getTestRuntimeWithDoc(t, testHTMLElem)135 v, err := rt.RunString(`doc.find("div").get(0).firstElementChild().textContent()`)136 if assert.NoError(t, err) {137 assert.Contains(t, v.Export(), "Nullam id nisi ")138 }139 })140 t.Run("LastElementChild", func(t *testing.T) {141 t.Parallel()142 rt := getTestRuntimeWithDoc(t, testHTMLElem)143 v, err := rt.RunString(`doc.find("div").get(0).lastElementChild().textContent()`)144 if assert.NoError(t, err) {145 assert.Contains(t, v.Export(), "Maecenas augue ligula")146 }147 })148 t.Run("PreviousSibling", func(t *testing.T) {149 t.Parallel()150 rt := getTestRuntimeWithDoc(t, testHTMLElem)151 v, err := rt.RunString(`doc.find("div").get(0).previousSibling().textContent()`)152 if assert.NoError(t, err) {153 assert.Contains(t, v.Export(), "pretext")154 }155 })156 t.Run("NextSibling", func(t *testing.T) {157 t.Parallel()158 rt := getTestRuntimeWithDoc(t, testHTMLElem)159 v, err := rt.RunString(`doc.find("div").get(0).nextSibling().textContent()`)160 if assert.NoError(t, err) {161 assert.Contains(t, v.Export(), "aftertext")162 }163 })164 t.Run("PreviousElementSibling", func(t *testing.T) {165 t.Parallel()166 rt := getTestRuntimeWithDoc(t, testHTMLElem)167 v, err := rt.RunString(`doc.find("div").get(0).previousElementSibling().textContent()`)168 if assert.NoError(t, err) {169 assert.Contains(t, v.Export(), "consectetur adipiscing elit")170 }171 })172 t.Run("NextElementSibling", func(t *testing.T) {173 t.Parallel()174 rt := getTestRuntimeWithDoc(t, testHTMLElem)175 v, err := rt.RunString(`doc.find("div").get(0).nextElementSibling().textContent()`)176 if assert.NoError(t, err) {177 assert.Contains(t, v.Export(), "This is the footer.")178 }179 })180 t.Run("ParentElement", func(t *testing.T) {181 t.Parallel()182 rt := getTestRuntimeWithDoc(t, testHTMLElem)183 v, err := rt.RunString(`doc.find("div").get(0).parentElement().nodeName()`)184 if assert.NoError(t, err) {185 assert.Equal(t, "body", v.String())186 }187 })188 t.Run("ParentNode", func(t *testing.T) {189 t.Parallel()190 rt := getTestRuntimeWithDoc(t, testHTMLElem)191 nodeVal, err1 := rt.RunString(`doc.find("html").get(0).parentNode().nodeName()`)192 nilVal, err2 := rt.RunString(`doc.find("html").get(0).parentElement()`)193 if assert.NoError(t, err1) && assert.NoError(t, err2) {194 assert.Equal(t, "#document", nodeVal.String())195 assert.Equal(t, nil, nilVal.Export())196 }197 })198 t.Run("ChildNodes", func(t *testing.T) {199 t.Parallel()200 rt := getTestRuntimeWithDoc(t, testHTMLElem)201 v, err := rt.RunString(`doc.find("div").get(0).childNodes()`)202 if assert.NoError(t, err) {203 nodes, ok := v.Export().([]goja.Value)204 assert.True(t, ok)205 assert.Equal(t, 9, len(nodes))206 assert.Contains(t, nodes[0].Export().(Element).TextContent(), "innerfirst")207 assert.Contains(t, nodes[8].Export().(Element).TextContent(), "innerlast")208 }209 })210 t.Run("Children", func(t *testing.T) {211 t.Parallel()212 rt := getTestRuntimeWithDoc(t, testHTMLElem)213 v, err := rt.RunString(`doc.find("div").get(0).children()`)214 if assert.NoError(t, err) {215 nodes, ok := v.Export().([]goja.Value)216 assert.True(t, ok)217 assert.Equal(t, 4, len(nodes))218 assert.Contains(t, nodes[0].Export().(Element).TextContent(), "Nullam id nisi eget ex")219 assert.Contains(t, nodes[3].Export().(Element).TextContent(), "Maecenas augue ligula")220 }221 })222 t.Run("ClassList", func(t *testing.T) {223 t.Parallel()224 rt := getTestRuntimeWithDoc(t, testHTMLElem)225 v, err := rt.RunString(`doc.find("div").get(0).classList()`)226 if assert.NoError(t, err) {227 clsNames, ok := v.Export().([]string)228 assert.True(t, ok)229 assert.Equal(t, 2, len(clsNames))230 assert.Equal(t, []string{"class1", "class2"}, clsNames)231 }232 })233 t.Run("ClassName", func(t *testing.T) {234 t.Parallel()235 rt := getTestRuntimeWithDoc(t, testHTMLElem)236 v, err := rt.RunString(`doc.find("div").get(0).className()`)237 if assert.NoError(t, err) {238 assert.Equal(t, "class1 class2", v.String())239 }240 })241 t.Run("Lang", func(t *testing.T) {242 t.Parallel()243 rt := getTestRuntimeWithDoc(t, testHTMLElem)244 v, err := rt.RunString(`doc.find("div").get(0).lang()`)245 if assert.NoError(t, err) {246 assert.Equal(t, "en", v.String())247 }248 })249 t.Run("ToString", func(t *testing.T) {250 t.Parallel()251 rt := getTestRuntimeWithDoc(t, testHTMLElem)252 v1, err1 := rt.RunString(`doc.find("div").get(0).toString()`)253 v2, err2 := rt.RunString(`doc.find("div").get(0).previousSibling().toString()`)254 if assert.NoError(t, err1) && assert.NoError(t, err2) {255 assert.Equal(t, "[object html.Node]", v1.String())256 assert.Equal(t, "[object #text]", v2.String())257 }258 })259 t.Run("HasAttribute", func(t *testing.T) {260 t.Parallel()261 rt := getTestRuntimeWithDoc(t, testHTMLElem)262 v1, err1 := rt.RunString(`doc.find("div").get(0).hasAttribute("id")`)263 v2, err2 := rt.RunString(`doc.find("div").get(0).hasAttribute("noattr")`)264 if assert.NoError(t, err1) && assert.NoError(t, err2) {265 assert.Equal(t, true, v1.Export())266 assert.Equal(t, false, v2.Export())267 }268 })269 t.Run("GetAttribute", func(t *testing.T) {270 t.Parallel()271 rt := getTestRuntimeWithDoc(t, testHTMLElem)272 v, err := rt.RunString(`doc.find("div").get(0).getAttribute("id")`)273 if assert.NoError(t, err) {274 assert.Equal(t, "div_elem", v.Export())275 }276 })277 t.Run("GetAttributeNode", func(t *testing.T) {278 t.Parallel()279 rt := getTestRuntimeWithDoc(t, testHTMLElem)280 v, err := rt.RunString(`doc.find("div").get(0).getAttributeNode("id")`)281 if assert.NoError(t, err) && assert.IsType(t, Attribute{}, v.Export()) {282 assert.Equal(t, "div_elem", v.Export().(Attribute).Value)283 }284 })285 t.Run("HasAttributes", func(t *testing.T) {286 t.Parallel()287 rt := getTestRuntimeWithDoc(t, testHTMLElem)288 v1, err1 := rt.RunString(`doc.find("h1").get(0).hasAttributes()`)289 v2, err2 := rt.RunString(`doc.find("footer").get(0).hasAttributes()`)290 if assert.NoError(t, err1) && assert.NoError(t, err2) {291 assert.Equal(t, true, v1.Export())292 assert.Equal(t, false, v2.Export())293 }294 })295 t.Run("HasChildNodes", func(t *testing.T) {296 t.Parallel()297 rt := getTestRuntimeWithDoc(t, testHTMLElem)298 v1, err1 := rt.RunString(`doc.find("p").get(0).hasChildNodes()`)299 v2, err2 := rt.RunString(`doc.find("empty").get(0).hasChildNodes()`)300 if assert.NoError(t, err1) && assert.NoError(t, err2) {301 assert.Equal(t, true, v1.Export())302 assert.Equal(t, false, v2.Export())303 }304 })305 t.Run("IsSameNode", func(t *testing.T) {306 t.Parallel()307 rt := getTestRuntimeWithDoc(t, testHTMLElem)308 v1, err1 := rt.RunString(`doc.find("p").get(0).isSameNode(doc.find("p").get(1))`)309 v2, err2 := rt.RunString(`doc.find("p").get(0).isSameNode(doc.find("p").get(0))`)310 if assert.NoError(t, err1) && assert.NoError(t, err2) {311 assert.Equal(t, false, v1.Export())312 assert.Equal(t, true, v2.Export())313 }314 })315 t.Run("IsEqualNode", func(t *testing.T) {316 t.Parallel()317 rt := getTestRuntimeWithDoc(t, testHTMLElem)318 v1, err1 := rt.RunString(`doc.find("p").get(0).isEqualNode(doc.find("p").get(1))`)319 v2, err2 := rt.RunString(`doc.find("p").get(0).isEqualNode(doc.find("p").get(0))`)320 if assert.NoError(t, err1) && assert.NoError(t, err2) {321 assert.Equal(t, true, v1.Export())322 assert.Equal(t, true, v2.Export())323 }324 })325 t.Run("GetElementsByClassName", func(t *testing.T) {326 t.Parallel()327 rt := getTestRuntimeWithDoc(t, testHTMLElem)328 v, err := rt.RunString(`doc.find("body").get(0).getElementsByClassName("class2")`)329 if assert.NoError(t, err) {330 elems, ok := v.Export().([]goja.Value)331 assert.True(t, ok)332 assert.Equal(t, "div_elem", elems[0].Export().(Element).Id())333 assert.Equal(t, "h2_elem", elems[1].Export().(Element).Id())334 }335 })336 t.Run("GetElementsByTagName", func(t *testing.T) {337 t.Parallel()338 rt := getTestRuntimeWithDoc(t, testHTMLElem)339 v, err := rt.RunString(`doc.find("body").get(0).getElementsByTagName("span")`)340 if assert.NoError(t, err) {341 elems, ok := v.Export().([]goja.Value)342 assert.True(t, ok)343 assert.Equal(t, 2, len(elems))344 }345 })346 t.Run("QuerySelector", func(t *testing.T) {347 t.Parallel()348 rt := getTestRuntimeWithDoc(t, testHTMLElem)349 v, err := rt.RunString(`doc.find("body").get(0).querySelector("#div_elem").id()`)350 if assert.NoError(t, err) {351 assert.Equal(t, "div_elem", v.Export())352 }353 })354 t.Run("QuerySelectorAll", func(t *testing.T) {355 t.Parallel()356 rt := getTestRuntimeWithDoc(t, testHTMLElem)357 v, err := rt.RunString(`doc.find("body").get(0).querySelectorAll("span")`)358 if assert.NoError(t, err) {359 elems, ok := v.Export().([]goja.Value)360 assert.True(t, ok)361 assert.Len(t, elems, 2)362 assert.Equal(t, "span1", elems[0].Export().(Element).Id())363 assert.Equal(t, "span2", elems[1].Export().(Element).Id())364 }365 })366 t.Run("Contains", func(t *testing.T) {367 t.Parallel()368 rt := getTestRuntimeWithDoc(t, testHTMLElem)369 v1, err1 := rt.RunString(`doc.find("html").get(0).contains(doc.find("body").get(0))`)370 v2, err2 := rt.RunString(`doc.find("body").get(0).contains(doc.find("body").get(0))`)371 if assert.NoError(t, err1) && assert.NoError(t, err2) {372 assert.Equal(t, true, v1.Export())373 assert.Equal(t, false, v2.Export())374 }375 })376 t.Run("Matches", func(t *testing.T) {377 t.Parallel()378 rt := getTestRuntimeWithDoc(t, testHTMLElem)379 v, err := rt.RunString(`doc.find("div").get(0).matches("#div_elem")`)380 if assert.NoError(t, err) {381 assert.Equal(t, true, v.Export())382 }383 })384 t.Run("NamespaceURI", func(t *testing.T) {385 t.Parallel()386 rt := getTestRuntimeWithDoc(t, testHTMLElem)387 v, err := rt.RunString(`doc.find("#svg_elem").get(0).namespaceURI()`)388 if assert.NoError(t, err) {389 assert.Equal(t, "http://www.w3.org/2000/svg", v.Export())390 }391 })392 t.Run("IsDefaultNamespace", func(t *testing.T) {393 t.Parallel()394 rt := getTestRuntimeWithDoc(t, testHTMLElem)395 v1, err1 := rt.RunString(`doc.find("#svg_elem").get(0).isDefaultNamespace()`)396 v2, err2 := rt.RunString(`doc.find("#div_elem").get(0).isDefaultNamespace()`)397 if assert.NoError(t, err1) && assert.NoError(t, err2) {398 assert.Equal(t, false, v1.ToBoolean())399 assert.Equal(t, true, v2.ToBoolean())400 }401 })402}...
getTestRuntimeWithDoc
Using AI Code Generation
1import (2func main() {3 vm := otto.New()4 vm.Run(`5 var html = {6 getTestRuntimeWithDoc: function (doc) {7 return doc;8 }9 };10 value, _ := vm.Run(`11 html.getTestRuntimeWithDoc('test');12 fmt.Println(value.String())13}
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!!