forked from microsoft/WinAppDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sessions.cs
140 lines (120 loc) · 6.17 KB
/
Sessions.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
138
139
140
//******************************************************************************
//
// Copyright (c) 2016 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************
using System.IO;
using System.Net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using OpenQA.Selenium.Appium.Windows;
namespace WebDriverAPI
{
[TestClass]
public class Sessions
{
[TestMethod]
public void GetSessions_CurrentList()
{
using (HttpWebResponse response = WebRequest.Create(CommonTestSettings.WindowsApplicationDriverUrl + "/sessions").GetResponse() as HttpWebResponse)
{
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
JObject responseObject = JObject.Parse(responseString);
Assert.AreEqual(0, (int)responseObject["status"]);
JArray capabilitiesArray = (JArray)responseObject["value"];
Assert.IsNotNull(capabilitiesArray);
Assert.IsTrue(capabilitiesArray.Count >= 0);
}
}
[TestMethod]
public void GetSessions_SingleEntry()
{
WindowsDriver<WindowsElement> session = Utility.CreateNewSession(CommonTestSettings.AlarmClockAppId);
Assert.IsNotNull(session);
using (HttpWebResponse response = WebRequest.Create(CommonTestSettings.WindowsApplicationDriverUrl + "/sessions").GetResponse() as HttpWebResponse)
{
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
JObject responseObject = JObject.Parse(responseString);
Assert.AreEqual(0, (int)responseObject["status"]);
JArray capabilitiesArray = (JArray)responseObject["value"];
Assert.IsTrue(capabilitiesArray.Count >= 1);
// Verify that the newly created session is on the list
JToken newSessionEntry = null;
foreach (var entry in capabilitiesArray.Children())
{
if (entry["id"].ToString() == session.SessionId.ToString())
{
newSessionEntry = entry;
break;
}
}
Assert.IsNotNull(newSessionEntry);
Assert.AreEqual(CommonTestSettings.AlarmClockAppId, newSessionEntry["capabilities"]["app"].ToString());
}
session.Quit();
}
[TestMethod]
public void GetSessions_MultipleEntry()
{
WindowsDriver<WindowsElement> calculatorSession = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
Assert.IsNotNull(calculatorSession);
WindowsDriver<WindowsElement> notepadSession = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);
Assert.IsNotNull(notepadSession);
int openSessionsCount = 0;
using (HttpWebResponse response = WebRequest.Create(CommonTestSettings.WindowsApplicationDriverUrl + "/sessions").GetResponse() as HttpWebResponse)
{
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
JObject responseObject = JObject.Parse(responseString);
Assert.AreEqual(0, (int)responseObject["status"]);
// There needs to be at least 2 open sessions after we create the 2 session above
JArray capabilitiesArray = (JArray)responseObject["value"];
openSessionsCount = capabilitiesArray.Count;
Assert.IsTrue(openSessionsCount >= 2);
// Verify that both calculator and notepad sessions are created
JToken calculatorSessionEntry = null;
JToken notepadSessionEntry = null;
foreach (var entry in capabilitiesArray.Children())
{
if (entry["id"].ToString() == calculatorSession.SessionId.ToString())
{
calculatorSessionEntry = entry;
}
else if (entry["id"].ToString() == notepadSession.SessionId.ToString())
{
notepadSessionEntry = entry;
}
if (calculatorSessionEntry != null && notepadSessionEntry != null)
{
break;
}
}
Assert.IsNotNull(calculatorSessionEntry);
Assert.AreEqual(CommonTestSettings.CalculatorAppId, calculatorSessionEntry["capabilities"]["app"].ToString());
Assert.IsNotNull(notepadSessionEntry);
Assert.AreEqual(CommonTestSettings.NotepadAppId, notepadSessionEntry["capabilities"]["app"].ToString());
}
// Close the newly created sessions
calculatorSession.Quit();
notepadSession.Quit();
using (HttpWebResponse response = WebRequest.Create(CommonTestSettings.WindowsApplicationDriverUrl + "/sessions").GetResponse() as HttpWebResponse)
{
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
JObject responseObject = JObject.Parse(responseString);
Assert.AreEqual(0, (int)responseObject["status"]);
// There needs to be 2 less sessions after we closed the 2 sessions above
JArray capabilitiesArray = (JArray)responseObject["value"];
Assert.AreEqual(openSessionsCount - 2, capabilitiesArray.Count);
}
}
}
}