Commits

Miki Tebeka committed 3a286d8

DecodeElement and "raw" ExecuteScript (nshah)

  • Participants
  • Parent commits a30052e

Comments (0)

Files changed (2)

 	"fmt"
 	"io/ioutil"
 	"net/http"
+	"net/url"
 	"strings"
 	"time"
 )
 	return false
 }
 
+func normalizeURL(n string, base string) (string, error) {
+	baseURL, err := url.Parse(base)
+	if err != nil {
+		return "", fmt.Errorf(
+			"Failed to parse base URL %s with error %s", base, err)
+	}
+	nURL, err := baseURL.Parse(n)
+	if err != nil {
+		return "", fmt.Errorf("Failed to parse new URL %s with error %s", n, err)
+	}
+	return nURL.String(), nil
+}
+
 func (wd *remoteWD) requestURL(template string, args ...interface{}) string {
 	path := fmt.Sprintf(template, args...)
 	return wd.executor + path
 
 	// http.Client don't follow POST redirects ....
 	if (method == "POST") && isRedirect(response) {
-		url := response.Header["Location"][0]
+		url, err := normalizeURL(response.Header["Location"][0], url)
+		if err != nil {
+			return nil, err
+		}
 		request, _ = newRequest("GET", url, nil)
 		response, err = http.DefaultClient.Do(request)
 		if err != nil {
 	return wd.execute("POST", url, data)
 }
 
-func decodeElement(wd *remoteWD, data []byte) (WebElement, error) {
+func (wd *remoteWD) DecodeElement(data []byte) (WebElement, error) {
 	reply := new(elementReply)
 	err := json.Unmarshal(data, reply)
 	if err != nil {
 		return nil, err
 	}
 
-	return decodeElement(wd, response)
+	return wd.DecodeElement(response)
 }
 
-func decodeElements(wd *remoteWD, data []byte) ([]WebElement, error) {
+func (wd *remoteWD) DecodeElements(data []byte) ([]WebElement, error) {
 	reply := new(elementsReply)
 	err := json.Unmarshal(data, reply)
 	if err != nil {
 		return nil, err
 	}
 
-	return decodeElements(wd, response)
+	return wd.DecodeElements(response)
 }
 
 func (wd *remoteWD) Close() error {
 		return nil, err
 	}
 
-	return decodeElement(wd, response)
+	return wd.DecodeElement(response)
 }
 
 func (wd *remoteWD) GetCookies() ([]Cookie, error) {
 	return wd.voidCommand("/session/%s/alert_text", data)
 }
 
-func (wd *remoteWD) execScript(script string, args []interface{}, suffix string) (interface{}, error) {
+func (wd *remoteWD) execScriptRaw(script string, args []interface{}, suffix string) ([]byte, error) {
 	params := map[string]interface{}{
 		"script": script,
 		"args":   args,
 
 	template := "/session/%s/execute" + suffix
 	url := wd.requestURL(template, wd.id)
-	response, err := wd.execute("POST", url, data)
+	return wd.execute("POST", url, data)
+}
+
+func (wd *remoteWD) execScript(script string, args []interface{}, suffix string) (interface{}, error) {
+	response, err := wd.execScriptRaw(script, args, suffix)
 	if err != nil {
 		return nil, err
 	}
 	return wd.execScript(script, args, "_async")
 }
 
+func (wd *remoteWD) ExecuteScriptRaw(script string, args []interface{}) ([]byte, error) {
+	return wd.execScriptRaw(script, args, "")
+}
+
+func (wd *remoteWD) ExecuteScriptAsyncRaw(script string, args []interface{}) ([]byte, error) {
+	return wd.execScriptRaw(script, args, "_async")
+}
+
 func (wd *remoteWD) Screenshot() ([]byte, error) {
 	data, err := wd.stringCommand("/session/%s/screenshot")
 	if err != nil {
 		return nil, err
 	}
 
-	return decodeElement(elem.parent, response)
+	return elem.parent.DecodeElement(response)
 }
 
 func (elem *remoteWE) FindElements(by, value string) ([]WebElement, error) {
 		return nil, err
 	}
 
-	return decodeElements(elem.parent, response)
+	return elem.parent.DecodeElements(response)
 }
 
 func (elem *remoteWE) boolQuery(urlTemplate string) (bool, error) {
 )
 
 const (
-	VERSION = "0.5.0" // Driver version
+	VERSION = "0.6.0" // Driver version
 )
 
 /* Element finding options */
 	/* Current active element. */
 	ActiveElement() (WebElement, error)
 
+	// Decoding element(s)
+	/* Decode a single element response. */
+	DecodeElement([]byte) (WebElement, error)
+	/* Decode a multi element response. */
+	DecodeElements([]byte) ([]WebElement, error)
+
 	// Cookies
 	/* Get all cookies */
 	GetCookies() ([]Cookie, error)
 	ExecuteScript(script string, args []interface{}) (interface{}, error)
 	/* Execute a script async. */
 	ExecuteScriptAsync(script string, args []interface{}) (interface{}, error)
+
+	/* Execute a script but don't JSON decode. */
+	ExecuteScriptRaw(script string, args []interface{}) ([]byte, error)
+	/* Execute a script async but don't JSON decode. */
+	ExecuteScriptAsyncRaw(script string, args []interface{}) ([]byte, error)
 }
 
 type WebElement interface {