Retrieve the session ID in *agouti.webdriver for Golang

679 Views Asked by At

I want to use the session id while creating a new WebDriver with Agouti to pass it to SauceLabs for status update.

Commands Used:

url := fmt.Sprintf("http://%s:%[email protected]/wd/hub", username, accesskey)
page,err :=agouti.NewPage(url, options)
Expect(err).NotTo(HaveOccurred())
page.Navigate(`https://qiita.com/login`)

I tried to retrieve the session ID from page.Session() but the return type is a Bus Interface and result is Session with the *http.client variable.

Is there any other alternative to it?, to just retrieve the session id.

2

There are 2 best solutions below

0
On

The page.Session().Bus returns a type *apiSession to extract the session ID. Use of Indirect can help us return the value that *apiSession points to in this case page.Session().Bus from there we can extract the sessionID.

sessionBus := reflect.ValueOf(page.Session().Bus)
sessionURL := reflect.Indirect(sessionBus)
sessionField := sessionURL.FieldByName(`SessionURL`)
sessionString := sessionField.String()
sessionSplit := strings.SplitN(sessionString, "/", 7)
sessionID := sessionSplit[len(sessionSplit)-1]
11
On

fmt.Println(fmt.Sprintf("%s", sessionId)[:32]) thanks to Gavin!

Good luck!