-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathDriverTestFixture.cs
202 lines (174 loc) · 10.8 KB
/
DriverTestFixture.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// <copyright file="DriverTestFixture.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>
using NUnit.Framework;
using OpenQA.Selenium.Environment;
using System;
using static NUnit.Framework.Interfaces.ResultState;
namespace OpenQA.Selenium
{
public abstract class DriverTestFixture
{
public string alertsPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("alerts.html");
public string blankPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("blank.html");
public string macbethPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("macbeth.html");
public string macbethTitle = "Macbeth: Entire Play";
public string simpleTestPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("simpleTest.html");
public string simpleTestTitle = "Hello WebDriver";
public string framesPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("win32frameset.html");
public string framesTitle = "This page has frames";
public string iframesPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("iframes.html");
public string iframesTitle = "This page has iframes";
public string formsPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("formPage.html");
public string formsTitle = "We Leave From Here";
public string javascriptPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("javascriptPage.html");
public string loginPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("login.html");
public string clickEventPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("clickEventPage.html");
public string resultPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("resultPage.html");
public string nestedPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("nestedElements.html");
public string xhtmlTestPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("xhtmlTest.html");
public string richTextPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("rich_text.html");
public string dragAndDropPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("dragAndDropTest.html");
public string framesetPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("frameset.html");
public string iframePage = EnvironmentManager.Instance.UrlBuilder.WhereIs("iframes.html");
public string metaRedirectPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("meta-redirect.html");
public string redirectPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("redirect");
public string rectanglesPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("rectangles.html");
public string javascriptEnhancedForm = EnvironmentManager.Instance.UrlBuilder.WhereIs("javascriptEnhancedForm.html");
public string uploadPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("upload.html");
public string transparentUploadPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("transparentUpload.html");
public string childPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("child/childPage.html");
public string grandchildPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("child/grandchild/grandchildPage.html");
public string documentWrite = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("document_write_in_onload.html");
public string chinesePage = EnvironmentManager.Instance.UrlBuilder.WhereIs("cn-test.html");
public string svgPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("svgPiechart.xhtml");
public string dynamicPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("dynamic.html");
public string tables = EnvironmentManager.Instance.UrlBuilder.WhereIs("tables.html");
public string deletingFrame = EnvironmentManager.Instance.UrlBuilder.WhereIs("frame_switching_tests/deletingFrame.html");
public string ajaxyPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("ajaxy_page.html");
public string sleepingPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep");
public string slowIframes = EnvironmentManager.Instance.UrlBuilder.WhereIs("slow_loading_iframes.html");
public string draggableLists = EnvironmentManager.Instance.UrlBuilder.WhereIs("draggableLists.html");
public string droppableItems = EnvironmentManager.Instance.UrlBuilder.WhereIs("droppableItems.html");
public string bodyTypingPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("bodyTypingTest.html");
public string formSelectionPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("formSelectionPage.html");
public string selectableItemsPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("selectableItems.html");
public string underscorePage = EnvironmentManager.Instance.UrlBuilder.WhereIs("underscore.html");
public string clickJackerPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("click_jacker.html");
public string errorsPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("errors.html");
public string selectPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("selectPage.html");
public string simpleXmlDocument = EnvironmentManager.Instance.UrlBuilder.WhereIs("simple.xml");
public string mapVisibilityPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("map_visibility.html");
public string mouseTrackerPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mousePositionTracker.html");
public string mouseOverPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mouseOver.html");
public string mouseInteractionPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mouse_interaction.html");
public string readOnlyPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("readOnlyPage.html");
public string clicksPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("clicks.html");
public string booleanAttributes = EnvironmentManager.Instance.UrlBuilder.WhereIs("booleanAttributes.html");
public string linkedImage = EnvironmentManager.Instance.UrlBuilder.WhereIs("linked_image.html");
public string xhtmlFormPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("xhtmlFormPage.xhtml");
public string svgTestPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("svgTest.svg");
public string slowLoadingAlertPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("slowLoadingAlert.html");
public string dragDropOverflowPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("dragDropOverflow.html");
public string missedJsReferencePage = EnvironmentManager.Instance.UrlBuilder.WhereIs("missedJsReference.html");
public string authenticationPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("basicAuth");
public string html5Page = EnvironmentManager.Instance.UrlBuilder.WhereIs("html5Page.html");
public string shadowRootPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("shadowRootPage.html");
public string scrollFrameOutOfViewport = EnvironmentManager.Instance.UrlBuilder.WhereIs("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");
public string scrollFrameInViewport = EnvironmentManager.Instance.UrlBuilder.WhereIs("scrolling_tests/frame_with_nested_scrolling_frame.html");
public string printPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("printPage.html");
public IWebDriver driver { get; set; }
public bool IsNativeEventsEnabled
{
get
{
if (driver is IHasCapabilities capabilitiesDriver &&
capabilitiesDriver.Capabilities.HasCapability(CapabilityType.HasNativeEvents) &&
(bool)capabilitiesDriver.Capabilities.GetCapability(CapabilityType.HasNativeEvents))
{
return true;
}
return false;
}
}
[OneTimeSetUp]
public void SetUp()
{
driver = EnvironmentManager.Instance.GetCurrentDriver();
}
[TearDown]
public void ResetOnError()
{
if (TestContext.CurrentContext.Result.Outcome == Error)
{
driver = EnvironmentManager.Instance.CreateFreshDriver();
}
}
/*
* Exists because a given test might require a fresh driver
*/
protected void CreateFreshDriver()
{
driver = EnvironmentManager.Instance.CreateFreshDriver();
}
protected bool WaitFor(Func<bool> waitFunction, string timeoutMessage)
{
return WaitFor<bool>(waitFunction, timeoutMessage);
}
protected T WaitFor<T>(Func<T> waitFunction, string timeoutMessage)
{
return this.WaitFor<T>(waitFunction, TimeSpan.FromSeconds(5), timeoutMessage);
}
protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout, string timeoutMessage)
{
DateTime endTime = DateTime.Now.Add(timeout);
T value = default;
Exception lastException = null;
while (DateTime.Now < endTime)
{
try
{
value = waitFunction();
if (typeof(T) == typeof(bool))
{
if ((bool)(object)value)
{
return value;
}
}
else if (value != null)
{
return value;
}
System.Threading.Thread.Sleep(100);
}
catch (Exception e)
{
// Swallow for later re-throwing
lastException = e;
}
}
if (lastException != null)
{
throw new WebDriverException("Operation timed out", lastException);
}
Assert.Fail("Condition timed out: " + timeoutMessage);
return default;
}
}
}