teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/21 14:07

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,72 +1,72 @@
1
- 一応これで動きましたがごまかしがあります。
2
-
3
- * 本来`NormalOutput`が欲しかったが`FindAll`でとれない(`TreeWalker`が必要)のと、Nameに方向制御コードがついていて面倒だったので`CalculatorResults`で^^;
4
- * 「キーボードからの文字入力」のほうは、`SendKeys`は使えるがカッコは認識しない
5
-
6
- ```C#
7
- using System;
8
- using System.Collections.Generic;
9
- using System.Diagnostics;
10
- using System.Linq;
11
- using System.Threading;
12
- using System.Windows.Automation;
13
-
14
- namespace Questions263202
15
- {
16
- internal class Program
17
- {
18
- private static void Main()
19
- {
20
- var process = Process.Start("calc");
21
- try
22
- {
23
- Thread.Sleep(1000);
24
-
25
- // UWPだといろいろあるようなので、Processをタイトルから取り直す
26
- foreach(var p in Process.GetProcesses())
27
- {
28
- if(p.MainWindowTitle.Contains("電卓"))
29
- {
30
- process = p;
31
- break;
32
- }
33
- }
34
-
35
- var mainForm = AutomationElement.FromHandle(process.MainWindowHandle);
36
- var btnClear = FindElementsByName(mainForm, "クリア").First()
37
- .GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
38
- btnClear.Invoke();
39
-
40
- var btn7 = FindButtonsByName(mainForm, "7").First()
41
- .GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
42
-
43
- foreach(var _ in Enumerable.Repeat(0, 7))
44
- {
45
- btn7.Invoke();
46
- }
47
-
48
- var resultArea = FindElementById(mainForm, "CalculatorResults");
49
- var EXPECTED_VALUE = "表示は 7,777,777 です";
50
- var actualValue = resultArea.Current.Name;
51
- Console.WriteLine("期待値 {0} に対して、結果値は {1} です", EXPECTED_VALUE, actualValue);
52
- Console.WriteLine("テスト結果は {0} です", EXPECTED_VALUE == actualValue ? "OK" : "NG");
53
-
54
- Console.ReadKey();
55
- }
56
- finally
57
- {
58
- process.CloseMainWindow();
59
- }
60
- }
61
-
62
- private static AutomationElement FindElementById(AutomationElement rootElement, string automationId)
63
- => rootElement.FindFirst(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
64
-
65
- private static IEnumerable<AutomationElement> FindElementsByName(AutomationElement rootElement, string name)
66
- => rootElement.FindAll(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, name)).Cast<AutomationElement>();
67
-
68
- private static IEnumerable<AutomationElement> FindButtonsByName(AutomationElement rootElement, string name)
69
- => FindElementsByName(rootElement, name).Where(x => x.Current.ClassName == "Button");
70
- }
71
- }
1
+ 一応これで動きましたがごまかしがあります。
2
+
3
+ * 本来`NormalOutput`が欲しかったが`FindAll`でとれない(`TreeWalker`が必要)のと、Nameに方向制御コードがついていて面倒だったので`CalculatorResults`で^^;
4
+ * 「キーボードからの文字入力」のほうは、`SendKeys`は使えるがカッコは認識しない
5
+
6
+ ```cs
7
+ using System;
8
+ using System.Collections.Generic;
9
+ using System.Diagnostics;
10
+ using System.Linq;
11
+ using System.Threading;
12
+ using System.Windows.Automation;
13
+
14
+ namespace Questions263202
15
+ {
16
+ internal class Program
17
+ {
18
+ private static void Main()
19
+ {
20
+ var process = Process.Start("calc");
21
+ try
22
+ {
23
+ Thread.Sleep(1000);
24
+
25
+ // UWPだといろいろあるようなので、Processをタイトルから取り直す
26
+ foreach(var p in Process.GetProcesses())
27
+ {
28
+ if(p.MainWindowTitle.Contains("電卓"))
29
+ {
30
+ process = p;
31
+ break;
32
+ }
33
+ }
34
+
35
+ var mainForm = AutomationElement.FromHandle(process.MainWindowHandle);
36
+ var btnClear = FindElementsByName(mainForm, "クリア").First()
37
+ .GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
38
+ btnClear.Invoke();
39
+
40
+ var btn7 = FindButtonsByName(mainForm, "7").First()
41
+ .GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
42
+
43
+ foreach(var _ in Enumerable.Repeat(0, 7))
44
+ {
45
+ btn7.Invoke();
46
+ }
47
+
48
+ var resultArea = FindElementById(mainForm, "CalculatorResults");
49
+ var EXPECTED_VALUE = "表示は 7,777,777 です";
50
+ var actualValue = resultArea.Current.Name;
51
+ Console.WriteLine("期待値 {0} に対して、結果値は {1} です", EXPECTED_VALUE, actualValue);
52
+ Console.WriteLine("テスト結果は {0} です", EXPECTED_VALUE == actualValue ? "OK" : "NG");
53
+
54
+ Console.ReadKey();
55
+ }
56
+ finally
57
+ {
58
+ process.CloseMainWindow();
59
+ }
60
+ }
61
+
62
+ private static AutomationElement FindElementById(AutomationElement rootElement, string automationId)
63
+ => rootElement.FindFirst(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
64
+
65
+ private static IEnumerable<AutomationElement> FindElementsByName(AutomationElement rootElement, string name)
66
+ => rootElement.FindAll(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, name)).Cast<AutomationElement>();
67
+
68
+ private static IEnumerable<AutomationElement> FindButtonsByName(AutomationElement rootElement, string name)
69
+ => FindElementsByName(rootElement, name).Where(x => x.Current.ClassName == "Button");
70
+ }
71
+ }
72
72
  ```