Skip to content

Commit

Permalink
Merge branch 'feat/log-levels' into merge-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Hungerarray committed Feb 3, 2025
2 parents 94e4bfb + bfba44f commit 194ed78
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 234 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pip install locatr

#### Python example

```
```py
# example assumes that there is already a page opened in the selenium session.
import os

Expand Down
84 changes: 53 additions & 31 deletions eval/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"flag"
"fmt"
"log"
"time"

"github.com/playwright-community/playwright-go"
"github.com/vertexcover-io/locatr/golang/logger"
)

var DefaultEvalFolder = "eval/evalFiles"
Expand All @@ -15,65 +15,73 @@ func runEval(browser playwright.Browser, eval *evalConfigYaml) []evalResult {
var results []evalResult = make([]evalResult, 0)
page, err := browser.NewPage()
if err != nil {
log.Fatalf("Error creating page: %s", err)
logger.Logger.Error("Error creating page", "error", err)
return nil
}
defer page.Close()

if _, err := page.Goto(eval.Url); err != nil {
log.Fatalf("Error navigating to %s: %s", eval.Url, err)
logger.Logger.Error("Error navigating to URL", "url", eval.Url, "error", err)
return nil
}

if eval.Config.PageLoadTimeout > 0 {
log.Printf("Waiting for %d seconds for page to load", eval.Config.PageLoadTimeout)
logger.Logger.Info("Waiting for page to load", "timeout", eval.Config.PageLoadTimeout)
time.Sleep(time.Duration(eval.Config.PageLoadTimeout) * time.Second)
}

playWrightLocatr := getLocatrFromYamlConfig(eval, page)
var lastLocatr playwright.Locator

for _, step := range eval.Steps {
log.Printf("Running step %s", step.Name)
logger.Logger.Info("Running step", "stepName", step.Name)

if step.Action != "" {
switch step.Action {
case "click":
if err := lastLocatr.Nth(step.ElementNo).Click(); err != nil {
log.Printf("Error clicking on locator: %s", err)
logger.Logger.Error("Error clicking on locator", "stepName", step.Name, "error", err)
continue
} else {
log.Printf("Clicked on item %s", step.Name)
logger.Logger.Info("Clicked on item", "stepName", step.Name)
}
case "fill":
if err := lastLocatr.Nth(step.ElementNo).Fill(step.FillText); err != nil {
log.Printf("Error filling text: %s", err)
logger.Logger.Error("Error filling text", "stepName", step.Name, "error", err)
continue
} else {
log.Printf("Filled text %s in locatr %s", step.FillText, step.Name)
logger.Logger.Info("Filled text in locator", "stepName", step.Name, "fillText", step.FillText)
}
case "hover":
if err := lastLocatr.Nth(step.ElementNo).Hover(); err != nil {
log.Printf("Error hovering on locator: %s", err)
logger.Logger.Error("Error hovering on locator", "stepName", step.Name, "error", err)
continue
} else {
log.Printf("Hovered on item %s", step.Name)
logger.Logger.Info("Hovered on item", "stepName", step.Name)
}
case "press":
if err := lastLocatr.Nth(step.ElementNo).Press(step.Key); err != nil {
log.Printf("Error pressing key: %s", err)
logger.Logger.Error("Error pressing key", "stepName", step.Name, "key", step.Key, "error", err)
continue
} else {
log.Printf("Pressed key %s on locatr %s", step.Key, step.Name)
logger.Logger.Info("Pressed key on locator", "stepName", step.Name, "key", step.Key)
}
default:
log.Printf("Unknown action %s", step.Action)
logger.Logger.Warn("Unknown action", "action", step.Action)
continue
}
log.Printf("Waiting for %d seconds after action %s", step.Timeout, step.Action)

logger.Logger.Info("Waiting after action", "timeout", step.Timeout, "action", step.Action)
time.Sleep(time.Duration(step.Timeout) * time.Second)
}

if step.UserRequest == "" {
continue
}

locatrOutput, err := playWrightLocatr.GetLocatr(step.UserRequest)
if err != nil {
log.Fatalf("Error getting locatr for step %s: %s", step.Name, err)
logger.Logger.Error("Error getting locator for step", "stepName", step.Name, "error", err)
results = append(results, evalResult{
Url: eval.Url,
UserRequest: step.UserRequest,
Expand All @@ -84,24 +92,23 @@ func runEval(browser playwright.Browser, eval *evalConfigYaml) []evalResult {
})
continue
}

lastLocatr = page.Locator(locatrOutput.Selectors[0])
currentResults := playWrightLocatr.GetLocatrResults()
currentLocatrs := currentResults[len(currentResults)-1].AllLocatrs

if !compareSlices(step.ExpectedLocatrs,
currentLocatrs) {
log.Printf("Expected locatrs %v, but got %v",
step.ExpectedLocatrs, currentLocatrs)
if !compareSlices(step.ExpectedLocatrs, currentLocatrs) {
logger.Logger.Warn("Expected locators do not match", "expectedLocatrs", step.ExpectedLocatrs, "generatedLocatrs", currentLocatrs)
results = append(results, evalResult{
Url: eval.Url,
UserRequest: step.UserRequest,
Passed: false,
GeneratedLocatrs: currentLocatrs,
ExpectedLocatrs: step.ExpectedLocatrs,
Error: "All generated locatrs do not match expected locatrs",
Error: "All generated locators do not match expected locators",
})
} else {
log.Printf("Step %s finished successfully", step.Name)
logger.Logger.Info("Step finished successfully", "stepName", step.Name)
results = append(results, evalResult{
Url: eval.Url,
UserRequest: step.UserRequest,
Expand All @@ -112,54 +119,69 @@ func runEval(browser playwright.Browser, eval *evalConfigYaml) []evalResult {
})
}
}

return results
}

func main() {
evalFolderPath := flag.String("evalFolder", DefaultEvalFolder, "Path to folder with eval files")
runOnly := flag.String("runOnly", "", "Run only the specified eval file")
flag.Parse()

var evalFiles []string
var evalYamlPath string = DefaultEvalFolder

if *runOnly == "" {
if *evalFolderPath != "" {
evalYamlPath = *evalFolderPath
}
evalFiles = getAllYamlFiles(evalYamlPath)
evalFiles, err := getAllYamlFiles(evalYamlPath)
if err != nil {
logger.Logger.Error("Error retrieving YAML files", "folder", evalYamlPath, "error", err)
return
}
if len(evalFiles) == 0 {
log.Fatal("No yaml files found in folder")
logger.Logger.Error("No YAML files found in folder", "folder", evalYamlPath)
return
}
} else {
evalFiles = []string{*runOnly}
}

pw, err := playwright.Run()
if err != nil {
log.Fatalf("Error running playwright: %s", err)
logger.Logger.Error("Error running playwright", "error", err)
return
}

browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
Headless: playwright.Bool(false),
})
if err != nil {
log.Fatalf("Error launching browser: %s", err)
logger.Logger.Error("Error launching browser", "error", err)
return
}

for _, evalFile := range evalFiles {
eval, err := readYamlFile(fmt.Sprintf("%s/%s", evalYamlPath, evalFile))
if err != nil {
log.Printf("Error reading yaml file %s... skipping", evalFile)
logger.Logger.Error("Error reading YAML file, skipping", "file", evalFile, "error", err)
continue
}
log.Printf("Running eval %s", eval.Name)

logger.Logger.Info("Running eval", "evalName", eval.Name)

results := runEval(browser, eval)
if results != nil {
writeEvalResultToCsv(results, fmt.Sprintf("%s.csv", evalFile))
log.Printf("Eval %s results written to %s.csv", eval.Name, evalFile)
logger.Logger.Info("Eval results written", "evalName", eval.Name, "file", fmt.Sprintf("%s.csv", evalFile))
}
log.Printf("Eval %s finished", eval.Name)

logger.Logger.Info("Eval finished", "evalName", eval.Name)
}

err = browser.Close()
if err != nil {
log.Fatalf("Error closing browser: %s", err)
logger.Logger.Error("Error closing browser", "error", err)
}
}
32 changes: 18 additions & 14 deletions eval/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"strings"

Expand All @@ -15,18 +14,22 @@ import (
"gopkg.in/yaml.v3"
)

func getAllYamlFiles(folder string) []string {
func getAllYamlFiles(folder string) ([]string, error) {
files, err := os.ReadDir(folder)
if err != nil {
log.Fatal(err)
logger.Logger.Error("Failed to read directory", "folder", folder, "error", err)
return nil, err
}

var res []string
for _, file := range files {
if strings.HasSuffix(file.Name(), ".yaml") {
res = append(res, file.Name())
}
}
return res
logger.Logger.Info("Successfully retrieved YAML files",
"folder", folder, "fileCount", len(res))
return res, nil
}
func contains(locatrs []string, loc string) bool {
for _, l := range locatrs {
Expand All @@ -36,6 +39,7 @@ func contains(locatrs []string, loc string) bool {
}
return false
}

func compareSlices(yamlLocatrs []string, locatrs []string) bool {
for _, loc := range yamlLocatrs {
if contains(locatrs, loc) {
Expand All @@ -47,18 +51,21 @@ func compareSlices(yamlLocatrs []string, locatrs []string) bool {
func readYamlFile(filePath string) (*evalConfigYaml, error) {
yamlFile, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Error reading file %s: %s", filePath, err)
logger.Logger.Error("Error reading file", "filePath", filePath, "error", err)
return nil, err

}

var eval evalConfigYaml
err = yaml.Unmarshal(yamlFile, &eval)
if err != nil {
log.Fatalf("Error unmarshalling yaml file %s: %s", filePath, err)
logger.Logger.Error("Error unmarshalling YAML file", "filePath", filePath, "error", err)
return nil, err
}

logger.Logger.Info("Successfully read and unmarshalled YAML file", "filePath", filePath)
return &eval, nil
}

func getLocatrFromYamlConfig(evalConfig *evalConfigYaml, page playwright.Page) *playwrightLocatr.PlaywrightLocator {
locatrOptions := locatr.BaseLocatrOptions{}
if evalConfig.Config.UseCache {
Expand All @@ -74,16 +81,13 @@ func getLocatrFromYamlConfig(evalConfig *evalConfigYaml, page playwright.Page) *
reRankClient := reranker.NewCohereClient(os.Getenv("COHERE_API_KEY"))
locatrOptions.ReRankClient = reRankClient
}
locatrOptions.LogConfig = logger.LogConfig{
Level: logger.Debug,
}
return playwrightLocatr.NewPlaywrightLocatr(page, locatrOptions)
}

func writeEvalResultToCsv(results []evalResult, fileName string) {
file, err := os.Create(fileName)
if err != nil {
log.Fatalf("Error creating csv file %s: %s", fileName, err)
logger.Logger.Error("Error creating CSV file", "fileName", fileName, "error", err)
return
}
defer file.Close()
Expand All @@ -94,7 +98,7 @@ func writeEvalResultToCsv(results []evalResult, fileName string) {
header := []string{"Url", "UserRequest", "Passed", "GeneratedLocatrs", "ExpectedLocatrs", "Error"}
err = writer.Write(header)
if err != nil {
log.Fatalf("Error writing header to csv file %s: %s", fileName, err)
logger.Logger.Error("Error writing header to CSV file", "fileName", fileName, "error", err)
return
}

Expand All @@ -109,13 +113,13 @@ func writeEvalResultToCsv(results []evalResult, fileName string) {
}
err = writer.Write(row)
if err != nil {
log.Fatalf("Error writing row to csv file %s: %s", fileName, err)
logger.Logger.Error("Error writing row to CSV file", "fileName", fileName, "error", err, "row", row)
return
}
}

err = writer.Error()
if err != nil {
log.Fatalf("Error occurred during writing the csv file %s: %s", fileName, err)
logger.Logger.Error("Error occurred during writing the CSV file", "fileName", fileName, "error", err)
}
}
5 changes: 0 additions & 5 deletions examples/appium/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
locatr "github.com/vertexcover-io/locatr/golang"
appiumLocatr "github.com/vertexcover-io/locatr/golang/appium"
"github.com/vertexcover-io/locatr/golang/llm"
"github.com/vertexcover-io/locatr/golang/logger"
)

func main() {
Expand All @@ -18,10 +17,6 @@ func main() {
)
bLocatr := locatr.BaseLocatrOptions{
LlmClient: llmClient,
// ReRankClient: reranker.NewCohereClient(os.Getenv("RERANK_KEY")),
LogConfig: logger.LogConfig{
Level: logger.Debug,
},
}
aLocatr, err := appiumLocatr.NewAppiumLocatr(
"https://device.pcloudy.com/appiumcloud/wd/hub",
Expand Down
2 changes: 0 additions & 2 deletions examples/dockerhub/docker_hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/playwright-community/playwright-go"
locatr "github.com/vertexcover-io/locatr/golang"
"github.com/vertexcover-io/locatr/golang/llm"
"github.com/vertexcover-io/locatr/golang/logger"
"github.com/vertexcover-io/locatr/golang/playwrightLocatr"
)

Expand Down Expand Up @@ -50,7 +49,6 @@ func main() {
log.Fatalf("could not create llm client: %v", err)
}
options := locatr.BaseLocatrOptions{UseCache: true,
LogConfig: logger.LogConfig{Level: logger.Debug},
LlmClient: llmClient,
ResultsFilePath: "docker_hub.json",
}
Expand Down
1 change: 1 addition & 0 deletions examples/python/appium.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pyright: reportPrivateImportUsage=false
import os

from appium import webdriver
Expand Down
3 changes: 1 addition & 2 deletions examples/steam/steam.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/playwright-community/playwright-go"
locatr "github.com/vertexcover-io/locatr/golang"
"github.com/vertexcover-io/locatr/golang/llm"
"github.com/vertexcover-io/locatr/golang/logger"
"github.com/vertexcover-io/locatr/golang/playwrightLocatr"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ func main() {
if err != nil {
log.Fatalf("could not create llm client: %v", err)
}
options := locatr.BaseLocatrOptions{UseCache: true, LogConfig: logger.LogConfig{Level: logger.Silent}, LlmClient: llmClient}
options := locatr.BaseLocatrOptions{UseCache: true, LlmClient: llmClient}

playWrightLocatr := playwrightLocatr.NewPlaywrightLocatr(page, options)

Expand Down
Loading

0 comments on commit 194ed78

Please sign in to comment.