How to use find method of main Package

Best Rod code snippet using main.find

manipulation_test.go

Source:manipulation_test.go Github

copy

Full Screen

1package goquery2import (3 "log"4 "testing"5)6const (7 wrapHtml = "<div id=\"ins\">test string<div><p><em><b></b></em></p></div></div>"8)9func TestAfter(t *testing.T) {10 doc := Doc2Clone()11 doc.Find("#main").After("#nf6")12 assertLength(t, doc.Find("#main #nf6").Nodes, 0)13 assertLength(t, doc.Find("#foot #nf6").Nodes, 0)14 assertLength(t, doc.Find("#main + #nf6").Nodes, 1)15 printSel(t, doc.Selection)16}17func TestAfterMany(t *testing.T) {18 doc := Doc2Clone()19 doc.Find(".one").After("#nf6")20 assertLength(t, doc.Find("#foot #nf6").Nodes, 1)21 assertLength(t, doc.Find("#main #nf6").Nodes, 1)22 assertLength(t, doc.Find(".one + #nf6").Nodes, 2)23 printSel(t, doc.Selection)24}25func TestAfterWithRemoved(t *testing.T) {26 doc := Doc2Clone()27 s := doc.Find("#main").Remove()28 s.After("#nf6")29 assertLength(t, s.Find("#nf6").Nodes, 0)30 assertLength(t, doc.Find("#nf6").Nodes, 0)31 printSel(t, doc.Selection)32}33func TestAfterSelection(t *testing.T) {34 doc := Doc2Clone()35 doc.Find("#main").AfterSelection(doc.Find("#nf1, #nf2"))36 assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0)37 assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0)38 assertLength(t, doc.Find("#main + #nf1, #nf1 + #nf2").Nodes, 2)39 printSel(t, doc.Selection)40}41func TestAfterHtml(t *testing.T) {42 doc := Doc2Clone()43 doc.Find("#main").AfterHtml("<strong>new node</strong>")44 assertLength(t, doc.Find("#main + strong").Nodes, 1)45 printSel(t, doc.Selection)46}47func TestAfterHtmlContext(t *testing.T) {48 doc := loadString(t, `49 <html>50 <body>51 <table>52 <tr>53 <td>Before1</td>54 </tr>55 <tr>56 <td>Before2</td>57 </tr>58 </table>59 </body>60 </html>`)61 doc.Find("table tr td").AfterHtml("<td class='c1'>Test</td><td class='c2'>Again</td>")62 assertLength(t, doc.Find("table tr td").Nodes, 6)63 assertClass(t, doc.Find("table tr td").Last(), "c2")64 printSel(t, doc.Selection)65}66func TestAppend(t *testing.T) {67 doc := Doc2Clone()68 doc.Find("#main").Append("#nf6")69 assertLength(t, doc.Find("#foot #nf6").Nodes, 0)70 assertLength(t, doc.Find("#main #nf6").Nodes, 1)71 printSel(t, doc.Selection)72}73func TestAppendBody(t *testing.T) {74 doc := Doc2Clone()75 doc.Find("body").Append("#nf6")76 assertLength(t, doc.Find("#foot #nf6").Nodes, 0)77 assertLength(t, doc.Find("#main #nf6").Nodes, 0)78 assertLength(t, doc.Find("body > #nf6").Nodes, 1)79 printSel(t, doc.Selection)80}81func TestAppendSelection(t *testing.T) {82 doc := Doc2Clone()83 doc.Find("#main").AppendSelection(doc.Find("#nf1, #nf2"))84 assertLength(t, doc.Find("#foot #nf1").Nodes, 0)85 assertLength(t, doc.Find("#foot #nf2").Nodes, 0)86 assertLength(t, doc.Find("#main #nf1").Nodes, 1)87 assertLength(t, doc.Find("#main #nf2").Nodes, 1)88 printSel(t, doc.Selection)89}90func TestAppendSelectionExisting(t *testing.T) {91 doc := Doc2Clone()92 doc.Find("#main").AppendSelection(doc.Find("#n1, #n2"))93 assertClass(t, doc.Find("#main :nth-child(1)"), "three")94 assertClass(t, doc.Find("#main :nth-child(5)"), "one")95 assertClass(t, doc.Find("#main :nth-child(6)"), "two")96 printSel(t, doc.Selection)97}98func TestAppendClone(t *testing.T) {99 doc := Doc2Clone()100 doc.Find("#n1").AppendSelection(doc.Find("#nf1").Clone())101 assertLength(t, doc.Find("#foot #nf1").Nodes, 1)102 assertLength(t, doc.Find("#main #nf1").Nodes, 1)103 printSel(t, doc.Selection)104}105func TestAppendHtml(t *testing.T) {106 doc := Doc2Clone()107 doc.Find("div").AppendHtml("<strong>new node</strong>")108 assertLength(t, doc.Find("strong").Nodes, 14)109 printSel(t, doc.Selection)110}111func TestAppendHtmlContext(t *testing.T) {112 doc := loadString(t, `113 <html>114 <body>115 <table>116 <tr>117 <td>Before1</td>118 </tr>119 <tr>120 <td>Before2</td>121 </tr>122 </table>123 </body>124 </html>`)125 doc.Find("table tr").AppendHtml("<td class='c1'>new1</td><td class='c2'>new2</td>")126 assertLength(t, doc.Find("table td").Nodes, 6)127 assertClass(t, doc.Find("table td").Last(), "c2")128 printSel(t, doc.Selection)129}130func TestBefore(t *testing.T) {131 doc := Doc2Clone()132 doc.Find("#main").Before("#nf6")133 assertLength(t, doc.Find("#main #nf6").Nodes, 0)134 assertLength(t, doc.Find("#foot #nf6").Nodes, 0)135 assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)136 printSel(t, doc.Selection)137}138func TestBeforeWithRemoved(t *testing.T) {139 doc := Doc2Clone()140 s := doc.Find("#main").Remove()141 s.Before("#nf6")142 assertLength(t, s.Find("#nf6").Nodes, 0)143 assertLength(t, doc.Find("#nf6").Nodes, 0)144 printSel(t, doc.Selection)145}146func TestBeforeSelection(t *testing.T) {147 doc := Doc2Clone()148 doc.Find("#main").BeforeSelection(doc.Find("#nf1, #nf2"))149 assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0)150 assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0)151 assertLength(t, doc.Find("body > #nf1:first-child, #nf1 + #nf2").Nodes, 2)152 printSel(t, doc.Selection)153}154func TestBeforeHtml(t *testing.T) {155 doc := Doc2Clone()156 doc.Find("#main").BeforeHtml("<strong>new node</strong>")157 assertLength(t, doc.Find("body > strong:first-child").Nodes, 1)158 printSel(t, doc.Selection)159}160func TestBeforeHtmlContext(t *testing.T) {161 doc := loadString(t, `162 <html>163 <body>164 <table>165 <tr>166 <td>Before1</td>167 </tr>168 <tr>169 <td>Before2</td>170 </tr>171 </table>172 </body>173 </html>`)174 doc.Find("table tr td:first-child").BeforeHtml("<td class='c1'>new1</td><td class='c2'>new2</td>")175 assertLength(t, doc.Find("table td").Nodes, 6)176 assertClass(t, doc.Find("table td").First(), "c1")177 printSel(t, doc.Selection)178}179func TestEmpty(t *testing.T) {180 doc := Doc2Clone()181 s := doc.Find("#main").Empty()182 assertLength(t, doc.Find("#main").Children().Nodes, 0)183 assertLength(t, s.Filter("div").Nodes, 6)184 printSel(t, doc.Selection)185}186func TestPrepend(t *testing.T) {187 doc := Doc2Clone()188 doc.Find("#main").Prepend("#nf6")189 assertLength(t, doc.Find("#foot #nf6").Nodes, 0)190 assertLength(t, doc.Find("#main #nf6:first-child").Nodes, 1)191 printSel(t, doc.Selection)192}193func TestPrependBody(t *testing.T) {194 doc := Doc2Clone()195 doc.Find("body").Prepend("#nf6")196 assertLength(t, doc.Find("#foot #nf6").Nodes, 0)197 assertLength(t, doc.Find("#main #nf6").Nodes, 0)198 assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)199 printSel(t, doc.Selection)200}201func TestPrependSelection(t *testing.T) {202 doc := Doc2Clone()203 doc.Find("#main").PrependSelection(doc.Find("#nf1, #nf2"))204 assertLength(t, doc.Find("#foot #nf1").Nodes, 0)205 assertLength(t, doc.Find("#foot #nf2").Nodes, 0)206 assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)207 assertLength(t, doc.Find("#main #nf2:nth-child(2)").Nodes, 1)208 printSel(t, doc.Selection)209}210func TestPrependSelectionExisting(t *testing.T) {211 doc := Doc2Clone()212 doc.Find("#main").PrependSelection(doc.Find("#n5, #n6"))213 assertClass(t, doc.Find("#main :nth-child(1)"), "five")214 assertClass(t, doc.Find("#main :nth-child(2)"), "six")215 assertClass(t, doc.Find("#main :nth-child(5)"), "three")216 assertClass(t, doc.Find("#main :nth-child(6)"), "four")217 printSel(t, doc.Selection)218}219func TestPrependClone(t *testing.T) {220 doc := Doc2Clone()221 doc.Find("#n1").PrependSelection(doc.Find("#nf1").Clone())222 assertLength(t, doc.Find("#foot #nf1:first-child").Nodes, 1)223 assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)224 printSel(t, doc.Selection)225}226func TestPrependHtml(t *testing.T) {227 doc := Doc2Clone()228 doc.Find("div").PrependHtml("<strong>new node</strong>")229 assertLength(t, doc.Find("strong:first-child").Nodes, 14)230 printSel(t, doc.Selection)231}232func TestPrependHtmlContext(t *testing.T) {233 doc := loadString(t, `234 <html>235 <body>236 <table>237 <tr>238 <td>Before1</td>239 </tr>240 <tr>241 <td>Before2</td>242 </tr>243 </table>244 </body>245 </html>`)246 doc.Find("table tr").PrependHtml("<td class='c1'>new node</td><td class='c2'>other new node</td>")247 assertLength(t, doc.Find("table td").Nodes, 6)248 assertClass(t, doc.Find("table tr td").First(), "c1")249 printSel(t, doc.Selection)250}251func TestRemove(t *testing.T) {252 doc := Doc2Clone()253 doc.Find("#nf1").Remove()254 assertLength(t, doc.Find("#foot #nf1").Nodes, 0)255 printSel(t, doc.Selection)256}257func TestRemoveAll(t *testing.T) {258 doc := Doc2Clone()259 doc.Find("*").Remove()260 assertLength(t, doc.Find("*").Nodes, 0)261 printSel(t, doc.Selection)262}263func TestRemoveRoot(t *testing.T) {264 doc := Doc2Clone()265 doc.Find("html").Remove()266 assertLength(t, doc.Find("html").Nodes, 0)267 printSel(t, doc.Selection)268}269func TestRemoveFiltered(t *testing.T) {270 doc := Doc2Clone()271 nf6 := doc.Find("#nf6")272 s := doc.Find("div").RemoveFiltered("#nf6")273 assertLength(t, doc.Find("#nf6").Nodes, 0)274 assertLength(t, s.Nodes, 1)275 if nf6.Nodes[0] != s.Nodes[0] {276 t.Error("Removed node does not match original")277 }278 printSel(t, doc.Selection)279}280func TestReplaceWith(t *testing.T) {281 doc := Doc2Clone()282 doc.Find("#nf6").ReplaceWith("#main")283 assertLength(t, doc.Find("#foot #main:last-child").Nodes, 1)284 printSel(t, doc.Selection)285 doc.Find("#foot").ReplaceWith("#main")286 assertLength(t, doc.Find("#foot").Nodes, 0)287 assertLength(t, doc.Find("#main").Nodes, 1)288 printSel(t, doc.Selection)289}290func TestReplaceWithHtml(t *testing.T) {291 doc := Doc2Clone()292 doc.Find("#main, #foot").ReplaceWithHtml("<div id=\"replace\"></div>")293 assertLength(t, doc.Find("#replace").Nodes, 2)294 printSel(t, doc.Selection)295}296func TestReplaceWithHtmlContext(t *testing.T) {297 doc := loadString(t, `298 <html>299 <body>300 <table>301 <tr>302 <th>Before1</th>303 </tr>304 <tr>305 <th>Before2</th>306 </tr>307 </table>308 </body>309 </html>`)310 doc.Find("table th").ReplaceWithHtml("<td class='c1'>Test</td><td class='c2'>Replace</td>")311 assertLength(t, doc.Find("table th").Nodes, 0)312 assertLength(t, doc.Find("table tr td").Nodes, 4)313 assertClass(t, doc.Find("table tr td").First(), "c1")314 printSel(t, doc.Selection)315}316func TestSetHtml(t *testing.T) {317 doc := Doc2Clone()318 q := doc.Find("#main, #foot")319 q.SetHtml(`<div id="replace">test</div>`)320 assertLength(t, doc.Find("#replace").Nodes, 2)321 assertLength(t, doc.Find("#main, #foot").Nodes, 2)322 if q.Text() != "testtest" {323 t.Errorf("Expected text to be %v, found %v", "testtest", q.Text())324 }325 printSel(t, doc.Selection)326}327func TestSetHtmlNoMatch(t *testing.T) {328 doc := Doc2Clone()329 q := doc.Find("#notthere")330 q.SetHtml(`<div id="replace">test</div>`)331 assertLength(t, doc.Find("#replace").Nodes, 0)332 printSel(t, doc.Selection)333}334func TestSetHtmlEmpty(t *testing.T) {335 doc := Doc2Clone()336 q := doc.Find("#main")337 q.SetHtml(``)338 assertLength(t, doc.Find("#main").Nodes, 1)339 assertLength(t, doc.Find("#main").Children().Nodes, 0)340 printSel(t, doc.Selection)341}342func TestSetHtmlContext(t *testing.T) {343 doc := loadString(t, `344 <html>345 <body>346 <table>347 <tr>348 <th>Before1</th>349 </tr>350 <tr>351 <th>Before2</th>352 </tr>353 </table>354 </body>355 </html>`)356 doc.Find("table tr").SetHtml("<td class='c1'>Test</td><td class='c2'>Again</td>")357 assertLength(t, doc.Find("table th").Nodes, 0)358 assertLength(t, doc.Find("table td").Nodes, 4)359 assertLength(t, doc.Find("table tr").Nodes, 2)360 printSel(t, doc.Selection)361}362func TestSetText(t *testing.T) {363 doc := Doc2Clone()364 q := doc.Find("#main, #foot")365 repl := "<div id=\"replace\">test</div>"366 q.SetText(repl)367 assertLength(t, doc.Find("#replace").Nodes, 0)368 assertLength(t, doc.Find("#main, #foot").Nodes, 2)369 if q.Text() != (repl + repl) {370 t.Errorf("Expected text to be %v, found %v", (repl + repl), q.Text())371 }372 h, err := q.Html()373 if err != nil {374 t.Errorf("Error: %v", err)375 }376 esc := "&lt;div id=&#34;replace&#34;&gt;test&lt;/div&gt;"377 if h != esc {378 t.Errorf("Expected html to be %v, found %v", esc, h)379 }380 printSel(t, doc.Selection)381}382func TestReplaceWithSelection(t *testing.T) {383 doc := Doc2Clone()384 sel := doc.Find("#nf6").ReplaceWithSelection(doc.Find("#nf5"))385 assertSelectionIs(t, sel, "#nf6")386 assertLength(t, doc.Find("#nf6").Nodes, 0)387 assertLength(t, doc.Find("#nf5").Nodes, 1)388 printSel(t, doc.Selection)389}390func TestUnwrap(t *testing.T) {391 doc := Doc2Clone()392 doc.Find("#nf5").Unwrap()393 assertLength(t, doc.Find("#foot").Nodes, 0)394 assertLength(t, doc.Find("body > #nf1").Nodes, 1)395 assertLength(t, doc.Find("body > #nf5").Nodes, 1)396 printSel(t, doc.Selection)397 doc = Doc2Clone()398 doc.Find("#nf5, #n1").Unwrap()399 assertLength(t, doc.Find("#foot").Nodes, 0)400 assertLength(t, doc.Find("#main").Nodes, 0)401 assertLength(t, doc.Find("body > #n1").Nodes, 1)402 assertLength(t, doc.Find("body > #nf5").Nodes, 1)403 printSel(t, doc.Selection)404}405func TestUnwrapBody(t *testing.T) {406 doc := Doc2Clone()407 doc.Find("#main").Unwrap()408 assertLength(t, doc.Find("body").Nodes, 1)409 assertLength(t, doc.Find("body > #main").Nodes, 1)410 printSel(t, doc.Selection)411}412func TestUnwrapHead(t *testing.T) {413 doc := Doc2Clone()414 doc.Find("title").Unwrap()415 assertLength(t, doc.Find("head").Nodes, 0)416 assertLength(t, doc.Find("head > title").Nodes, 0)417 assertLength(t, doc.Find("title").Nodes, 1)418 printSel(t, doc.Selection)419}420func TestUnwrapHtml(t *testing.T) {421 doc := Doc2Clone()422 doc.Find("head").Unwrap()423 assertLength(t, doc.Find("html").Nodes, 0)424 assertLength(t, doc.Find("html head").Nodes, 0)425 assertLength(t, doc.Find("head").Nodes, 1)426 printSel(t, doc.Selection)427}428func TestWrap(t *testing.T) {429 doc := Doc2Clone()430 doc.Find("#nf1").Wrap("#nf2")431 nf1 := doc.Find("#foot #nf2 #nf1")432 assertLength(t, nf1.Nodes, 1)433 nf2 := doc.Find("#nf2")434 assertLength(t, nf2.Nodes, 2)435 printSel(t, doc.Selection)436}437func TestWrapEmpty(t *testing.T) {438 doc := Doc2Clone()439 doc.Find("#nf1").Wrap("#doesnt-exist")440 origHtml, _ := Doc2().Html()441 newHtml, _ := doc.Html()442 if origHtml != newHtml {443 t.Error("Expected the two documents to be identical.")444 }445 printSel(t, doc.Selection)446}447func TestWrapHtml(t *testing.T) {448 doc := Doc2Clone()449 doc.Find(".odd").WrapHtml(wrapHtml)450 nf2 := doc.Find("#ins #nf2")451 assertLength(t, nf2.Nodes, 1)452 printSel(t, doc.Selection)453}454func TestWrapSelection(t *testing.T) {455 doc := Doc2Clone()456 doc.Find("#nf1").WrapSelection(doc.Find("#nf2"))457 nf1 := doc.Find("#foot #nf2 #nf1")458 assertLength(t, nf1.Nodes, 1)459 nf2 := doc.Find("#nf2")460 assertLength(t, nf2.Nodes, 2)461 printSel(t, doc.Selection)462}463func TestWrapAll(t *testing.T) {464 doc := Doc2Clone()465 doc.Find(".odd").WrapAll("#nf1")466 nf1 := doc.Find("#main #nf1")467 assertLength(t, nf1.Nodes, 1)468 sel := nf1.Find("#n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")469 assertLength(t, sel.Nodes, 1)470 printSel(t, doc.Selection)471}472func TestWrapAllHtml(t *testing.T) {473 doc := Doc2Clone()474 doc.Find(".odd").WrapAllHtml(wrapHtml)475 nf1 := doc.Find("#main div#ins div p em b #n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")476 assertLength(t, nf1.Nodes, 1)477 printSel(t, doc.Selection)478}479func TestWrapInnerNoContent(t *testing.T) {480 doc := Doc2Clone()481 doc.Find(".one").WrapInner(".two")482 twos := doc.Find(".two")483 assertLength(t, twos.Nodes, 4)484 assertLength(t, doc.Find(".one .two").Nodes, 2)485 printSel(t, doc.Selection)486}487func TestWrapInnerWithContent(t *testing.T) {488 doc := Doc3Clone()489 doc.Find(".one").WrapInner(".two")490 twos := doc.Find(".two")491 assertLength(t, twos.Nodes, 4)492 assertLength(t, doc.Find(".one .two").Nodes, 2)493 printSel(t, doc.Selection)494}495func TestWrapInnerNoWrapper(t *testing.T) {496 doc := Doc2Clone()497 doc.Find(".one").WrapInner(".not-exist")498 twos := doc.Find(".two")499 assertLength(t, twos.Nodes, 2)500 assertLength(t, doc.Find(".one").Nodes, 2)501 assertLength(t, doc.Find(".one .two").Nodes, 0)502 printSel(t, doc.Selection)503}504func TestWrapInnerHtml(t *testing.T) {505 doc := Doc2Clone()506 doc.Find("#foot").WrapInnerHtml(wrapHtml)507 foot := doc.Find("#foot div#ins div p em b #nf1 ~ #nf2 ~ #nf3")508 assertLength(t, foot.Nodes, 1)509 printSel(t, doc.Selection)510}511func TestParsingRespectsVaryingContext(t *testing.T) {512 docA := loadString(t, `513 <html>514 <body>515 <a class="x"></a>516 </body>517 </html>`)518 docTable := loadString(t, `519 <html>520 <body>521 <table class="x"></table>522 </body>523 </html>`)524 docBoth := loadString(t, `525 <html>526 <body>527 <table class="x"></table>528 <a class="x"></a>529 </body>530 </html>`)531 sA := docA.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")532 sTable := docTable.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")533 sBoth := docBoth.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")534 printSel(t, docA.Selection)535 printSel(t, docTable.Selection)536 printSel(t, docBoth.Selection)537 oA, _ := sA.Html()538 oTable, _ := sTable.Html()539 if oA == oTable {540 t.Errorf("Expected inner html of <a> and <table> to not be equal, but got %s and %s", oA, oTable)541 }542 oBothTable, _ := sBoth.First().Html()543 if oBothTable != oTable {544 t.Errorf("Expected inner html of <table> and <table> in doc containing both tags to be equal, but got %s and %s",545 oTable,546 oBothTable)547 }548 oBothA, _ := sBoth.Last().Html()549 if oBothA != oA {550 t.Errorf("Expected inner html of <a> and <a> in doc containing both tags to be equal, but got %s and %s",551 oA,552 oBothA)553 }554}555func TestHtmlWithNonElementNode(t *testing.T) {556 const data = `557<html>558 <head>559 </head>560 <body>561 <p>562 This is <span>some</span><b>text</b>.563 </p>564 </body>565</html>566`567 cases := map[string]func(*Selection, string) *Selection{568 "AfterHtml": (*Selection).AfterHtml,569 "AppendHtml": (*Selection).AppendHtml,570 "BeforeHtml": (*Selection).BeforeHtml,571 "PrependHtml": (*Selection).PrependHtml,572 "ReplaceWithHtml": (*Selection).ReplaceWithHtml,573 "SetHtml": (*Selection).SetHtml,574 }575 for nm, fn := range cases {576 // this test is only to make sure that the HTML parsing/manipulation577 // methods do not raise panics when executed over Selections that contain578 // non-Element nodes.579 t.Run(nm, func(t *testing.T) {580 doc := loadString(t, data)581 sel := doc.Find("p").Contents()582 func() {583 defer func() {584 if err := recover(); err != nil {585 t.Fatal(err)586 }587 }()588 fn(sel, "<div></div>")589 }()590 // print the resulting document in verbose mode591 h, err := OuterHtml(doc.Selection)592 if err != nil {593 log.Fatal(err)594 }595 t.Log(h)596 })597 }598}...

Full Screen

Full Screen

property_test.go

Source:property_test.go Github

copy

Full Screen

1package goquery2import (3 "regexp"4 "strings"5 "testing"6)7func TestAttrExists(t *testing.T) {8 if val, ok := Doc().Find("a").Attr("href"); !ok {9 t.Error("Expected a value for the href attribute.")10 } else {11 t.Logf("Href of first anchor: %v.", val)12 }13}14func TestAttrOr(t *testing.T) {15 if val := Doc().Find("a").AttrOr("fake-attribute", "alternative"); val != "alternative" {16 t.Error("Expected an alternative value for 'fake-attribute' attribute.")17 } else {18 t.Logf("Value returned for not existing attribute: %v.", val)19 }20 if val := Doc().Find("zz").AttrOr("fake-attribute", "alternative"); val != "alternative" {21 t.Error("Expected an alternative value for 'fake-attribute' on an empty selection.")22 } else {23 t.Logf("Value returned for empty selection: %v.", val)24 }25}26func TestAttrNotExist(t *testing.T) {27 if val, ok := Doc().Find("div.row-fluid").Attr("href"); ok {28 t.Errorf("Expected no value for the href attribute, got %v.", val)29 }30}31func TestRemoveAttr(t *testing.T) {32 sel := Doc2Clone().Find("div")33 sel.RemoveAttr("id")34 _, ok := sel.Attr("id")35 if ok {36 t.Error("Expected there to be no id attributes set")37 }38}39func TestSetAttr(t *testing.T) {40 sel := Doc2Clone().Find("#main")41 sel.SetAttr("id", "not-main")42 val, ok := sel.Attr("id")43 if !ok {44 t.Error("Expected an id attribute on main")45 }46 if val != "not-main" {47 t.Errorf("Expected an attribute id to be not-main, got %s", val)48 }49}50func TestSetAttr2(t *testing.T) {51 sel := Doc2Clone().Find("#main")52 sel.SetAttr("foo", "bar")53 val, ok := sel.Attr("foo")54 if !ok {55 t.Error("Expected an 'foo' attribute on main")56 }57 if val != "bar" {58 t.Errorf("Expected an attribute 'foo' to be 'bar', got '%s'", val)59 }60}61func TestText(t *testing.T) {62 txt := Doc().Find("h1").Text()63 if strings.Trim(txt, " \n\r\t") != "Provok.in" {64 t.Errorf("Expected text to be Provok.in, found %s.", txt)65 }66}67func TestText2(t *testing.T) {68 txt := Doc().Find(".hero-unit .container-fluid .row-fluid:nth-child(1)").Text()69 if ok, e := regexp.MatchString(`^\s+Provok\.in\s+Prove your point.\s+$`, txt); !ok || e != nil {70 t.Errorf("Expected text to be Provok.in Prove your point., found %s.", txt)71 if e != nil {72 t.Logf("Error: %s.", e.Error())73 }74 }75}76func TestText3(t *testing.T) {77 txt := Doc().Find(".pvk-gutter").First().Text()78 // There's an &nbsp; character in there...79 if ok, e := regexp.MatchString(`^[\s\x{00A0}]+$`, txt); !ok || e != nil {80 t.Errorf("Expected spaces, found <%v>.", txt)81 if e != nil {82 t.Logf("Error: %s.", e.Error())83 }84 }85}86func TestHtml(t *testing.T) {87 txt, e := Doc().Find("h1").Html()88 if e != nil {89 t.Errorf("Error: %s.", e)90 }91 if ok, e := regexp.MatchString(`^\s*<a href="/">Provok<span class="green">\.</span><span class="red">i</span>n</a>\s*$`, txt); !ok || e != nil {92 t.Errorf("Unexpected HTML content, found %s.", txt)93 if e != nil {94 t.Logf("Error: %s.", e.Error())95 }96 }97}98func TestNbsp(t *testing.T) {99 src := `<p>Some&nbsp;text</p>`100 d, err := NewDocumentFromReader(strings.NewReader(src))101 if err != nil {102 t.Fatal(err)103 }104 txt := d.Find("p").Text()105 ix := strings.Index(txt, "\u00a0")106 if ix != 4 {107 t.Errorf("Text: expected a non-breaking space at index 4, got %d", ix)108 }109 h, err := d.Find("p").Html()110 if err != nil {111 t.Fatal(err)112 }113 ix = strings.Index(h, "\u00a0")114 if ix != 4 {115 t.Errorf("Html: expected a non-breaking space at index 4, got %d", ix)116 }117}118func TestAddClass(t *testing.T) {119 sel := Doc2Clone().Find("#main")120 sel.AddClass("main main main")121 // Make sure that class was only added once122 if a, ok := sel.Attr("class"); !ok || a != "main" {123 t.Error("Expected #main to have class main")124 }125}126func TestAddClassSimilar(t *testing.T) {127 sel := Doc2Clone().Find("#nf5")128 sel.AddClass("odd")129 assertClass(t, sel, "odd")130 assertClass(t, sel, "odder")131 printSel(t, sel.Parent())132}133func TestAddEmptyClass(t *testing.T) {134 sel := Doc2Clone().Find("#main")135 sel.AddClass("")136 // Make sure that class was only added once137 if a, ok := sel.Attr("class"); ok {138 t.Errorf("Expected #main to not to have a class, have: %s", a)139 }140}141func TestAddClasses(t *testing.T) {142 sel := Doc2Clone().Find("#main")143 sel.AddClass("a b")144 // Make sure that class was only added once145 if !sel.HasClass("a") || !sel.HasClass("b") {146 t.Errorf("#main does not have classes")147 }148}149func TestHasClass(t *testing.T) {150 sel := Doc().Find("div")151 if !sel.HasClass("span12") {152 t.Error("Expected at least one div to have class span12.")153 }154}155func TestHasClassNone(t *testing.T) {156 sel := Doc().Find("h2")157 if sel.HasClass("toto") {158 t.Error("Expected h1 to have no class.")159 }160}161func TestHasClassNotFirst(t *testing.T) {162 sel := Doc().Find(".alert")163 if !sel.HasClass("alert-error") {164 t.Error("Expected .alert to also have class .alert-error.")165 }166}167func TestRemoveClass(t *testing.T) {168 sel := Doc2Clone().Find("#nf1")169 sel.RemoveClass("one row")170 if !sel.HasClass("even") || sel.HasClass("one") || sel.HasClass("row") {171 classes, _ := sel.Attr("class")172 t.Error("Expected #nf1 to have class even, has ", classes)173 }174}175func TestRemoveClassSimilar(t *testing.T) {176 sel := Doc2Clone().Find("#nf5, #nf6")177 assertLength(t, sel.Nodes, 2)178 sel.RemoveClass("odd")179 assertClass(t, sel.Eq(0), "odder")180 printSel(t, sel)181}182func TestRemoveAllClasses(t *testing.T) {183 sel := Doc2Clone().Find("#nf1")184 sel.RemoveClass()185 if a, ok := sel.Attr("class"); ok {186 t.Error("All classes were not removed, has ", a)187 }188 sel = Doc2Clone().Find("#main")189 sel.RemoveClass()190 if a, ok := sel.Attr("class"); ok {191 t.Error("All classes were not removed, has ", a)192 }193}194func TestToggleClass(t *testing.T) {195 sel := Doc2Clone().Find("#nf1")196 sel.ToggleClass("one")197 if sel.HasClass("one") {198 t.Error("Expected #nf1 to not have class one")199 }200 sel.ToggleClass("one")201 if !sel.HasClass("one") {202 t.Error("Expected #nf1 to have class one")203 }204 sel.ToggleClass("one even row")205 if a, ok := sel.Attr("class"); ok {206 t.Errorf("Expected #nf1 to have no classes, have %q", a)207 }208}...

Full Screen

Full Screen

find

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, World!")4}5import (6func main() {7 fmt.Println("Hello, World!")8}9import (10func main() {11 fmt.Println("Hello, World!")12}13import (14func main() {15 fmt.Println("Hello, World!")16}17import (18func main() {19 fmt.Println("Hello, World!")20}21import (22func main() {23 fmt.Println("Hello, World!")24}25import (26func main() {27 fmt.Println("Hello, World!")28}29import (30func main() {31 fmt.Println("Hello, World!")32}33import (34func main() {35 fmt.Println("Hello, World!")36}37import (38func main() {39 fmt.Println("Hello, World!")40}41import (42func main() {43 fmt.Println("Hello, World!")44}45import (46func main() {47 fmt.Println("Hello, World!")48}49import (50func main() {51 fmt.Println("Hello, World!")52}53import

Full Screen

Full Screen

find

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter the number: ")4 fmt.Scanln(&input)5 num, err := strconv.Atoi(input)6 if err != nil {7 fmt.Println(err)8 } else {9 fmt.Println(num)10 }11}

Full Screen

Full Screen

find

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 reg, err := regexp.Compile("^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$")4 if err != nil {5 fmt.Println("Error in compiling regex")6 }7 fmt.Println(reg.FindString("

Full Screen

Full Screen

find

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var re = regexp.MustCompile(`\w+`)4 fmt.Println(re.FindAllString("Hello World", 2))5}6import (7func main() {8 var re = regexp.MustCompile(`\w+`)9 fmt.Println(re.FindAllString("Hello World", -1))10}11import (12func main() {13 var re = regexp.MustCompile(`\w+`)14 fmt.Println(re.FindAllString("Hello World", -2))15}16import (17func main() {18 var re = regexp.MustCompile(`\w+`)19 fmt.Println(re.FindAllString("Hello World", 0))20}21import (22func main() {23 var re = regexp.MustCompile(`\w+`)24 fmt.Println(re.FindAllString("Hello World", 1))25}26import (27func main() {28 var re = regexp.MustCompile(`\w+`)29 fmt.Println(re.FindAllString("Hello World", 2))30}31import (32func main() {33 var re = regexp.MustCompile(`\w+`)34 fmt.Println(re.FindAllString("Hello World", -1))35}36import (37func main() {38 var re = regexp.MustCompile(`\w+`)39 fmt.Println(re.FindAllString("Hello World", -2))40}

Full Screen

Full Screen

find

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World!")4}5Recommended Posts: Regular Expression | replaceAllString() method in Golang6Regular Expression | findStringIndex() method in Golang7Regular Expression | findAllStringIndex() method in Golang8Regular Expression | findStringSubmatchIndex() method in Golang9Regular Expression | findStringSubmatch() method in Golang10Regular Expression | findAllStringSubmatch() method in Golang11Regular Expression | replaceAllStringFunc() method in Golang12Regular Expression | findAllString() method in Golang13Regular Expression | findStringSubmatchIndex() method in Golang14Regular Expression | replaceAllLiteralString() method in Golang15Regular Expression | findAllStringSubmatchIndex() method in Golang16Regular Expression | replaceAllLiteral() method in Golang17Regular Expression | replaceAllString() method in Golang18Regular Expression | replaceAll() method in Golang19Regular Expression | findAllStringSubmatch() method in Golang20Regular Expression | findString() method in Golang21Regular Expression | matchString() method in Golang22Regular Expression | split() method in Golang23Regular Expression | splitN() method in Golang24Regular Expression | match() method in Golang

Full Screen

Full Screen

find

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Using Find method of main class")4 fmt.Println(main.Find(2))5}6Note : If you are using the same package name in the same directory then it will give you an error. In that case, you have to use the alias for the package name. Ex: “import m “github.com/GoLang-Training/GoLang-Training-1/2”

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful