-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from rusq/i293
#293: browser compatibility
- Loading branch information
Showing
10 changed files
with
209 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Package pwcompat provides a compatibility layer, so when the playwright-go | ||
// team decides to break compatibility again, there's a place to write a | ||
// workaround. | ||
package pwcompat | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/playwright-community/playwright-go" | ||
) | ||
|
||
// Workaround for unexported driver dir in playwright. | ||
|
||
// newDriverFn is the function that creates a new driver. It is set to | ||
// playwright.NewDriver by default, but can be overridden for testing. | ||
var newDriverFn = playwright.NewDriver | ||
|
||
func getDefaultCacheDirectory() (string, error) { | ||
// pinched from playwright | ||
userHomeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", fmt.Errorf("could not get user home directory: %w", err) | ||
} | ||
switch runtime.GOOS { | ||
case "windows": | ||
return filepath.Join(userHomeDir, "AppData", "Local"), nil | ||
case "darwin": | ||
return filepath.Join(userHomeDir, "Library", "Caches"), nil | ||
case "linux": | ||
return filepath.Join(userHomeDir, ".cache"), nil | ||
} | ||
return "", errors.New("could not determine cache directory") | ||
} | ||
|
||
func NewDriver(runopts *playwright.RunOptions) (*playwright.PlaywrightDriver, error) { | ||
drv, err := newDriverFn(runopts) | ||
if err != nil { | ||
return nil, fmt.Errorf("error initialising driver: %w", err) | ||
} | ||
return drv, nil | ||
} | ||
|
||
// DriverDir returns the driver directory, broken in this commit: | ||
// https://github.com/playwright-community/playwright-go/pull/449/commits/372e209c776222f4681cf1b24a1379e3648dd982 | ||
func DriverDir(runopts *playwright.RunOptions) (string, error) { | ||
drv, err := NewDriver(runopts) | ||
if err != nil { | ||
return "", err | ||
} | ||
baseDriverDirectory, err := getDefaultCacheDirectory() | ||
if err != nil { | ||
return "", fmt.Errorf("it's just not your day: %w", err) | ||
} | ||
driverDirectory := filepath.Join(nvl(runopts.DriverDirectory, baseDriverDirectory), "ms-playwright-go", drv.Version) | ||
return driverDirectory, nil | ||
} | ||
|
||
func nvl(first string, rest ...string) string { | ||
if first != "" { | ||
return first | ||
} | ||
for _, s := range rest { | ||
if s != "" { | ||
return s | ||
} | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Package pwcompat provides a compatibility layer, so when the playwright-go | ||
// team decides to break compatibility again, there's a place to write a | ||
// workaround. | ||
// | ||
//go:build: darwin | ||
package pwcompat | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func bailonerr(t *testing.T, err error) { | ||
t.Helper() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func Test_getDefaultCacheDirectory(t *testing.T) { | ||
t.Parallel() | ||
home, err := os.UserHomeDir() | ||
bailonerr(t, err) | ||
tests := []struct { | ||
name string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
"darwin", | ||
filepath.Join(home, "Library", "Caches"), | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
got, err := getDefaultCacheDirectory() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("getDefaultCacheDirectory() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("getDefaultCacheDirectory() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Package pwcompat provides a compatibility layer, so when the playwright-go | ||
// team decides to break compatibility again, there's a place to write a | ||
// workaround. | ||
package pwcompat | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/playwright-community/playwright-go" | ||
) | ||
|
||
func TestNewDriver(t *testing.T) { | ||
t.Parallel() | ||
type args struct { | ||
runopts *playwright.RunOptions | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
"default dir", | ||
args{&playwright.RunOptions{ | ||
DriverDirectory: "", | ||
SkipInstallBrowsers: true, | ||
Browsers: []string{"chrome"}}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"custom dir", | ||
args{&playwright.RunOptions{ | ||
DriverDirectory: t.TempDir(), | ||
SkipInstallBrowsers: true, | ||
Browsers: []string{"chrome"}}, | ||
}, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
_, err := NewDriver(tt.args.runopts) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("NewDriver() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.