@@ -63,7 +63,7 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
63
63
exec := args [0 ]
64
64
flags .Usage = func () {
65
65
fmt .Fprintf (stderr , "usage: %s <url>]\n " , exec )
66
- fmt .Fprintf (stderr , "chromedp fetch url.\n " )
66
+ fmt .Fprintf (stderr , "chromedp fetch url and click on `campfire-commerce` .\n " )
67
67
fmt .Fprintf (stderr , "\n Command line options:\n " )
68
68
flags .PrintDefaults ()
69
69
fmt .Fprintf (stderr , "\n Environment vars:\n " )
@@ -102,23 +102,37 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
102
102
return fmt .Errorf ("new tab: %w" , err )
103
103
}
104
104
105
- err := chromedp .Run (ctx , chromedp .Navigate (url ))
105
+ // Navigate and click on the link
106
+ err := chromedp .Run (ctx ,
107
+ chromedp .Navigate (url ),
108
+ chromedp .Click ("a[href='campfire-commerce/']" , chromedp .ByQuery ),
109
+ )
106
110
if err != nil {
107
- return fmt .Errorf ("navigate %s : %w" , url , err )
111
+ return fmt .Errorf ("click : %w" , err )
108
112
}
109
113
114
+ // Validation
110
115
var currentURL string
116
+ var priceText string
117
+ var reviewNames []string
118
+ var reviewTexts []string
111
119
err = chromedp .Run (ctx ,
112
- chromedp .Click ("a[href='campfire-commerce/']" , chromedp .ByQuery ),
113
- // wait or sleep?
114
120
chromedp .Location (& currentURL ),
121
+ chromedp .Text ("#product-price" , & priceText , chromedp .NodeVisible , chromedp .ByQuery ),
122
+ chromedp .Evaluate (`Array.from(document.querySelectorAll('#product-reviews > div h4')).map(e => e.textContent)` , & reviewNames ),
123
+ chromedp .Evaluate (`Array.from(document.querySelectorAll('#product-reviews > div p')).map(e => e.textContent)` , & reviewTexts ),
115
124
)
116
125
if err != nil {
117
- return fmt .Errorf ("click : %w" , err )
126
+ return fmt .Errorf ("checks failed : %w" , err )
118
127
}
119
-
120
128
if currentURL != "http://127.0.0.1:1234/campfire-commerce/" {
121
- log .Fatal ("The new page URL is not as expected." )
129
+ return errors .New ("the new page URL is not as expected" )
130
+ }
131
+ if priceText != "$244.99" {
132
+ return fmt .Errorf ("incorrect product price: %s" , priceText )
133
+ }
134
+ if len (reviewNames ) != 3 || len (reviewTexts ) != 3 {
135
+ return errors .New ("incorrect reviews count" )
122
136
}
123
137
124
138
return nil
0 commit comments