Skip to content

Commit 9894e0f

Browse files
authored
[Testing] Update Appium to v8 (#29774)
* Update Appium to the latest version * Removed deprecated logic * Update comment
1 parent 8e5a173 commit 9894e0f

File tree

11 files changed

+85
-20
lines changed

11 files changed

+85
-20
lines changed

src/Controls/tests/TestCases.Android.Tests/Controls.TestCases.Android.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Appium.WebDriver" Version="7.0.0" />
13+
<PackageReference Include="Appium.WebDriver" Version="8.0.0" />
1414
<PackageReference Include="NUnit" Version="4.0.1" />
1515
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1616
</ItemGroup>

src/Controls/tests/TestCases.Mac.Tests/Controls.TestCases.Mac.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Appium.WebDriver" Version="7.0.0" />
13+
<PackageReference Include="Appium.WebDriver" Version="8.0.0" />
1414
<PackageReference Include="NUnit" Version="4.0.1" />
1515
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1616
</ItemGroup>

src/Controls/tests/TestCases.Shared.Tests/UITest.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,12 @@ but both can happen.
222222
{
223223
case TestDevice.Android:
224224
environmentName = "android";
225-
var deviceApiLevel = (long)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceApiLevel");
226-
var deviceScreenSize = (string)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceScreenSize");
227-
var deviceScreenDensity = (long)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceScreenDensity");
225+
var deviceApiLevel = (long?)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceApiLevel")
226+
?? throw new InvalidOperationException("deviceApiLevel capability is missing or null.");
227+
var deviceScreenSize = (string?)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceScreenSize")
228+
?? throw new InvalidOperationException("deviceScreenSize capability is missing or null.");
229+
var deviceScreenDensity = (long?)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceScreenDensity")
230+
?? throw new InvalidOperationException("deviceScreenDensity capability is missing or null.");
228231

229232
if (!(deviceApiLevel == 30 && deviceScreenSize == "1080x1920" && deviceScreenDensity == 420))
230233
{
@@ -233,8 +236,10 @@ but both can happen.
233236
break;
234237

235238
case TestDevice.iOS:
236-
var platformVersion = (string)((AppiumApp)App).Driver.Capabilities.GetCapability("platformVersion");
237-
var device = (string)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceName");
239+
var platformVersion = (string?)((AppiumApp)App).Driver.Capabilities.GetCapability("platformVersion")
240+
?? throw new InvalidOperationException("platformVersion capability is missing or null.");
241+
var device = (string?)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceName")
242+
?? throw new InvalidOperationException("deviceName capability is missing or null.");
238243

239244
if (device.Contains(" Xs", StringComparison.OrdinalIgnoreCase) && platformVersion == "18.0")
240245
{

src/Controls/tests/TestCases.WinUI.Tests/Controls.TestCases.WinUI.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Appium.WebDriver" Version="7.0.0" />
13+
<PackageReference Include="Appium.WebDriver" Version="8.0.0" />
1414
<PackageReference Include="NUnit" Version="4.0.1" />
1515
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1616
</ItemGroup>

src/Controls/tests/TestCases.iOS.Tests/Controls.TestCases.iOS.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Appium.WebDriver" Version="7.0.0" />
13+
<PackageReference Include="Appium.WebDriver" Version="8.0.0" />
1414
<PackageReference Include="NUnit" Version="4.0.1" />
1515
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1616
</ItemGroup>

src/TestUtils/src/UITest.Appium/Actions/AppiumAndroidContextMenuActions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ protected CommandResponse ActivateContextMenu(IDictionary<string, object> parame
4848
{
4949
var appiumElement = GetAppiumElement(element);
5050

51+
if (appiumElement == null)
52+
{
53+
return CommandResponse.FailedEmptyResponse;
54+
}
55+
5156
OpenQA.Selenium.Appium.Interactions.PointerInputDevice touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
5257
var longPress = new ActionSequence(touchDevice, 0);
5358

src/TestUtils/src/UITest.Appium/Actions/AppiumAndroidSpecificActions.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,17 @@ CommandResponse ToggleAirplaneMode(IDictionary<string, object> parameters)
6161
if (_appiumApp.Driver is AndroidDriver androidDriver)
6262
{
6363
// Toggle airplane mode on device.
64-
androidDriver.ToggleAirplaneMode();
64+
var currentConnectivity = androidDriver.ExecuteScript("mobile:getConnectivity") as IDictionary<string, object>;
6565

66-
return CommandResponse.SuccessEmptyResponse;
66+
if (currentConnectivity is not null && currentConnectivity.TryGetValue("airplaneMode", out var currentState))
67+
{
68+
bool newState = !(bool)currentState;
69+
70+
var connectivityParams = new Dictionary<string, object> { { "airplaneMode", newState } };
71+
androidDriver.ExecuteScript("mobile:setConnectivity", connectivityParams);
72+
73+
return CommandResponse.SuccessEmptyResponse;
74+
}
6775
}
6876

6977
return CommandResponse.FailedEmptyResponse;
@@ -73,9 +81,18 @@ CommandResponse ToggleData(IDictionary<string, object> parameters)
7381
{
7482
if (_appiumApp.Driver is AndroidDriver androidDriver)
7583
{
76-
androidDriver.ToggleData();
84+
// Toggle device data.
85+
var currentConnectivity = androidDriver.ExecuteScript("mobile:getConnectivity") as IDictionary<string, object>;
7786

78-
return CommandResponse.SuccessEmptyResponse;
87+
if (currentConnectivity is not null && currentConnectivity.TryGetValue("data", out var currentState))
88+
{
89+
bool newState = !(bool)currentState;
90+
91+
var connectivityParams = new Dictionary<string, object> { { "data", newState } };
92+
androidDriver.ExecuteScript("mobile:setConnectivity", connectivityParams);
93+
94+
return CommandResponse.SuccessEmptyResponse;
95+
}
7996
}
8097

8198
return CommandResponse.FailedEmptyResponse;
@@ -85,10 +102,18 @@ CommandResponse ToggleWifi(IDictionary<string, object> parameters)
85102
{
86103
if (_appiumApp.Driver is AndroidDriver androidDriver)
87104
{
88-
// Switch the state of the wifi service
89-
androidDriver.ToggleWifi();
105+
// Switch the state of the WiFi service
106+
var currentConnectivity = androidDriver.ExecuteScript("mobile:getConnectivity") as IDictionary<string, object>;
90107

91-
return CommandResponse.SuccessEmptyResponse;
108+
if (currentConnectivity is not null && currentConnectivity.TryGetValue("wifi", out var currentState))
109+
{
110+
bool newState = !(bool)currentState;
111+
112+
var connectivityParams = new Dictionary<string, object> { { "wifi", newState } };
113+
androidDriver.ExecuteScript("mobile:setConnectivity", connectivityParams);
114+
115+
return CommandResponse.SuccessEmptyResponse;
116+
}
92117
}
93118

94119
return CommandResponse.FailedEmptyResponse;

src/TestUtils/src/UITest.Appium/Actions/AppiumAndroidVirtualKeyboardActions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ public AppiumAndroidVirtualKeyboardActions(AppiumApp app)
1212

1313
protected override CommandResponse DismissKeyboard(IDictionary<string, object> parameters)
1414
{
15-
if (_app.Driver.IsKeyboardShown())
15+
try
1616
{
1717
_app.Driver.HideKeyboard();
18+
19+
return CommandResponse.SuccessEmptyResponse;
20+
}
21+
catch
22+
{
23+
return CommandResponse.FailedEmptyResponse;
1824
}
19-
return CommandResponse.SuccessEmptyResponse;
2025
}
2126

2227
protected override CommandResponse PressEnter(IDictionary<string, object> parameters)

src/TestUtils/src/UITest.Appium/Actions/AppiumMouseActions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ CommandResponse DoubleClick(IDictionary<string, object> parameters)
155155
{
156156
var element = GetAppiumElement(parameters["element"]);
157157

158+
if (element == null)
159+
{
160+
return CommandResponse.FailedEmptyResponse;
161+
}
162+
158163
OpenQA.Selenium.Appium.Interactions.PointerInputDevice touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Mouse);
159164
var sequence = new ActionSequence(touchDevice, 0);
160165
sequence.AddAction(touchDevice.CreatePointerMove(element, 0, 0, TimeSpan.FromMilliseconds(5)));
@@ -189,6 +194,11 @@ CommandResponse LongPress(IDictionary<string, object> parameters)
189194
{
190195
var element = GetAppiumElement(parameters["element"]);
191196

197+
if (element == null)
198+
{
199+
return CommandResponse.FailedEmptyResponse;
200+
}
201+
192202
OpenQA.Selenium.Appium.Interactions.PointerInputDevice touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Mouse);
193203
var longPress = new ActionSequence(touchDevice, 0);
194204

src/TestUtils/src/UITest.Appium/Actions/AppiumTouchActions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ CommandResponse PressDown(IDictionary<string, object> parameters)
140140
{
141141
var element = GetAppiumElement(parameters["element"]);
142142

143+
if (element == null)
144+
{
145+
return CommandResponse.FailedEmptyResponse;
146+
}
147+
143148
// Currently only pen and touch pointer input source types are supported, but this works fine
144149
OpenQA.Selenium.Appium.Interactions.PointerInputDevice touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
145150

@@ -178,6 +183,11 @@ CommandResponse DoubleTap(IDictionary<string, object> parameters)
178183
{
179184
var element = GetAppiumElement(parameters["element"]);
180185

186+
if (element == null)
187+
{
188+
return CommandResponse.FailedEmptyResponse;
189+
}
190+
181191
OpenQA.Selenium.Appium.Interactions.PointerInputDevice touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
182192
var sequence = new ActionSequence(touchDevice, 0);
183193
sequence.AddAction(touchDevice.CreatePointerMove(element, 0, 0, TimeSpan.FromMilliseconds(5)));
@@ -211,7 +221,12 @@ CommandResponse DoubleTapCoordinates(float x, float y)
211221
CommandResponse TouchAndHold(IDictionary<string, object> parameters)
212222
{
213223
var element = GetAppiumElement(parameters["element"]);
214-
224+
225+
if (element == null)
226+
{
227+
return CommandResponse.FailedEmptyResponse;
228+
}
229+
215230
OpenQA.Selenium.Appium.Interactions.PointerInputDevice touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
216231
var longPress = new ActionSequence(touchDevice, 0);
217232

0 commit comments

Comments
 (0)