-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutlookSession.cs
89 lines (73 loc) · 3.12 KB
/
OutlookSession.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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OutlookAutomation
{
public class OutlookSession
{
private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
private const string OutlookAppId = @"C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE";
static bool outlookWindow = false;
public static Process WinApp;
public static WindowsDriver<WindowsElement> outlookSession;
public static WindowsDriver<WindowsElement> desktopSession;
public static void OutlookSessionSetup()
{
WinApp = Process.Start(@"..\..\Windows Application Driver\WinAppDriver.exe");
AppiumOptions desktopCapabilities = new AppiumOptions();
desktopCapabilities.AddAdditionalCapability("app", "Root");
desktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), desktopCapabilities);
try
{
outlookWindow = desktopSession.FindElementByClassName("rctrl_renwnd32").Displayed;
}
catch (WebDriverException)
{
outlookWindow = false;
}
if (!outlookWindow)
{
AppiumOptions outlookCapabilities = new AppiumOptions();
outlookCapabilities.AddAdditionalCapability("app", OutlookAppId);
outlookSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), outlookCapabilities);
outlookSession.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Thread.Sleep(TimeSpan.FromSeconds(10));
}
else
{
OutlookSessionCreate();
}
}
public static void OutlookSessionCreate()
{
WindowsElement outlookWindowElement = desktopSession.FindElementByClassName("rctrl_renwnd32");
string OutlookTopLevelWindowHandle = OutlookTopLevelWindowHandle = (int.Parse(outlookWindowElement.GetAttribute("NativeWindowHandle"))).ToString("x");
AppiumOptions outlookCapabilities = new AppiumOptions();
outlookCapabilities.AddAdditionalCapability("appTopLevelWindow", OutlookTopLevelWindowHandle);
outlookCapabilities.AddAdditionalCapability("deviceName", "DESKTOP-SHASHANK");
outlookSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), outlookCapabilities);
Thread.Sleep(TimeSpan.FromSeconds(2));
}
public static void TearDown()
{
if (outlookSession != null)
{
outlookSession.Quit();
outlookSession = null;
}
if (desktopSession != null)
{
desktopSession.Quit();
desktopSession = null;
}
}
}
}