Repository: aerokube/selenoid
Pull request #1305 (2023-02-03 17:21:12)

Use the appium:deviceName capability to find the specific browser
Use the `appium:deviceName` capability to find the specific browser as well. New Appium clients do not send the non-W3C `deviceName` capability.


Patch:
diff --git a/session/session.go b/session/session.go
index 2a18bc5..3b73062 100644
--- a/session/session.go
+++ b/session/session.go
@@ -16,6 +16,7 @@ type Caps struct {
 	W3CVersion            string            `json:"browserVersion,omitempty"`
 	Platform              string            `json:"platform,omitempty"`
 	W3CPlatform           string            `json:"platformName,omitempty"`
+	W3CDeviceName         string            `json:"appium:deviceName,omitempty"`
 	ScreenResolution      string            `json:"screenResolution,omitempty"`
 	Skin                  string            `json:"skin,omitempty"`
 	VNC                   bool              `json:"enableVNC,omitempty"`
@@ -47,6 +48,9 @@ func (c *Caps) ProcessExtensionCapabilities() {
 	if c.W3CPlatform != "" {
 		c.Platform = c.W3CPlatform
 	}
+	if c.W3CDeviceName != "" {
+		c.DeviceName = c.W3CDeviceName
+	}
 
 	if c.ExtensionCapabilities != nil {
 		mergo.Merge(c, *c.ExtensionCapabilities, mergo.WithOverride) //We probably need to handle returned error
@@ -58,7 +62,10 @@ func (c *Caps) BrowserName() string {
 	if browserName != "" {
 		return browserName
 	}
-	return c.DeviceName
+	if c.DeviceName != "" {
+		return c.DeviceName
+	}
+	return c.W3CDeviceName
 }
 
 // Container - container information


Test patch:
diff --git a/utils_test.go b/utils_test.go
index 160afdd..23d5682 100644
--- a/utils_test.go
+++ b/utils_test.go
@@ -188,6 +188,7 @@ func TestProcessExtensionCapabilities(t *testing.T) {
 	capsJson := `{
 		"version": "57.0",
 		"browserName": "firefox",
+		"appium:deviceName": "android",
 		"selenoid:options": {
 			"name": "ExampleTestName",
 			"enableVNC": true,
@@ -206,6 +207,7 @@ func TestProcessExtensionCapabilities(t *testing.T) {
 	caps.ProcessExtensionCapabilities()
 	AssertThat(t, caps.Name, EqualTo{"firefox"})
 	AssertThat(t, caps.Version, EqualTo{"57.0"})
+	AssertThat(t, caps.DeviceName, EqualTo{"android"})
 	AssertThat(t, caps.TestName, EqualTo{"ExampleTestName"})
 	AssertThat(t, caps.VNC, EqualTo{true})
 	AssertThat(t, caps.VideoFrameRate, EqualTo{uint16(24)})
@@ -250,3 +252,30 @@ func TestSumUsedTotalGreaterThanPending(t *testing.T) {
 	AssertThat(t, queue.Pending(), EqualTo{0})
 	AssertThat(t, queue.Used(), EqualTo{2})
 }
+
+func TestBrowserName(t *testing.T) {
+	var caps session.Caps
+
+	var capsJson = `{
+		"appium:deviceName": "iPhone 7"
+	}`
+	err := json.Unmarshal([]byte(capsJson), &caps)
+	AssertThat(t, err, Is{nil})
+	AssertThat(t, caps.BrowserName(), EqualTo{"iPhone 7"})
+
+	capsJson = `{
+		"deviceName": "android 11"
+	}`
+	err = json.Unmarshal([]byte(capsJson), &caps)
+	AssertThat(t, err, Is{nil})
+	AssertThat(t, caps.BrowserName(), EqualTo{"android 11"})
+
+	capsJson = `{
+		"deviceName": "android 11",
+		"appium:deviceName": "iPhone 7",
+		"browserName": "firefox"
+	}`
+	err = json.Unmarshal([]byte(capsJson), &caps)
+	AssertThat(t, err, Is{nil})
+	AssertThat(t, caps.BrowserName(), EqualTo{"firefox"})
+}