Skip to content

Commit

Permalink
Merge pull request #66 from wirepair/install_later_chromium_ci_test
Browse files Browse the repository at this point in the history
Fix gcd to handle PUT requests for JSON protocol
  • Loading branch information
wirepair committed Mar 28, 2023
2 parents 08cf63a + 144a736 commit d75c758
Show file tree
Hide file tree
Showing 34 changed files with 967 additions and 222 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/build_gcd_v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ jobs:
- name: Install Packages
run: |
sudo apt-get -qq update
sudo apt-get install -y build-essential chromium-browser
sudo apt-get install -y build-essential curl
echo -n "Determining latest build... "
LATEST=`curl -s http://commondatastorage.googleapis.com/chromium-browser-snapshots/Linux_x64/LAST_CHANGE`
echo "done!"
echo "http://commondatastorage.googleapis.com/chromium-browser-snapshots/Linux_x64/$LATEST/chrome-linux.zip"
echo -n "Downloading build for $LATEST... "
curl -L http://commondatastorage.googleapis.com/chromium-browser-snapshots/Linux_x64/$LATEST/chrome-linux.zip -o chrome-linux.zip
echo "done!"
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: |
cd v2 && go test -v -race ./...
cd v2 && go test -v -race -p 1 ./...
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Changelog (2023)
- 2.3.0 (Mar 28) Updated JSON endpoint calls to use PUT instead of GET as GET returns an error since 111.
- Updates to chrome 111.0.5563.147 protocol
- gcdapigen updated to handle different revisions thanks to @eatdrinksleepcode

# Changelog (2022)
- 2.2.6 (Sept 5th) Fixes a bug where new types that were just arrays to another ref type were not being output correctly
- Updates to chrome 105.0.5195.102
Expand Down
1 change: 1 addition & 0 deletions gcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func (c *Gcd) getConnectableTargets() ([]*TargetInfo, error) {
targets := make([]*TargetInfo, 0)
err = json.Unmarshal(body, &targets)
if err != nil {
fmt.Printf("%s\n", string(body))
return nil, &GcdDecodingErr{Message: err.Error()}
}

Expand Down
2 changes: 1 addition & 1 deletion gcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
func init() {
switch runtime.GOOS {
case "windows":
flag.StringVar(&testPath, "chrome", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "path to chrome")
flag.StringVar(&testPath, "chrome", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", "path to chrome")
flag.StringVar(&testDir, "dir", "C:\\temp\\gcd\\", "user directory")
case "darwin":
flag.StringVar(&testPath, "chrome", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "path to chrome")
Expand Down
46 changes: 25 additions & 21 deletions v2/gcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (

var json = jsoniter.ConfigCompatibleWithStandardLibrary

var GCDVERSION = "v2.2.6"
var GCDVERSION = "v2.3.0"

var (
ErrNoTabAvailable = errors.New("no available tab found")
Expand Down Expand Up @@ -374,7 +374,12 @@ func (c *Gcd) GetNewTargets(knownIds map[string]struct{}) ([]*ChromeTarget, erro
func (c *Gcd) getConnectableTargets() ([]*TargetInfo, error) {
// some times it takes a while to get results, so retry 4x
for i := 0; i < 4; i++ {
resp, err := http.Get(c.apiEndpoint)
req, err := http.NewRequest("PUT", c.apiEndpoint, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -408,7 +413,12 @@ func (c *Gcd) getConnectableTargets() ([]*TargetInfo, error) {

// NewTab a new empty tab, returns the chrome target.
func (c *Gcd) NewTab() (*ChromeTarget, error) {
resp, err := http.Get(c.apiEndpoint + "/new")
req, err := http.NewRequest("PUT", c.apiEndpoint+"/new", nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -427,30 +437,19 @@ func (c *Gcd) NewTab() (*ChromeTarget, error) {
return openChromeTarget(c, tabTarget, c.messageObserver)
}

// GetFirstTab returns the first tab created, to be called when
// first started, otherwise you will get a random tab returned.
func (c *Gcd) GetFirstTab() (*ChromeTarget, error) {
connectableTargets, err := c.getConnectableTargets()
if err != nil {
return nil, err
}

for _, tabTarget := range connectableTargets {
if tabTarget.Type == "page" {
return openChromeTarget(c, tabTarget, c.messageObserver)
}
}
return nil, ErrNoTabAvailable
}

// GetRevision of chrome
func (c *Gcd) GetRevision() string {
return gcdapi.CHROME_VERSION
}

// CloseTab closes the target tab.
func (c *Gcd) CloseTab(target *ChromeTarget) error {
resp, err := http.Get(fmt.Sprintf("%s/close/%s", c.apiEndpoint, target.Target.Id))
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/close/%s", c.apiEndpoint, target.Target.Id), nil)
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
Expand All @@ -461,7 +460,12 @@ func (c *Gcd) CloseTab(target *ChromeTarget) error {

// ActivateTab (focus) the tab.
func (c *Gcd) ActivateTab(target *ChromeTarget) error {
resp, err := http.Get(fmt.Sprintf("%s/activate/%s", c.apiEndpoint, target.Target.Id))
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/activate/%s", c.apiEndpoint, target.Target.Id), nil)
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
Expand Down
94 changes: 59 additions & 35 deletions v2/gcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,25 @@ func TestGetPages(t *testing.T) {
testDefaultStartup(t)
defer debugger.ExitProcess()

newTab, err := debugger.NewTab()
if err != nil {
t.Fatalf("error creating tab: %s\n", err)
}

targets, err := debugger.GetTargets()
if err != nil {
t.Fatalf("error getting targets: %s\n", err)
}

if len(targets) <= 0 {
t.Fatalf("invalid number of targets, got: %d\n", len(targets))
}

err = debugger.CloseTab(newTab)
if err != nil {
t.Fatalf("error CloseTab: %s\n", err)
}

t.Logf("page: %s\n", targets[0].Target.Url)
}

Expand Down Expand Up @@ -132,11 +144,11 @@ func TestGoRoutineCount(t *testing.T) {
}

func TestProcessKilled(t *testing.T) {
doneCh := make(chan struct{})
doneCh := make(chan error)

terminatedHandler := func(reason string) {
t.Logf("reason: %s\n", reason)
doneCh <- struct{}{}
doneCh <- nil
}

testDefaultStartup(t, WithTerminationHandler(terminatedHandler))
Expand All @@ -160,8 +172,8 @@ func TestTargetCrashed(t *testing.T) {
testDefaultStartup(t)
defer debugger.ExitProcess()

doneCh := make(chan struct{})
go testTimeoutListener(t, doneCh, 5, "timed out waiting for crashed to be handled")
doneCh := make(chan error)
go testTimeoutListener(doneCh, 10, "timed out waiting for crashed to be handled")

targetCrashedFn := func(targ *ChromeTarget, payload []byte) {
t.Logf("reason: %s\n", string(payload))
Expand All @@ -178,7 +190,11 @@ func TestTargetCrashed(t *testing.T) {

navParams := &gcdapi.PageNavigateParams{Url: "chrome://crash", TransitionType: "typed"}
tab.Page.NavigateWithParams(testCtx, navParams)
<-doneCh

err = <-doneCh
if err != nil {
t.Fatal(err)
}
}

func TestEvents(t *testing.T) {
Expand All @@ -191,7 +207,7 @@ func TestEvents(t *testing.T) {
}
console := target.Console

doneCh := make(chan struct{}, 1)
doneCh := make(chan error, 1)
target.Subscribe("Console.messageAdded", func(target *ChromeTarget, v []byte) {
target.Unsubscribe("Console.messageAdded")
msg := &gcdapi.ConsoleMessageAddedEvent{}
Expand All @@ -215,9 +231,12 @@ func TestEvents(t *testing.T) {
t.Fatalf("error attempting to navigate: %s\n", err)
}

go testTimeoutListener(t, doneCh, 5, "console message")
go testTimeoutListener(doneCh, 5, "console message")

<-doneCh
err = <-doneCh
if err != nil {
t.Fatal(err)
}
}

func TestEvaluate(t *testing.T) {
Expand Down Expand Up @@ -327,7 +346,7 @@ func TestDOMEnableWithWhiteSpace(t *testing.T) {
t.Fatalf("got error enabling DOM: %s\n", err)
}

doneCh := make(chan struct{})
doneCh := make(chan error)
target.Subscribe("Page.loadEventFired", func(target *ChromeTarget, payload []byte) {

doc, err := target.DOM.GetDocument(context.Background(), -1, true)
Expand All @@ -338,7 +357,7 @@ func TestDOMEnableWithWhiteSpace(t *testing.T) {
if doc.ChildNodeCount != 2 {
t.Fatalf("failed to get 2 child nodes, got %d\n", doc.ChildNodeCount)
}
doneCh <- struct{}{}
doneCh <- nil
})

navParams := &gcdapi.PageNavigateParams{Url: testServerAddr + "cookie.html", TransitionType: "typed"}
Expand Down Expand Up @@ -408,8 +427,8 @@ func TestComplexReturn(t *testing.T) {
testDefaultStartup(t)
defer debugger.ExitProcess()

doneCh := make(chan struct{}, 1)
go testTimeoutListener(t, doneCh, 7, "waiting for page load to get cookies")
doneCh := make(chan error, 1)
go testTimeoutListener(doneCh, 7, "waiting for page load to get cookies")
target, err := debugger.NewTab()

if err != nil {
Expand Down Expand Up @@ -450,16 +469,20 @@ func TestComplexReturn(t *testing.T) {
}

t.Logf("waiting for loadEventFired")
<-doneCh
err = <-doneCh
if err != nil {
t.Fatal(err)
}
}

func TestConnectToInstance(t *testing.T) {
testDefaultStartup(t)
defer debugger.ExitProcess()

doneCh := make(chan struct{})
doneCh := make(chan error)

go testTimeoutListener(doneCh, 15, "timed out waiting for remote connection")

go testTimeoutListener(t, doneCh, 15, "timed out waiting for remote connection")
go func() {
remoteDebugger := NewChromeDebugger()
remoteDebugger.ConnectToInstance(debugger.host, debugger.port)
Expand All @@ -481,7 +504,11 @@ func TestConnectToInstance(t *testing.T) {
}
close(doneCh)
}()
<-doneCh

err := <-doneCh
if err != nil {
t.Fatal(err)
}
}

func TestLocalExtension(t *testing.T) {
Expand All @@ -492,7 +519,7 @@ func TestLocalExtension(t *testing.T) {

defer debugger.ExitProcess()

doneCh := make(chan struct{})
doneCh := make(chan error)

target, err := debugger.NewTab()
if err != nil {
Expand All @@ -518,8 +545,11 @@ func TestLocalExtension(t *testing.T) {
t.Fatalf("error navigating: %s\n", err)
}

go testTimeoutListener(t, doneCh, 15, "timed out waiting for remote connection")
<-doneCh
go testTimeoutListener(doneCh, 15, "timed out waiting for remote connection")
err = <-doneCh
if err != nil {
t.Fatal(err)
}
}

func TestContextCancel(t *testing.T) {
Expand All @@ -529,7 +559,7 @@ func TestContextCancel(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()

target, err := debugger.GetFirstTab()
target, err := debugger.NewTab()
if err != nil {
t.Fatalf("error getting first tab")
}
Expand All @@ -548,14 +578,14 @@ func TestNetworkIntercept(t *testing.T) {
testDefaultStartup(t, WithEventDebugging(), WithInternalDebugMessages(), WithLogger(&DebugLogger{}))
defer debugger.ExitProcess()

doneCh := make(chan struct{})
doneCh := make(chan error)

target, err := debugger.NewTab()
if err != nil {
t.Fatalf("error getting new tab: %s\n", err)
}

go testTimeoutListener(t, doneCh, 5, "timed out waiting for requestIntercepted")
go testTimeoutListener(doneCh, 5, "timed out waiting for requestIntercepted")
ctx := context.Background()
if _, err := target.Fetch.Enable(ctx, []*gcdapi.FetchRequestPattern{
{
Expand Down Expand Up @@ -602,22 +632,16 @@ func TestNetworkIntercept(t *testing.T) {
t.Fatalf("error navigating: %s\n", err)
}

<-doneCh
}

func TestGetFirstTab(t *testing.T) {
testDefaultStartup(t)
defer debugger.ExitProcess()
_, err := debugger.GetFirstTab()
err = <-doneCh
if err != nil {
t.Fatalf("error getting first tab: %v\n", err)
t.Fatal(err)
}
}

func TestCloseTab(t *testing.T) {
testDefaultStartup(t)
defer debugger.ExitProcess()
target, err := debugger.GetFirstTab()
target, err := debugger.NewTab()
if err != nil {
t.Fatalf("error getting first tab: %v\n", err)
}
Expand All @@ -638,9 +662,9 @@ func TestCustomLogger(t *testing.T) {
customLogger := &testLogger{}
testDefaultStartup(t, WithLogger(customLogger), WithEventDebugging())
defer debugger.ExitProcess()
tab, err := debugger.GetFirstTab()
tab, err := debugger.NewTab()
if err != nil {
t.Fatalf("error getting first tab: %v\n", err)
t.Fatalf("error getting new tab: %v\n", err)
}

if _, err = tab.Page.Enable(context.TODO()); err != nil {
Expand Down Expand Up @@ -683,16 +707,16 @@ func testServer() {
go http.Serve(testListener, http.FileServer(http.Dir("testdata/")))
}

func testTimeoutListener(t *testing.T, closeCh chan struct{}, seconds time.Duration, message string) {
func testTimeoutListener(closeCh chan error, seconds time.Duration, message string) {
timeout := time.NewTimer(seconds * time.Second)
for {
select {
case <-closeCh:
timeout.Stop()
return
case <-timeout.C:
closeCh <- fmt.Errorf("timed out waiting %s", message)
close(closeCh)
t.Fatalf("timed out waiting for %s", message)
return
}
}
Expand Down
Loading

0 comments on commit d75c758

Please sign in to comment.