回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,143 +1,72 @@
|
|
1
1
|
一応これで動きましたがごまかしがあります。
|
2
2
|
|
3
|
-
|
4
|
-
|
5
3
|
* 本来`NormalOutput`が欲しかったが`FindAll`でとれない(`TreeWalker`が必要)のと、Nameに方向制御コードがついていて面倒だったので`CalculatorResults`で^^;
|
6
|
-
|
7
4
|
* 「キーボードからの文字入力」のほうは、`SendKeys`は使えるがカッコは認識しない
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
```
|
6
|
+
```cs
|
12
|
-
|
13
7
|
using System;
|
14
|
-
|
15
8
|
using System.Collections.Generic;
|
16
|
-
|
17
9
|
using System.Diagnostics;
|
18
|
-
|
19
10
|
using System.Linq;
|
20
|
-
|
21
11
|
using System.Threading;
|
22
|
-
|
23
12
|
using System.Windows.Automation;
|
24
13
|
|
25
|
-
|
26
|
-
|
27
14
|
namespace Questions263202
|
28
|
-
|
29
15
|
{
|
30
|
-
|
31
16
|
internal class Program
|
32
|
-
|
33
17
|
{
|
34
|
-
|
35
18
|
private static void Main()
|
36
|
-
|
37
19
|
{
|
38
|
-
|
39
20
|
var process = Process.Start("calc");
|
40
|
-
|
41
21
|
try
|
42
|
-
|
43
22
|
{
|
44
|
-
|
45
23
|
Thread.Sleep(1000);
|
46
24
|
|
47
|
-
|
48
|
-
|
49
25
|
// UWPだといろいろあるようなので、Processをタイトルから取り直す
|
50
|
-
|
51
26
|
foreach(var p in Process.GetProcesses())
|
52
|
-
|
53
27
|
{
|
54
|
-
|
55
28
|
if(p.MainWindowTitle.Contains("電卓"))
|
56
|
-
|
57
29
|
{
|
58
|
-
|
59
30
|
process = p;
|
60
|
-
|
61
31
|
break;
|
62
|
-
|
63
32
|
}
|
64
|
-
|
65
33
|
}
|
66
34
|
|
35
|
+
var mainForm = AutomationElement.FromHandle(process.MainWindowHandle);
|
36
|
+
var btnClear = FindElementsByName(mainForm, "クリア").First()
|
37
|
+
.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
|
38
|
+
btnClear.Invoke();
|
67
39
|
|
68
|
-
|
69
|
-
var mainForm = AutomationElement.FromHandle(process.MainWindowHandle);
|
70
|
-
|
71
|
-
var btn
|
40
|
+
var btn7 = FindButtonsByName(mainForm, "7").First()
|
72
|
-
|
73
41
|
.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
|
74
42
|
|
75
|
-
btnClear.Invoke();
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
var btn7 = FindButtonsByName(mainForm, "7").First()
|
80
|
-
|
81
|
-
.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
|
82
|
-
|
83
|
-
|
84
|
-
|
85
43
|
foreach(var _ in Enumerable.Repeat(0, 7))
|
86
|
-
|
87
44
|
{
|
88
|
-
|
89
45
|
btn7.Invoke();
|
90
|
-
|
91
46
|
}
|
92
47
|
|
93
|
-
|
94
|
-
|
95
48
|
var resultArea = FindElementById(mainForm, "CalculatorResults");
|
96
|
-
|
97
49
|
var EXPECTED_VALUE = "表示は 7,777,777 です";
|
98
|
-
|
99
50
|
var actualValue = resultArea.Current.Name;
|
100
|
-
|
101
51
|
Console.WriteLine("期待値 {0} に対して、結果値は {1} です", EXPECTED_VALUE, actualValue);
|
102
|
-
|
103
52
|
Console.WriteLine("テスト結果は {0} です", EXPECTED_VALUE == actualValue ? "OK" : "NG");
|
104
53
|
|
105
|
-
|
106
|
-
|
107
54
|
Console.ReadKey();
|
108
|
-
|
109
55
|
}
|
110
|
-
|
111
56
|
finally
|
112
|
-
|
113
57
|
{
|
114
|
-
|
115
58
|
process.CloseMainWindow();
|
116
|
-
|
117
59
|
}
|
118
|
-
|
119
60
|
}
|
120
61
|
|
121
|
-
|
122
|
-
|
123
62
|
private static AutomationElement FindElementById(AutomationElement rootElement, string automationId)
|
124
|
-
|
125
63
|
=> rootElement.FindFirst(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
|
126
64
|
|
127
|
-
|
128
|
-
|
129
65
|
private static IEnumerable<AutomationElement> FindElementsByName(AutomationElement rootElement, string name)
|
130
|
-
|
131
66
|
=> rootElement.FindAll(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, name)).Cast<AutomationElement>();
|
132
67
|
|
133
|
-
|
134
|
-
|
135
68
|
private static IEnumerable<AutomationElement> FindButtonsByName(AutomationElement rootElement, string name)
|
136
|
-
|
137
69
|
=> FindElementsByName(rootElement, name).Where(x => x.Current.ClassName == "Button");
|
138
|
-
|
139
70
|
}
|
140
|
-
|
141
71
|
}
|
142
|
-
|
143
72
|
```
|