Skip to content

Commit

Permalink
Merge pull request #5 from DanielFillol/daniel
Browse files Browse the repository at this point in the history
Get Current URL
  • Loading branch information
DanielFillol authored May 27, 2024
2 parents 878d1f5 + f5e8603 commit 7c973cd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
16 changes: 16 additions & 0 deletions goSpider.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,19 @@ func (nav *Navigator) Close() {
nav.Cancel()
nav.Logger.Println("Navigator instance closed successfully")
}

// GetCurrentURL extracts the current URL from the browser
// Returns the current URL as a string and an error if any
func (nav *Navigator) GetCurrentURL() (string, error) {
nav.Logger.Println("Extracting the current URL")
var currentURL string
err := chromedp.Run(nav.Ctx,
chromedp.Location(&currentURL),
)
if err != nil {
nav.Logger.Printf("Failed to extract current URL: %v\n", err)
return "", fmt.Errorf("failed to extract current URL: %v", err)
}
nav.Logger.Println("Current URL extracted successfully")
return currentURL, nil
}
42 changes: 42 additions & 0 deletions goSpider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,45 @@ func TestWaitForAJAX(t *testing.T) {
t.Errorf("WaitForAJAX error: %v", err)
}
}

// TestGetCurrentURL tests extracting the current URL from the browser
func TestGetCurrentURL(t *testing.T) {
// Navigate to the main page
htmlContent, err := nav.FetchHTML("http://localhost:8080")
if err != nil {
t.Errorf("FetchHTML error: %v", err)
}
if htmlContent == "" {
t.Error("FetchHTML returned empty content")
}

// Extract and verify the current URL
currentURL, err := nav.GetCurrentURL()
if err != nil {
t.Errorf("GetCurrentURL error: %v", err)
}

expectedURL := "http://localhost:8080/"
if currentURL != expectedURL {
t.Errorf("Expected URL: %s, but got: %s", expectedURL, currentURL)
}

// Navigate to page 2
err = nav.ClickButton("#linkToPage2")
if err != nil {
t.Errorf("ClickButton error: %v", err)
}

time.Sleep(2 * time.Second) // Wait for navigation to complete

// Extract and verify the current URL for page 2
currentURL, err = nav.GetCurrentURL()
if err != nil {
t.Errorf("GetCurrentURL error: %v", err)
}

expectedURL = "http://localhost:8080/page2"
if currentURL != expectedURL {
t.Errorf("Expected URL: %s, but got: %s", expectedURL, currentURL)
}
}
15 changes: 15 additions & 0 deletions testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func StartTestServer() *http.Server {
<html>
<head><title>Test Page</title></head>
<body>
<a href="/page2" id="linkToPage2">Go to Page 2</a>
<button id="exampleButton" onclick="showDynamicContent()">Click Me</button>
<form id="searchForm">
<input type="text" id="searchBar" />
Expand Down Expand Up @@ -64,6 +65,20 @@ func StartTestServer() *http.Server {
</html>
`))
})

mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<!DOCTYPE html>
<html>
<head><title>Page 2</title></head>
<body>
<a href="/" id="linkToPage1">Go to Page 1</a>
<p>This is Page 2</p>
</body>
</html>
`))
})

server := &http.Server{Addr: ":8080", Handler: mux}
go server.ListenAndServe()
return server
Expand Down

0 comments on commit 7c973cd

Please sign in to comment.