-
Notifications
You must be signed in to change notification settings - Fork 674
/
WebView2Tests.cs
2926 lines (2524 loc) · 132 KB
/
WebView2Tests.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using Common;
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Common;
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Infra;
using Microsoft.Windows.Apps.Test.Automation;
using Microsoft.Windows.Apps.Test.Foundation;
using Microsoft.Windows.Apps.Test.Foundation.Controls;
using Microsoft.Windows.Apps.Test.Foundation.Waiters;
#if USING_TAEF
using WEX.TestExecution;
using WEX.TestExecution.Markup;
using WEX.Logging.Interop;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Button = Microsoft.Windows.Apps.Test.Foundation.Controls.Button;
namespace Windows.UI.Xaml.Tests.MUXControls.InteractionTests
{
[TestClass]
public class WebView2Tests
{
[ClassInitialize]
[TestProperty("RunAs", "User")]
[TestProperty("Classification", "Integration")]
[TestProperty("Platform", "Any")]
[TestProperty("TestSuite", "A")]
public static void ClassInitialize(TestContext testContext)
{
TestEnvironment.Initialize(testContext);
EnsureBrowser();
}
private static async Task DownloadFileAsync(string url, string filePath)
{
using (var client = new HttpClient())
{
using (var result = await client.GetAsync(url))
{
result.EnsureSuccessStatusCode();
var bytes = await result.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync(filePath, bytes);
}
}
}
private static void DownloadFile(string url, string filePath)
{
var task = DownloadFileAsync(url, filePath);
task.Wait(TimeSpan.FromMinutes(10));
}
// Ensure a suitable version of Edge browser or WebView2 runtime is present. If not, get an installer from the web and install it.
public static void EnsureBrowser()
{
// If a previous run of CoreWebView2Initialized_FailedTest failed to clean up the
// fake browser executable folder key it set, remove it here.
RemoveFakeBrowserExecutableFolderKey();
// 1. Get installed Edge browser (or WebView2 Runtime) build version info.
int browserBuildVersion = GetInstalledBrowserVersion();
// 2. Get Microsoft.Web.WebView2.Core.dll build version info.
int sdkBuildVersion = GetSdkBuildVersion();
// 3. Check if a runtime is already installed, and if it's compatible.
bool hasCompatibleRuntimeInstalled = GetHasCompatibleRuntimeInstalled(browserBuildVersion, sdkBuildVersion);
// 4a. If we have a compatible runtime installed, continue on to the tests!
if (hasCompatibleRuntimeInstalled)
{
return;
}
// 4b. If we don't have a compatible runtime, try to install one.
// 5. Before installing, check if Edge is already installing/updating. We can't run the standalone installer if it is.
bool didWait = WaitForEdgeIfAlreadyInstalling();
if (didWait && IsEdgeAlreadyInstalling())
{
Log.Error("WebView2Tests Init: Edge took more than 3 minutes to install, give up.");
}
else if (didWait)
{
// If we waited for Edge to finish installing/updating and it finished, check the versions again
browserBuildVersion = GetInstalledBrowserVersion();
hasCompatibleRuntimeInstalled = GetHasCompatibleRuntimeInstalled(browserBuildVersion, sdkBuildVersion);
if (hasCompatibleRuntimeInstalled)
{
return;
}
}
// 6. If Edge wasn't already installing/updating, or it was but installed something old (unlikely),
// run the standalone installer to install it.
// This installation can sometimes fail, so we'll try a number of times before failing out.
int attemptsLeft = 5;
bool successfullyInstalled = false;
while (attemptsLeft > 0)
{
attemptsLeft--;
successfullyInstalled = TryInstallingBrowser(attemptsLeft);
if (successfullyInstalled) break;
}
if (attemptsLeft == 0 && !successfullyInstalled)
{
Log.Error("WebView2Tests Init: Browser installation failed, out of retries.");
}
}
private static void RemoveFakeBrowserExecutableFolderKey()
{
var browserExecutableKey = GetBrowserExecutableFolderKey();
var browserExecutableFolder = browserExecutableKey.GetValue(TestApplicationInfo.MUXControlsTestApp.ProcessName);
if (browserExecutableFolder != null)
{
browserExecutableKey.DeleteValue(TestApplicationInfo.MUXControlsTestApp.ProcessName);
Log.Comment("Removed key for {0}", TestApplicationInfo.MUXControlsTestApp.ProcessName);
}
}
private static int GetInstalledBrowserVersion()
{
string browserVersionString = string.Empty;
IntPtr browserVersionPtr;
int retval = NativeMethods.GetAvailableCoreWebView2BrowserVersionString(string.Empty, out browserVersionPtr);
if (retval != (int)NativeMethods.HResults.S_OK)
{
Log.Warning("WebView2Tests Init: Error: got hresult {0:x8} retrieving GetAvailableCoreWebView2BrowserVersionString", retval);
}
else if (browserVersionPtr == IntPtr.Zero)
{
Log.Warning("WebView2Tests Init: GetAvailableCoreWebView2BrowserVersionString returned version of 0");
}
else
{
// eg "80.0.361.48 beta", 361 is the build version
browserVersionString = Marshal.PtrToStringUni(browserVersionPtr);
Marshal.FreeCoTaskMem(browserVersionPtr);
}
int browserBuildVersion = 0;
string installedBrowser;
if (string.IsNullOrEmpty(browserVersionString))
{
installedBrowser = "none";
}
else
{
browserBuildVersion = int.Parse(browserVersionString.Split('.')[2]);
installedBrowser = string.Format("version {0} [build version: {1}]", browserVersionString, browserBuildVersion);
}
Log.Comment("WebView2Tests Init: Found Edge browser/WV2Runtime: {0}", installedBrowser);
return browserBuildVersion;
}
private static int GetSdkBuildVersion()
{
int sdkBuildVersion = 0;
string dllName = "Microsoft.Web.WebView2.Core.dll";
string dllPath = Path.Combine(Environment.CurrentDirectory, dllName);
try
{
FileVersionInfo sdkVersionInfo = FileVersionInfo.GetVersionInfo(dllPath);
sdkBuildVersion = sdkVersionInfo.ProductBuildPart;
Log.Comment("WebView2Tests Init: Found {0}: version {1} [build version: {2}] (file: {3})",
dllName,
sdkVersionInfo.ProductVersion,
sdkBuildVersion,
sdkVersionInfo.FileName);
}
catch (Exception e)
{
Log.Error("WebView2Tests Init: could not find loader at {0} [exception: {1}]", dllPath, e.ToString());
}
return sdkBuildVersion;
}
private static bool GetHasCompatibleRuntimeInstalled(int browserBuildVersion, int sdkBuildVersion)
{
bool hasCompatibleRuntime = false;
// The full versions of browser/runtime and WebView2 SDK are not directly comparable:
// Browser/Runtime version ex: 80.0.361.48
// WebView2 SDK version ex: 0.8.355
// Webview2Loader compares the build versions (361 and 355 in above example), we do so here as well
if (browserBuildVersion >= sdkBuildVersion)
{
Log.Comment("WebView2Tests Init: Installed Edge build will be used for testing... ");
hasCompatibleRuntime = true;
}
return hasCompatibleRuntime;
}
private static bool WaitForEdgeIfAlreadyInstalling()
{
bool didWait = false;
int curWait = 0;
int maxWait = 180; // wait for 3 minutes, should finish in 1-2
while (IsEdgeAlreadyInstalling() && curWait < maxWait)
{
Log.Comment("Note: Edge is updating, wait up to 180 seconds for it to finish. Waited {0} seconds so far, wait another 10...", curWait);
Wait.ForSeconds(10);
curWait += 10;
didWait = true;
}
return didWait;
}
private static bool IsEdgeAlreadyInstalling()
{
bool edgeUpdateIsRunning = false;
// Print if there are any Edge processes running that might interfere with installation
// (specifically, MicrosoftEdge<Version> or MicrosoftEdgeUpdate).
foreach (var process in Process.GetProcesses())
{
if (process.ProcessName.ToLower().ToString().Contains("edge"))
{
Log.Comment("Note: process '{0}' ({1}) is running", process.ProcessName, process.Id);
if (process.ProcessName.ToString().Equals("MicrosoftEdgeUpdate"))
{
edgeUpdateIsRunning = true;
}
}
}
return edgeUpdateIsRunning;
}
private static bool TryInstallingBrowser(int attemptsLeft)
{
Log.Comment("Downloading Edge installer...");
DownloadFile("https://go.microsoft.com/fwlink/p/?LinkId=2124703",
"MicrosoftEdgeWebview2Setup.exe");
bool successfullyInstalled = false;
string installer = "MicrosoftEdgeWebview2Setup.exe";
// Note: Starting with SDK 0.9.488 / Edge version 83, evergreen WebView2 no longer targets the Stable
// browser channel. Instead, it targets another set of binaries, branded Evergreen WebView2 Runtime.
// (See https://docs.microsoft.com/en-us/microsoft-edge/webview2/releasenotes).
string installerArgs_WV2Runtime = "/silent /install";
Log.Comment(@"WebView2Tests Init: Installing WebView2 runtime: '{0} {1}'", installer, installerArgs_WV2Runtime);
ProcessStartInfo installerStartInfo = new ProcessStartInfo(installer, installerArgs_WV2Runtime);
Process installerProcess = Process.Start(installerStartInfo);
installerProcess.WaitForExit();
if (installerProcess.ExitCode == 0)
{
Log.Comment("WebView2Tests Init: {0} exited successfully", installer);
successfullyInstalled = true;
}
else
{
Log.Warning("WebView2Tests Init: {0} failed with exit code {1}! Attempts left: {2}", installer, installerProcess.ExitCode, attemptsLeft);
// Print information about some of the error codes we sometimes hit
if (installerProcess.ExitCode == 4)
{
Log.Comment("Exit code 4 = HIGHER_VERSION_EXISTS, Higher version already exists");
}
else if (installerProcess.ExitCode == 60)
{
Log.Comment("Exit code 60 = SETUP_SINGLETON_ACQUISITION_FAILED, The setup process could not acquire the exclusive right to modify the installation.");
}
}
return successfullyInstalled;
}
[TestCleanup]
public void TestCleanup()
{
TestCleanupHelper.Cleanup();
}
internal class WebView2TestSetupHelper : TestSetupHelper
{
public WebView2TestSetupHelper(global::System.Collections.Generic.ICollection<string> testNames)
: base(testNames)
{
}
public override void Dispose()
{
try
{
var result = new Edit(FindElement.ById("CleanupResultTextBox"));
Button CleanupTestButton = new Button(FindElement.ById("CleanupTest"));
string expectedValue = "Cleanup completed.";
Log.Comment("Waiting for cleanup...");
using (var waiter = new ValueChangedEventWaiter(result, expectedValue))
{
CleanupTestButton.Invoke();
if (!waiter.TryWait())
{
Log.Warning("Cleanup may have failed.");
}
}
}
catch { }
base.Dispose();
}
}
private static void WaitForWebMessageResult(string selectedTest)
{
var result = new Edit(FindElement.ById("TestResult"));
var check = new CheckBox(FindElement.ById("MessageReceived"));
string expectedValue = selectedTest + ": Passed []";
Log.Comment("Waiting for result...");
using (var waiter = check.GetToggledWaiter())
{
// If the web message hasn't been received, wait for it
if (check.ToggleState != ToggleState.On && !waiter.TryWait())
{
Log.Error("Expected web message was never received.");
}
}
// By now, we'll have gotten a pass/fail result
Verify.AreEqual(expectedValue, result.Value);
}
private static void CompleteTestAndWaitForWebMessageResult(string selectedTest)
{
var result = new Edit(FindElement.ById("TestResult"));
var check = new CheckBox(FindElement.ById("MessageReceived"));
Button CompleteTestButton = new Button(FindElement.ById("CompleteTest"));
string expectedValue = selectedTest + ": Passed []";
Log.Comment("Waiting for result...");
CompleteTestButton.Invoke();
// If the web message hasn't been received, wait for it
using (var waiter = check.GetToggledWaiter())
{
if (check.ToggleState != ToggleState.On && !waiter.TryWait())
{
Log.Error("Expected web message was never received.");
}
}
// By now, we'll have gotten a pass/fail result
Verify.AreEqual(expectedValue, result.Value);
}
private static void CompleteTestAndWaitForResult(string selectedTest)
{
var result = new Edit(FindElement.ById("TestResult"));
Button CompleteTestButton = new Button(FindElement.ById("CompleteTest"));
string expectedValue = selectedTest + ": Passed []";
Log.Comment("Waiting for result...");
using (var waiter = new ValueChangedEventWaiter(result, expectedValue))
{
CompleteTestButton.Invoke();
waiter.TryWait();
}
Verify.AreEqual(expectedValue, result.Value);
}
private static void WaitForTextBoxValue(Edit textbox, string expectedValue, bool match = true)
{
// if value is empty, wait for it
if (textbox.Value == string.Empty)
{
using (var waiter = new ValueChangedEventWaiter(textbox, expectedValue))
{
waiter.Wait();
}
}
if (match == false)
{
if (textbox.Value == expectedValue)
{
return;
}
}
else
{
if (textbox.Value != expectedValue)
{
return;
}
}
}
private static void DoSelectAllByKeyboard()
{
Log.Comment("Do Select All");
KeyboardHelper.PressDownModifierKey(ModifierKey.Control);
TextInput.SendText("a");
KeyboardHelper.ReleaseModifierKey(ModifierKey.Control);
}
private static void CopySelected()
{
Log.Comment("Do Copy");
KeyboardHelper.PressDownModifierKey(ModifierKey.Control);
TextInput.SendText("c");
KeyboardHelper.ReleaseModifierKey(ModifierKey.Control);
}
private static void PasteClipboard()
{
Log.Comment("Do Paste");
KeyboardHelper.PressDownModifierKey(ModifierKey.Control);
TextInput.SendText("v");
KeyboardHelper.ReleaseModifierKey(ModifierKey.Control);
}
private static void WaitForCopyCompleted()
{
string expectedValue = "HTML files ready";
var status1 = new Edit(FindElement.ById("Status1"));
WaitForTextBoxValue(status1, expectedValue);
}
private static void WaitForLoadCompleted()
{
// if load completed then status2 will have name of the webview
var status2 = new Edit(FindElement.ById("Status2"));
WaitForTextBoxValue(status2, string.Empty, false /* match = false for not match */);
}
private static void WaitForEnsureCompleted()
{
var status1 = new Edit(FindElement.ById("Status1"));
WaitForTextBoxValue(status1, "EnsureCoreWebView2Async() completed", true);
}
private static void ChooseTest(string testName, bool waitForLoadCompleted = true)
{
WaitForCopyCompleted();
string comboBoxName = "TestNameComboBox";
Log.Comment("Retrieve ComboBox item with name '{0}'.", comboBoxName);
ComboBox comboBox = new ComboBox(FindElement.ById(comboBoxName));
comboBox.SelectItemByName(testName);
Wait.ForIdle();
if (waitForLoadCompleted)
{
WaitForLoadCompleted();
}
}
// Replace calls to WaitForFocus with waiters when task 23527231 is completed making WebMessageReceivedHandler available.
private static void WaitForFocus(string target)
{
var anaheimFocusTextBox = new Edit(FindElement.ById("AnaheimFocusTextBox"));
var expectedFocusMessage = "Focus on: " + target;
WaitForTextBoxValue(anaheimFocusTextBox, expectedFocusMessage);
Log.Comment("Focus is on " + UIObject.Focused);
Verify.AreEqual(anaheimFocusTextBox.Value, expectedFocusMessage);
}
// 'target' and 'tabs' should be in accordance with the html page being navigated on.
private static void SetWebViewElementFocus(string target, uint tabs)
{
Button x1 = new Button(FindElement.ById("TabStopButton1")); // Xaml TabStop 1
x1.SetFocus();
Wait.ForIdle();
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
KeyboardHelper.PressKey(Key.Tab, ModifierKey.None, tabs);
WaitForFocus(target);
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void BasicRenderingTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("BasicRenderingTest");
Wait.ForMilliseconds(100); // rendering a webpage takes some time after navigation completed
// To verify, check for the appropriate pixel(s) using User32 and GDI32 calls here in the test runner.
WebView2Temporary.WebView2RenderingVerifier.VerifyInstances("BasicRenderingTest");
CompleteTestAndWaitForResult("BasicRenderingTest");
}
}
private static void MouseClickTestCommon(PointerButtons mouseButton)
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
string selectedTest = "Invalid";
if (mouseButton == PointerButtons.Primary)
{
selectedTest = "MouseLeftClickTest";
}
else if (mouseButton == PointerButtons.Middle)
{
selectedTest = "MouseMiddleClickTest";
}
else if (mouseButton == PointerButtons.Secondary)
{
selectedTest = "MouseRightClickTest";
}
else if (mouseButton == PointerButtons.XButton1)
{
selectedTest = "MouseXButton1ClickTest";
}
else if (mouseButton == PointerButtons.XButton2)
{
selectedTest = "MouseXButton2ClickTest";
}
ChooseTest(selectedTest);
var webview = FindElement.ById("MyWebView2");
Rectangle bounds = webview.BoundingRectangle;
Log.Comment("Bounds = X:{0}, Y:{1}, Width:{2}, Height:{3}", bounds.X, bounds.Y, bounds.Width, bounds.Height);
// TODO_WebView2Input - Task 30555367 - Stack Overflow in WebView2 pointer tests when (MUXC) test infra calls WV2.GetClickablePoint()
// Workaround to avoid any calls to WebView2.GetClickablePoint
/*
var point = webview.GetClickablePoint();
Log.Comment("ClickablePoint = X:{0}, Y:{1}", point.X, point.Y);
// Move mouse to top left coordinate (location of web button) relative from the center of webview
InputHelper.MoveMouse(webview,
-bounds.Width/2 + 20, -bounds.Height/2 + 20);
*/
var point = new Point(bounds.X + 20, bounds.Y + 20);
Log.Comment("Move mouse to ({0}, {1})", bounds.X + 20, bounds.Y + 20);
PointerInput.Move(point);
PointerInput.Press(mouseButton);
PointerInput.Release(mouseButton);
Wait.ForIdle();
WaitForWebMessageResult(selectedTest);
}
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void MouseLeftClickTest()
{
MouseClickTestCommon(PointerButtons.Primary);
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void MouseMiddleClickTest()
{
MouseClickTestCommon(PointerButtons.Middle);
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void MouseRightClickTest()
{
MouseClickTestCommon(PointerButtons.Secondary);
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void MouseXButton1ClickTest()
{
MouseClickTestCommon(PointerButtons.XButton1);
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void MouseXButton2ClickTest()
{
MouseClickTestCommon(PointerButtons.XButton2);
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void MouseWheelScrollTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("MouseWheelScrollTest");
var webview = FindElement.ById("MyWebView2");
// Mouse wheel delta amount required per initial velocity unit
const int mouseWheelDeltaForVelocityUnit = -4000; // scroll downwards
// TODO_WebView2Input - Task 30555367 - Stack Overflow in WebView2 pointer tests when (MUXC) test infra calls WV2.GetClickablePoint()
// Workaround to avoid any calls to WebView2.GetClickablePoint
/*
InputHelper.RotateWheel(webview, mouseWheelDeltaForVelocityUnit);
*/
Rectangle bounds = webview.BoundingRectangle;
var point = new Point(bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2);
PointerInput.Move(point);
MouseWheelInput.RotateWheel(mouseWheelDeltaForVelocityUnit);
WaitForWebMessageResult("MouseWheelScrollTest");
}
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void CursorUpdateTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
// On ChooseTest, replace the default webview with one that exposes the ProtectedCursor member
ChooseTest("CursorUpdateTest");
// Clear the cache so we can find the new webview
ElementCache.Clear();
var webview = FindElement.ById("MyWebView2");
Rectangle bounds = webview.BoundingRectangle;
Log.Comment("Bounds = X:{0}, Y:{1}, Width:{2}, Height:{3}", bounds.X, bounds.Y, bounds.Width, bounds.Height);
// Top left coordinate (location of web button) relative from the center of webview
// TODO_WebView2Input - Task 30555367 - Stack Overflow in WebView2 pointer tests when (MUXC) test infra calls WV2.GetClickablePoint()
// Workaround to avoid any calls to WebView2.GetClickablePoint
/*
InputHelper.MoveMouse(webview, -bounds.Width / 2 + 20, -bounds.Height / 2 + 20);
*/
var point = new Point(bounds.X + 20, bounds.Y + 20);
PointerInput.Move(point);
Wait.ForIdle();
// Checks that the ProtectedCursor type has changed to the one we expect
CompleteTestAndWaitForResult("CursorUpdateTest");
}
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void CursorClickUpdateTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
// On ChooseTest, replace the default webview with one that exposes the ProtectedCursor member
ChooseTest("CursorClickUpdateTest");
// Clear the cache so we can find the new webview
ElementCache.Clear();
var webview = FindElement.ById("MyWebView2");
Rectangle bounds = webview.BoundingRectangle;
Log.Comment("Bounds = X:{0}, Y:{1}, Width:{2}, Height:{3}", bounds.X, bounds.Y, bounds.Width, bounds.Height);
// Click somewhere in the webview that's not the button a few times
var centerOfWebView = new Point(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
PointerInput.Move(centerOfWebView);
PointerInput.Press(PointerButtons.Primary);
PointerInput.Release(PointerButtons.Primary);
PointerInput.Press(PointerButtons.Primary);
PointerInput.Release(PointerButtons.Primary);
PointerInput.Press(PointerButtons.Primary);
PointerInput.Release(PointerButtons.Primary);
var insideButton = new Point(bounds.X + 20, bounds.Y + 20);
PointerInput.Move(insideButton);
Wait.ForIdle();
// Checks that the ProtectedCursor type has changed to the one we expect
CompleteTestAndWaitForResult("CursorClickUpdateTest");
}
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void NavigationErrorTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("NavigationErrorTest");
CompleteTestAndWaitForResult("NavigationErrorTest");
}
}
// For each of the Focus_* tests:
// 1) We Tab/Shift+Tab/Click amongst xaml controls(x1, x2) and web controls(w1, w2).
// 2) After each such change:
// a. Validate Edge focus state by waiting for web message sent when an element gets focus
// b. Validate Xaml focus using MITA APIs directly
[TestMethod]
[TestProperty("TestSuite", "A")]
public void Focus_BasicTabTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("Focus_BasicTabTest");
Button x1 = new Button(FindElement.ById("TabStopButton1")); // Xaml TabStop 1
Button x2 = new Button(FindElement.ById("TabStopButton2")); // Xaml TabStop 2
Log.Comment("Description: x1 -> w1 -> w2 -> x2 ");
Log.Comment("Focus on x1");
x1.SetFocus();
Wait.ForIdle();
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
Log.Comment("Tab x1 -> w1");
KeyboardHelper.PressKey(Key.Tab);
WaitForFocus("w1");
Log.Comment("Tab w1 -> w2");
KeyboardHelper.PressKey(Key.Tab);
WaitForFocus("w2");
Log.Comment("Tab w2 -> x2");
using (var xamlFocusWaiter = new FocusAcquiredWaiter("TabStopButton2"))
{
KeyboardHelper.PressKey(Key.Tab);
xamlFocusWaiter.Wait();
Log.Comment("Focus is on " + UIObject.Focused);
Verify.IsTrue(x2.HasKeyboardFocus, "TabStopButton2 has keyboard focus");
}
CompleteTestAndWaitForResult("Focus_BasicTabTest");
}
}
// See comment on Focus_BasicTabTest() for details on test mechanism.
[TestMethod]
[TestProperty("TestSuite", "A")]
public void Focus_ReverseTabTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("Focus_ReverseTabTest");
Button x1 = new Button(FindElement.ById("TabStopButton1")); // Xaml TabStop 1
Button x2 = new Button(FindElement.ById("TabStopButton2")); // Xaml TabStop 2
Log.Comment("Description: x2 shift-> w2 shift-> w1 shift-> x1");
Log.Comment("Focus on x2");
x2.SetFocus();
Wait.ForIdle();
Verify.IsTrue(x2.HasKeyboardFocus, "TabStopButton2 has keyboard focus");
Log.Comment("Shift+Tab x2 -> w2");
KeyboardHelper.PressKey(Key.Tab, ModifierKey.Shift);
WaitForFocus("w2");
Log.Comment("Shift+Tab w2 -> w1");
KeyboardHelper.PressKey(Key.Tab, ModifierKey.Shift);
WaitForFocus("w1");
Log.Comment("Shift+Tab w1 -> x1");
using (var xamlFocusWaiter = new FocusAcquiredWaiter("TabStopButton1"))
{
KeyboardHelper.PressKey(Key.Tab, ModifierKey.Shift);
xamlFocusWaiter.Wait();
Log.Comment("Focus is on " + UIObject.Focused);
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
}
CompleteTestAndWaitForResult("Focus_ReverseTabTest");
}
}
// See comment on Focus_BasicTabTest() for details on test mechanism.
[TestMethod]
[TestProperty("TestSuite", "A")]
public void Focus_BackAndForthTabTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("Focus_BackAndForthTabTest");
Button x1 = new Button(FindElement.ById("TabStopButton1")); // Xaml TabStop 1
Log.Comment("Description: x1 -> w1 shift-> x1 -> w1 -> x1");
Log.Comment("Focus on x1");
x1.SetFocus();
Wait.ForIdle();
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
Log.Comment("Tab x1 -> w1");
KeyboardHelper.PressKey(Key.Tab);
WaitForFocus("w1");
Log.Comment("Shift+Tab w1 -> x1");
using (var xamlFocusWaiter = new FocusAcquiredWaiter("TabStopButton1"))
{
KeyboardHelper.PressKey(Key.Tab, ModifierKey.Shift);
xamlFocusWaiter.Wait();
Log.Comment("Focus is on " + UIObject.Focused);
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
}
Log.Comment("Tab x1 -> w1");
KeyboardHelper.PressKey(Key.Tab);
WaitForFocus("w1");
Log.Comment("Shift+Tab w1 -> x1");
using (var xamlFocusWaiter = new FocusAcquiredWaiter("TabStopButton1"))
{
KeyboardHelper.PressKey(Key.Tab, ModifierKey.Shift);
xamlFocusWaiter.Wait();
Log.Comment("Focus is on " + UIObject.Focused);
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
}
CompleteTestAndWaitForResult("Focus_BackAndForthTabTest");
}
}
// See comment on Focus_BasicTabTest() for details on test mechanism.
[TestMethod]
[TestProperty("TestSuite", "A")]
public void Focus_MouseActivateTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("Focus_MouseActivateTest");
Button x1 = new Button(FindElement.ById("TabStopButton1")); // Xaml TabStop 1
Log.Comment("Test1: Focus on x1, Click on w1, Shift+Tab w2 -> w1, Click on x2");
Log.Comment("Focus on x1");
x1.SetFocus();
Wait.ForIdle();
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
Log.Comment("Click on w2");
Rectangle bounds = FindElement.ById("MyWebView2").BoundingRectangle;
const int w2_offsetX = 100;
const int w2_offsetY = 200;
// top left coordinate (location of web button) relative from the center of webview
InputHelper.MoveMouse(FindElement.ById("MyWebView2"), -bounds.Width / 2 + w2_offsetX, -bounds.Height / 2 + w2_offsetY);
PointerInput.Press(PointerButtons.Primary);
PointerInput.Release(PointerButtons.Primary);
WaitForFocus("w2");
Log.Comment("Shift+Tab w2 -> w1");
KeyboardHelper.PressKey(Key.Tab, ModifierKey.Shift);
WaitForFocus("w1");
Log.Comment("Click on x1");
using (var xamlFocusWaiter = new FocusAcquiredWaiter("TabStopButton1"))
{
InputHelper.MoveMouse(x1, 10, 10);
PointerInput.Press(PointerButtons.Primary);
PointerInput.Release(PointerButtons.Primary);
xamlFocusWaiter.Wait();
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
}
CompleteTestAndWaitForResult("Focus_MouseActivateTest");
}
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void ExecuteScriptTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("ExecuteScriptTest");
CompleteTestAndWaitForResult("ExecuteScriptTest");
}
}
[TestMethod]
[TestProperty("TestSuite", "A")]
public void MultipleWebviews_FocusTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("MultipleWebviews_FocusTest");
Button x1 = new Button(FindElement.ById("TabStopButton1")); // Xaml TabStop 1
Button x2 = new Button(FindElement.ById("TabStopButton2")); // Xaml TabStop 2
UIObject webviewControl = new UIObject(FindElement.ById("MyWebView2"));
UIObject webviewControlB = new UIObject(FindElement.ById("MyWebView2B"));
Log.Comment("Description: first tabstop1 -> webview1 -> second tabstop2 -> WebView2");
Log.Comment("Focus on tabstop1");
x1.SetFocus();
Wait.ForIdle();
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");
Log.Comment("Tab tabstop1 -> MyWebView2");
KeyboardHelper.PressKey(Key.Tab);
WaitForFocus("w1");
Log.Comment("Tab to move to next element within first webview page");
KeyboardHelper.PressKey(Key.Tab);
Log.Comment("Tab MyWebView2 -> tabstop2");
using (var xamlFocusWaiter = new FocusAcquiredWaiter("TabStopButton2"))
{
KeyboardHelper.PressKey(Key.Tab);
xamlFocusWaiter.Wait();
Verify.IsTrue(x2.HasKeyboardFocus, "TabStopButton2 has keyboard focus");
}
Log.Comment("Tab tabstop2 -> MyWebView2B");
KeyboardHelper.PressKey(Key.Tab);
WaitForFocus("w1B");
using (var xamlFocusWaiter = new FocusAcquiredWaiter("TabStopButton2"))
{
KeyboardHelper.PressKey(Key.Tab, ModifierKey.Shift);
xamlFocusWaiter.Wait();
Verify.IsTrue(x2.HasKeyboardFocus, "TabStopButton2 has keyboard focus");
}
CompleteTestAndWaitForResult("MultipleWebviews_FocusTest");
}
}
[TestMethod]
[TestProperty("TestSuite", "B")]
public void MultipleWebviews_BasicRenderingTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("MultipleWebviews_BasicRenderingTest");
CompleteTestAndWaitForResult("MultipleWebviews_BasicRenderingTest");
Wait.ForIdle();
ElementCache.Clear();
WebView2Temporary.WebView2RenderingVerifier.VerifyInstances("MultipleWebviews_BasicRenderingTest");
}
}
[TestMethod]
[TestProperty("TestSuite", "B")]
[TestProperty("Ignore", "True")] // TODO: Investigate why LanguageTest is failing on latest WebView2 runtime
public void MultipleWebviews_LanguageTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("MultipleWebviews_LanguageTest");
CompleteTestAndWaitForResult("MultipleWebviews_LanguageTest");
}
}
// CopyPasteTest tests basic copy and pasting scenarios involving the WebView2 control as follows:
// 1) Copying from text housed in a XAML control in the app to the webview's textbox.
// 2) [Xaml text -> WebView2] After overwriting the clipboard's DataPackage, verify successful 'copy to webview'
// by copying from the textbox and pasting to an external Xaml textbox for verification.
// 3) [WebView2 text -> Xaml] Overwrite webview textbox's contents and attempt to copy this out to
// the Xaml app to provide an isolated 'copy from webview' test.
[TestMethod]
[TestProperty("TestSuite", "B")]
[TestProperty("Ignore", "True")] // Task 37000273
public void CopyPasteTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("CopyPasteTest");
var CopyPasteTextBox1 = new Edit(FindElement.ById("CopyPasteTextBox1"));
var CopyPasteTextBox2 = new Edit(FindElement.ById("CopyPasteTextBox2"));
// Copy text to SimpleInputPage's text box page.
Log.Comment("Copy and paste text to SimpleInputPage's text box page...");
CopyPasteTextBox1.SetFocus();
DoSelectAllByKeyboard();
CopySelected();
SetWebViewElementFocus("w2", 2);
// Do ctrl+V to paste into html.
PasteClipboard();
using (var clearClipboardWaiter = new ValueChangedEventWaiter(CopyPasteTextBox2, "Clear"))
{
// "Clear" clipboard by copying text between TextBoxes on Xaml page.
Log.Comment("Clear clipboard by copying text between TextBoxes on Xaml page...");
CopyPasteTextBox1.SetValue("Clear");
CopyPasteTextBox1.SetFocus();
Wait.ForIdle();
Verify.IsTrue(CopyPasteTextBox1.HasKeyboardFocus);
DoSelectAllByKeyboard();
CopySelected();
CopyPasteTextBox2.SetFocus();
Wait.ForIdle();
Verify.IsTrue(CopyPasteTextBox2.HasKeyboardFocus);
DoSelectAllByKeyboard();
PasteClipboard();
clearClipboardWaiter.Wait();
}