cdpu

package
v2.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 13 Imported by: 0

README

goutils/v2/cdpu

The cdpu package is a collection of utility functions designed to simplify common cdpu tasks.


Table of contents


Functions

CheckElement(web.Site, string, chan error)
CheckElement(web.Site, string, chan error) error

CheckElement checks if a web page element, identified by the provided XPath, exists within a specified timeout.

Note: Ensure to handle the error sent to the "done" channel in a separate goroutine or after calling this function to avoid deadlock.

Parameters:

site: A web.Site struct representing the target site. elementXPath: A string representing the XPath of the target element. done: A channel through which the function sends an error if the element is found or another error occurs.

Returns:

error: An error if the element is found, the web driver is not of type *Driver, failed to create a random wait time, or another error occurs.


Driver.GetContext()
GetContext() context.Context

GetContext retrieves the context associated with the Driver instance.

Returns:

context.Context: The context associated with this Driver.


Driver.SetContext(context.Context)
SetContext(context.Context)

SetContext associates a new context with the Driver instance.

Parameters:

ctx (context.Context): The new context to be associated with this Driver.


GetPageSource(web.Site)
GetPageSource(web.Site) string, error

GetPageSource retrieves the HTML source code of the currently loaded page in the provided Site's session.

Parameters:

site (web.Site): The site whose source code is to be retrieved.

Returns:

string: The source code of the currently loaded page. error: An error if any occurred during source code retrieval.


Init(bool, bool)
Init(bool, bool) web.Browser, error

Init initializes a chrome browser instance with the specified headless mode and SSL certificate error ignoring options, then returns the browser instance for further operations.

Parameters:

headless (bool): Whether or not the browser should be in headless mode. ignoreCertErrors (bool): Whether or not SSL certificate errors should be ignored.

Returns:

web.Browser: An initialized Browser instance. error: Any error encountered during initialization.


Navigate(web.Site, []InputAction, time.Duration)
Navigate(web.Site, []InputAction, time.Duration) error

Navigate performs the provided actions sequentially on the provided Site's session. It enables network events and sets up request logging.

Parameters:

site (web.Site): The site on which the actions should be performed. actions ([]InputAction): A slice of InputAction objects which define the actions to be performed. waitTime (time.Duration): The time to wait between actions.

Returns:

error: An error if any occurred during navigation.


SaveCookiesToDisk(web.Site, string)
SaveCookiesToDisk(web.Site, string) error

SaveCookiesToDisk retrieves cookies from the current session and writes them to a file.

Parameters:

site (web.Site): The site from which to retrieve cookies. filePath (string): The file path where the cookies should be saved.

Returns:

error: An error if any occurred during cookie retrieval or file writing.


ScreenShot(web.Site, string)
ScreenShot(web.Site, string) error

ScreenShot captures a screenshot of the currently loaded page in the provided Site's session and writes the image data to the provided file path.

Parameters:

site (web.Site): The site whose page a screenshot should be taken of. imgPath (string): The path to which the screenshot should be saved.

Returns:

error: An error if any occurred during screenshot capturing or saving.


Installation

To use the goutils/v2/cdpu package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/cdpu

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/cdpu"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/cdpu:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckElement

func CheckElement(site web.Site, elementXPath string, done chan error) error

CheckElement checks if a web page element, identified by the provided XPath, exists within a specified timeout.

**Note:** Ensure to handle the error sent to the "done" channel in a separate goroutine or after calling this function to avoid deadlock.

**Parameters:**

site: A web.Site struct representing the target site. elementXPath: A string representing the XPath of the target element. done: A channel through which the function sends an error if the element is found or another error occurs.

**Returns:**

error: An error if the element is found, the web driver is not of type *Driver, failed to create a random wait time, or another error occurs.

Example
package main

import (
	"log"
	"time"

	"github.com/l50/goutils/v2/web"
	"github.com/l50/goutils/v2/web/cdpu"
)

func main() {
	// Initialize the chrome browser
	browser, err := cdpu.Init(true, true)
	if err != nil {
		log.Fatalf("failed to initialize a chrome browser: %v", err)
	}

	defer web.CancelAll(browser.Cancels...)

	url := "https://somesite.com/login"

	// Set up the site with the browser's driver
	site := web.Site{
		LoginURL: url,
		Session: web.Session{
			Driver: browser.Driver,
		},
	}

	// Define the XPath for the element to check
	elementXPath := "//button[@id='login']"

	// Create a done channel
	done := make(chan error)

	// Call the function in a goroutine and wait for result
	go func() {
		err := cdpu.CheckElement(site, elementXPath, done)
		if err != nil {
			log.Printf("failed to execute CheckElement: %v", err)
		}
	}()

	// Handle the result from the done channel
	select {
	case err := <-done:
		if err != nil {
			log.Printf("Element found or another error occurred: %v", err)
		}
	case <-time.After(10 * time.Second):
		log.Println("Timeout exceeded while waiting for element check")
	}
}
Output:

func GetPageSource

func GetPageSource(site web.Site) (string, error)

GetPageSource retrieves the HTML source code of the currently loaded page in the provided Site's session.

**Parameters:**

site (web.Site): The site whose source code is to be retrieved.

**Returns:**

string: The source code of the currently loaded page. error: An error if any occurred during source code retrieval.

Example
package main

import (
	"log"

	"github.com/l50/goutils/v2/web"
	"github.com/l50/goutils/v2/web/cdpu"
)

func main() {
	site := web.Site{
		// initialize site
	}

	source, err := cdpu.GetPageSource(site)
	if err != nil {
		log.Fatalf("failed to get page source: %v", err)
	}

	_ = source
}
Output:

func Init

func Init(headless bool, ignoreCertErrors bool) (web.Browser, error)

Init initializes a chrome browser instance with the specified headless mode and SSL certificate error ignoring options, then returns the browser instance for further operations.

**Parameters:**

headless (bool): Whether or not the browser should be in headless mode. ignoreCertErrors (bool): Whether or not SSL certificate errors should be ignored.

**Returns:**

web.Browser: An initialized Browser instance. error: Any error encountered during initialization.

Example
package main

import (
	"log"

	"github.com/l50/goutils/v2/web/cdpu"
)

func main() {
	browser, err := cdpu.Init(true, true)
	if err != nil {
		log.Fatalf("failed to initialize a chrome browser: %v", err)
	}

	_ = browser
}
Output:

func Navigate(site web.Site, actions []InputAction, waitTime time.Duration) error

Navigate performs the provided actions sequentially on the provided Site's session. It enables network events and sets up request logging.

**Parameters:**

site (web.Site): The site on which the actions should be performed. actions ([]InputAction): A slice of InputAction objects which define the actions to be performed. waitTime (time.Duration): The time to wait between actions.

**Returns:**

error: An error if any occurred during navigation.

Example
package main

import (
	"log"

	"github.com/l50/goutils/v2/web"
	"github.com/l50/goutils/v2/web/cdpu"
)

func main() {
	actions := []cdpu.InputAction{
		// initialize actions
	}

	site := web.Site{
		// initialize site
	}

	if err := cdpu.Navigate(site, actions, 1000); err != nil {
		log.Fatalf("failed to navigate site: %v", err)
	}
}
Output:

func SaveCookiesToDisk added in v2.1.7

func SaveCookiesToDisk(site web.Site, filePath string) error

SaveCookiesToDisk retrieves cookies from the current session and writes them to a file.

**Parameters:**

site (web.Site): The site from which to retrieve cookies. filePath (string): The file path where the cookies should be saved.

**Returns:**

error: An error if any occurred during cookie retrieval or file writing.

func ScreenShot

func ScreenShot(site web.Site, imgPath string) error

ScreenShot captures a screenshot of the currently loaded page in the provided Site's session and writes the image data to the provided file path.

**Parameters:**

site (web.Site): The site whose page a screenshot should be taken of. imgPath (string): The path to which the screenshot should be saved.

**Returns:**

error: An error if any occurred during screenshot capturing or saving.

Example
package main

import (
	"log"

	"github.com/l50/goutils/v2/web"
	"github.com/l50/goutils/v2/web/cdpu"
)

func main() {
	site := web.Site{
		// initialize site
	}

	if err := cdpu.ScreenShot(site, "/path/to/save/image.png"); err != nil {
		log.Fatalf("failed to capture screenshot: %v", err)
	}
}
Output:

Types

type Driver

type Driver struct {
	Context context.Context
	Options *[]chromedp.ExecAllocatorOption
}

Driver is an interface to Google Chrome, containing a context.Context associated with this Driver and Options for the execution of Google Chrome.

**Attributes:**

Context: The context associated with this Driver. Options: The options for the execution of Google Chrome.

func (*Driver) GetContext

func (d *Driver) GetContext() context.Context

GetContext retrieves the context associated with the Driver instance.

**Returns:**

context.Context: The context associated with this Driver.

Example
package main

import (
	"log"

	"github.com/l50/goutils/v2/web/cdpu"
)

func main() {
	d := &cdpu.Driver{}
	ctx := d.GetContext()

	if ctx == nil {
		log.Fatalf("context is nil")
	}
}
Output:

func (*Driver) SetContext

func (d *Driver) SetContext(ctx context.Context)

SetContext associates a new context with the Driver instance.

**Parameters:**

ctx (context.Context): The new context to be associated with this Driver.

Example
package main

import (
	"context"
	"log"

	"github.com/l50/goutils/v2/web/cdpu"
)

func main() {
	d := &cdpu.Driver{}
	newCtx := context.Background()
	d.SetContext(newCtx)

	if d.GetContext() != newCtx {
		log.Fatalf("failed to set new context")
	}
}
Output:

type InputAction

type InputAction struct {
	Description string
	Selector    string
	Action      chromedp.Action
	Context     context.Context
}

InputAction represents selectors and actions to run with Chrome. It contains a description, a selector to find an element on the page, and a chromedp.Action which defines the action to perform on the selected element.

**Attributes:**

Description: A string that describes the action. Selector: The CSS selector of the element to perform the action on. Action: A chromedp.Action that defines what action to perform. Context: The context in which to execute the action.

Example
package main

import (
	"github.com/chromedp/chromedp"
	"github.com/l50/goutils/v2/web/cdpu"
)

func main() {
	action := cdpu.InputAction{
		Description: "Type in search box",
		Selector:    "#searchbox",
		Action:      chromedp.SendKeys("#searchbox", "example search"),
	}

	_ = action
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL