Skip to content

Commit

Permalink
- Manages AppiumDriver at test case level
Browse files Browse the repository at this point in the history
- Add ScrollIntoView with Direction parameter
- Drop supports on .net 4.7.2 (.net 4.8 is minimum)
  • Loading branch information
huaxing-yuan committed Apr 2, 2024
1 parent ba6d605 commit 5d75606
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<NoStandardLibraries>false</NoStandardLibraries>
<RootNamespace>AxaFrance.WebEngine.ExcelUI</RootNamespace>
<AssemblyName>AxaFrance.WebEngine.ExcelUI</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<DefineConstants>VSTO40</DefineConstants>
<TargetFrameworkProfile />
<IsWebBootstrapper>False</IsWebBootstrapper>
Expand Down Expand Up @@ -115,6 +115,7 @@
<Prefer32Bit>false</Prefer32Bit>
<CodeAnalysisRuleSet>
</CodeAnalysisRuleSet>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<!--
This section defines properties that are set when the "Release" configuration is selected.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions src/AxaFrance.WebEngine.ExcelUI/ThisAddIn.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 61 additions & 4 deletions src/AxaFrance.WebEngine.MobileApp/AppElementDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,77 @@ protected override IWebElement InternalFindElement()
}

/// <summary>
/// Tires to bring current element into viewport. If the element is still not visible after scroll action, the return value will be false.
/// Tries to bring current element into viewport.
/// This method will scroll down the page multiple times to make the element visible.
/// If after the maximum scroll action and the element is still not visible, the method will return false.
/// </summary>
/// <returns>True if the element is visible after scrolling. False when the element is not visible.</returns>
public bool ScrollIntoView()
public bool ScrollIntoView(int maxSwipe = 10)
{
int max = 10, count = 0;
while(!this.Exists(1) && count < max)
return ScrollIntoViewDown(maxSwipe);
}

/// <summary>
/// Tries to bring current element into viewport.
/// This method will scroll the page multiple times to make the element visible.
/// If after the maximum scroll action and the element is still not visible, the method will return false.
/// </summary>
/// <param name="direction">The direction of the scroll</param>
/// <param name="maxSwipe">The maximum number of swipe action to make the element visible</param>
/// <returns>True if the element is visible after scrolling. False when the element is not visible.</returns>
public bool ScrollIntoView(ScrollDirection direction, int maxSwipe = 10)
{
if (direction == ScrollDirection.Up)
{
return ScrollIntoViewUp(maxSwipe);
}
else
{
return ScrollIntoViewDown(maxSwipe);
}
}

private bool ScrollIntoViewDown(int maxSwipe)
{
int count = 0;
while (!this.Exists(1) && count < maxSwipe)
{
count++;
ScrollDown((int)(driver.Manage().Window.Size.Width * 0.5), (int)(driver.Manage().Window.Size.Height * 0.5));
}
return this.Exists(1);
}

private bool ScrollIntoViewUp(int maxSwipe)
{
int count = 0;
while (!this.Exists(1) && count < maxSwipe)
{
count++;
ScrollUp((int)(driver.Manage().Window.Size.Width * 0.5), (int)(driver.Manage().Window.Size.Height * 0.5));
}
return this.Exists(1);
}

private void ScrollUp(int x, int y)
{
int startY = (int)(driver.Manage().Window.Size.Height * 0.30);
int endY = (int)(driver.Manage().Window.Size.Height * 0.80);

var finger = new PointerInputDevice(PointerKind.Touch);
var actionSequence = new ActionSequence(finger, 0);

actionSequence.AddAction(finger.CreatePointerMove(CoordinateOrigin.Viewport, x, startY, TimeSpan.Zero));
actionSequence.AddAction(finger.CreatePointerDown(MouseButton.Touch));
actionSequence.AddAction(finger.CreatePointerMove(CoordinateOrigin.Viewport, x, endY, new TimeSpan(0, 0, 1)));
actionSequence.AddAction(finger.CreatePointerUp(MouseButton.Touch));


driver.PerformActions(new List<ActionSequence> { actionSequence });
}



private void ScrollDown(int x, int y)
{
int startY = (int)(driver.Manage().Window.Size.Height * 0.80);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net472;net48</TargetFrameworks>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
Expand All @@ -20,9 +20,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.2" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.7" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Selenium.WebDriver" Version="4.16.2" />
<PackageReference Include="Selenium.WebDriver" Version="4.19.0" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions src/AxaFrance.WebEngine.MobileApp/ScrollDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AxaFrance.WebEngine.MobileApp
{
/// <summary>
/// Defines the direction of the scroll.
/// </summary>
public enum ScrollDirection
{
/// <summary>
/// The scroll is in the up direction.
/// </summary>
Up,

/// <summary>
/// The scroll is in the down direction.
/// </summary>
Down
}
}
38 changes: 25 additions & 13 deletions src/AxaFrance.WebEngine.MobileApp/TestCaseApp.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2016-2022 AXA France IARD / AXA France VIE. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Modified By: YUAN Huaxing, at: 2022-5-13 18:26
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.iOS;
Expand All @@ -17,10 +18,12 @@ public class TestCaseApp : TestCase
/// <inheritdoc/>
public override string Cleanup()
{
try
{
var l = (Context as AppiumDriver).Manage().Logs;
foreach(var logType in l.AvailableLogTypes)
var l = (Context as AppiumDriver).Manage().Logs;
DebugLogger.WriteLine($"There are following available logs: {string.Join(", ", l.AvailableLogTypes)}");
foreach (var logType in l.AvailableLogTypes)
{
DebugLogger.WriteLine($"Log for {logType}");
try
{
StringBuilder sb = new StringBuilder();
var logs = l.GetLog(logType);
Expand All @@ -31,19 +34,16 @@ public override string Cleanup()
sb.AppendLine($"[{log.Level}] {log.Timestamp} {log.Message}");
}
}
DebugLogger.WriteLine($"Log for {logType}");
DebugLogger.WriteLine(sb.ToString());
}

}
catch (Exception ex)
{
DebugLogger.WriteLine("[ERROR] Get log error: " + ex.Message);
catch (Exception ex)
{
DebugLogger.WriteLine("[ERROR] Get log error: " + ex.Message);
}
}

try
{

if (Context is AppiumDriver appiumDriver && !string.IsNullOrEmpty(Settings.Instance.AppPackageName))
{
ResetApp(appiumDriver);
Expand All @@ -54,6 +54,18 @@ public override string Cleanup()
DebugLogger.WriteLine("[DEBUG] Current App closing error: " + ex.Message);
}


if (Context is AppiumDriver ad)
{
try
{
DebugLogger.WriteLine("Close the app");
ad.Close();
ad.Dispose();
}
catch { }
}

return string.Empty;

}
Expand All @@ -76,7 +88,7 @@ private void ResetApp(AppiumDriver appiumDriver)
/// <inheritdoc/>
public override void Initialize()
{
Context = TestSuiteApp.CurrentDriver;
Context = AppFactory.GetDriver(Settings.Instance.Platform);
startDate = DateTime.Now;
}

Expand Down
51 changes: 1 addition & 50 deletions src/AxaFrance.WebEngine.MobileApp/TestSuiteApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,6 @@ namespace AxaFrance.WebEngine.MobileApp
/// </summary>
public abstract class TestSuiteApp : TestSuite
{
static WebDriver driver;

/// <summary>
/// gets the currently using WebDriver (AndroidDriver or IOSDriver for example)
/// </summary>
public static WebDriver CurrentDriver
{
get
{
return driver;
}
}

/// <summary>
/// Cleanup method to close the uninstall the test application.
/// </summary>
/// <param name="s">Test settings (not used in this contexte)</param>
public override void CleanUp(Settings s)
{
if (driver is AndroidDriver androidDriver)
{
try
{
androidDriver.Close();
}
catch { }
try
{
androidDriver.RemoveApp(s.AppPackageName);
}
catch { }
androidDriver.Quit();
}
else if (driver is IOSDriver iosDriver)
{
try
{
iosDriver.Close();
}
catch { }
try
{
iosDriver.RemoveApp(s.AppPackageName);
}
catch { }
iosDriver.Quit();
}
}


/// <summary>
/// Initialize the connection to Selenium Grid compatible device Cloud and install the application package (if using browserstack)
Expand Down Expand Up @@ -92,7 +43,7 @@ public override void Initialize(Settings s)
DebugLogger.WriteLine($"Application Id privided by Mobile Lab service is: {id}");
}
}
driver = AppFactory.GetDriver(s.Platform);
//driver = AppFactory.GetDriver(s.Platform);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net472;net48;net6.0;net6.0-windows</TargetFrameworks>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<Nullable>disable</Nullable>
<PublishSingleFile>false</PublishSingleFile>
<SelfContained>false</SelfContained>
Expand Down
6 changes: 3 additions & 3 deletions src/AxaFrance.WebEngine.Web/AxaFrance.WebEngine.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net472;net48</TargetFrameworks>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
Expand Down Expand Up @@ -31,10 +31,10 @@


<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.2" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.7" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Selenium.WebDriver" Version="4.16.2" />
<PackageReference Include="Selenium.WebDriver" Version="4.19.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/AxaFrance.WebEngine/AxaFrance.WebEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net472;net48;net6.0-windows</TargetFrameworks>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
Expand Down
4 changes: 2 additions & 2 deletions src/Samples.LinearScripting/Samples.LinearScripting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.2" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Selenium.WebDriver" Version="4.16.2" />
<PackageReference Include="Selenium.WebDriver" Version="4.19.0" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions src/WebEngine.Test/UnitTests/MobileAppStep2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public override void DoAction(AppiumDriver driver)
{
throw new Exception("Test Failed");
}
if (model.ButtonFirst.ScrollIntoView(ScrollDirection.Up))
{
model.ButtonFirst.Click();
}
else
{
throw new Exception("Test Failed");
}
}

public override bool DoCheckpoint(AppiumDriver driver)
Expand All @@ -51,6 +59,11 @@ public class SecondPageModel : PageModel
AccessbilityId = "WebView"
};

public AppElementDescription ButtonFirst = new AppElementDescription
{
AccessbilityId = "Animation"
};

public WebElementDescription FormInput = new WebElementDescription
{
Name = "i_am_a_textbox"
Expand Down

0 comments on commit 5d75606

Please sign in to comment.