/
AppiumApp.cs
137 lines (114 loc) · 4.43 KB
/
AppiumApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;
using UITest.Core;
namespace UITest.Appium
{
public abstract class AppiumApp : IApp, IScreenshotSupportedApp, ILogsSupportedApp
{
protected readonly AppiumDriver _driver;
protected readonly IConfig _config;
protected readonly AppiumCommandExecutor _commandExecutor;
public AppiumApp(AppiumDriver driver, IConfig config)
{
_driver = driver ?? throw new ArgumentNullException(nameof(driver));
_config = config ?? throw new ArgumentNullException(nameof(config));
_commandExecutor = new AppiumCommandExecutor();
_commandExecutor.AddCommandGroup(new AppiumPointerActions(this));
_commandExecutor.AddCommandGroup(new AppiumTextActions());
_commandExecutor.AddCommandGroup(new AppiumGeneralActions());
_commandExecutor.AddCommandGroup(new AppiumVirtualKeyboardActions(this));
_commandExecutor.AddCommandGroup(new AppiumSliderActions(this));
_commandExecutor.AddCommandGroup(new AppiumSwipeActions(this));
_commandExecutor.AddCommandGroup(new AppiumOrientationActions(this));
}
public abstract ApplicationState AppState { get; }
public virtual IUIElementQueryable Query => new AppiumQueryable(this);
public IConfig Config => _config;
public AppiumDriver Driver => _driver;
public ICommandExecution CommandExecutor => _commandExecutor;
public string ElementTree => _driver.PageSource;
public FileInfo Screenshot(string fileName)
{
Screenshot screenshot = _driver.GetScreenshot();
screenshot.SaveAsFile(fileName, ScreenshotImageFormat.Png);
var file = new FileInfo(fileName);
return file;
}
public byte[] Screenshot()
{
Screenshot screenshot = _driver.GetScreenshot();
return screenshot.AsByteArray;
}
public IEnumerable<string> GetLogTypes()
{
return _driver.Manage().Logs.AvailableLogTypes;
}
public IEnumerable<string> GetLogEntries(string logType)
{
var entries = _driver.Manage().Logs.GetLog(logType);
foreach (var entry in entries)
{
yield return entry.Message;
}
}
#nullable disable
public virtual IUIElement FindElement(string id)
{
return Query.ById(id).FirstOrDefault();
}
#nullable enable
public virtual IUIElement FindElement(IQuery query)
{
AppiumQuery? appiumQuery = query as AppiumQuery;
if (appiumQuery is not null)
{
return appiumQuery.FindElement(this);
}
var queryString = query.ToString() ?? throw new InvalidOperationException($"{nameof(FindElement)} could not get query string");
var q = new AppiumQuery(queryString);
return q.FindElement(this);
}
public virtual IReadOnlyCollection<IUIElement> FindElements(string id)
{
return Query.ById(id);
}
public virtual IReadOnlyCollection<IUIElement> FindElements(IQuery query)
{
AppiumQuery? appiumQuery = query as AppiumQuery;
if (appiumQuery is not null)
{
return appiumQuery.FindElements(this);
}
var queryString = query.ToString() ?? throw new InvalidOperationException($"{nameof(FindElement)} could not get query string");
var q = new AppiumQuery(queryString);
return q.FindElements(this);
}
protected static void SetGeneralAppiumOptions(IConfig config, AppiumOptions appiumOptions)
{
appiumOptions.AddAdditionalAppiumOption("reportDirectory", config.GetProperty<string>("ReportDirectory"));
appiumOptions.AddAdditionalAppiumOption("reportFormat", config.GetProperty<string>("ReportFormat"));
var testName = config.GetProperty<string>("TestName");
if (!string.IsNullOrWhiteSpace(testName))
appiumOptions.AddAdditionalAppiumOption("testName", testName);
if (config.GetProperty<bool>("FullReset"))
appiumOptions.AddAdditionalAppiumOption(MobileCapabilityType.FullReset, "true");
var appPath = config.GetProperty<string>("AppPath");
if (!string.IsNullOrEmpty(appPath))
appiumOptions.App = appPath;
appiumOptions.PlatformName = config.GetProperty<string>("PlatformName");
appiumOptions.AutomationName = config.GetProperty<string>("AutomationName");
var deviceName = config.GetProperty<string>("DeviceName");
if (!string.IsNullOrEmpty(deviceName))
appiumOptions.DeviceName = deviceName;
var platformVersion = config.GetProperty<string>("PlatformVersion");
if (!string.IsNullOrEmpty(platformVersion))
appiumOptions.PlatformVersion = platformVersion;
appiumOptions.AddAdditionalAppiumOption(MobileCapabilityType.NewCommandTimeout, 3000);
}
public void Dispose()
{
_driver?.Dispose();
}
}
}