Skip to content

Commit

Permalink
feat: return error when session could not be created. Add session req…
Browse files Browse the repository at this point in the history
…uest timeout
  • Loading branch information
robertomier committed May 3, 2023
1 parent 41c966f commit 2df743a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
23 changes: 20 additions & 3 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ type serverReply struct {
Error
}

type StackItem struct {
FileName string `json:"fileName"`
ModuleVersion *string `json:"moduleVersion"`
ModuleName *string `json:"moduleName"`
NativeMethod bool `json:"nativeMethod"`
MethodName string `json:"methodName"`
ClassName string `json:"className"`
LineNumber int `json:"lineNumber"`
ClassLoaderName *string `json:"classLoaderName"`
}

// Error contains information about a failure of a command. See the table of
// these strings at https://www.w3.org/TR/webdriver/#handling-errors .
//
Expand All @@ -104,7 +115,7 @@ type Error struct {
// Message is a detailed, human-readable message specific to the failure.
Message string `json:"message"`
// Stacktrace may contain the server-side stacktrace where the error occurred.
Stacktrace string `json:"stacktrace"`
Stacktrace []StackItem `json:"stacktrace"`
// HTTPCode is the HTTP status code returned by the server.
HTTPCode int
// LegacyCode is the "Response Status Code" defined in the legacy Selenium
Expand Down Expand Up @@ -460,9 +471,11 @@ func (wd *remoteWD) NewSession() (string, error) {
}
return "", err
}

if reply.Status != 0 && i < len(attempts) {
continue
}

if reply.SessionID != nil {
wd.id = *reply.SessionID
}
Expand Down Expand Up @@ -495,10 +508,14 @@ func (wd *remoteWD) NewSession() (string, error) {
// Legacy implementations returned most data directly in the "values"
// key.
returnedCapabilities
}{}

Error
}{}
if err := json.Unmarshal(reply.Value, &value); err != nil {
return "", fmt.Errorf("error unmarshalling value: %v", err)
return "", fmt.Errorf("error unmarshalling value: %s", err)
}
if len(value.Err) > 0 {
return "", errors.New(value.Message)
}
if value.SessionID != "" && wd.id == "" {
wd.id = value.SessionID
Expand Down
14 changes: 13 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ func V4() ServiceOption {
}
}

// SessionRequestTimeout specifies the timeout it takes to discard a session request
func SessionRequestTimeout(seconds int) ServiceOption {
return func(s *Service) error {
s.sessionRequestTimeout = seconds
return nil
}
}

// HTMLUnit specifies the path to the JAR for the HTMLUnit driver (compiled
// with its dependencies).
//
Expand Down Expand Up @@ -166,7 +174,8 @@ type Service struct {

output io.Writer

v4 bool
v4 bool
sessionRequestTimeout int
}

// FrameBuffer returns the FrameBuffer if one was started by the service and nil otherwise.
Expand Down Expand Up @@ -201,6 +210,9 @@ func NewSeleniumService(jarPath string, port int, opts ...ServiceOption) (*Servi
s.cmd.Args = append(s.cmd.Args, "--ext", strings.Join(classpath, ":"))
}
s.cmd.Args = append(s.cmd.Args, "standalone", "--port", strconv.Itoa(port))
if s.sessionRequestTimeout > 0 {
s.cmd.Args = append(s.cmd.Args, "--session-request-timeout", strconv.Itoa(s.sessionRequestTimeout))
}
} else {
if s.geckoDriverPath != "" {
s.cmd.Args = append([]string{"java", "-Dwebdriver.gecko.driver=" + s.geckoDriverPath}, s.cmd.Args[1:]...)
Expand Down

0 comments on commit 2df743a

Please sign in to comment.