Skip to content

Commit ebb9508

Browse files
committed
Additional result validation
1 parent 03f9fab commit ebb9508

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

chromedp/click/main.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
6363
exec := args[0]
6464
flags.Usage = func() {
6565
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")
6767
fmt.Fprintf(stderr, "\nCommand line options:\n")
6868
flags.PrintDefaults()
6969
fmt.Fprintf(stderr, "\nEnvironment vars:\n")
@@ -102,23 +102,37 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
102102
return fmt.Errorf("new tab: %w", err)
103103
}
104104

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+
)
106110
if err != nil {
107-
return fmt.Errorf("navigate %s: %w", url, err)
111+
return fmt.Errorf("click: %w", err)
108112
}
109113

114+
// Validation
110115
var currentURL string
116+
var priceText string
117+
var reviewNames []string
118+
var reviewTexts []string
111119
err = chromedp.Run(ctx,
112-
chromedp.Click("a[href='campfire-commerce/']", chromedp.ByQuery),
113-
// wait or sleep?
114120
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),
115124
)
116125
if err != nil {
117-
return fmt.Errorf("click: %w", err)
126+
return fmt.Errorf("checks failed: %w", err)
118127
}
119-
120128
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")
122136
}
123137

124138
return nil

0 commit comments

Comments
 (0)