0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WinAppDriver用C#スクリプトの書き方メモ

Last updated at Posted at 2019-04-09

WinAppDriverを使ってWindowsフォームアプリの自動制御する時の覚え書き。

Windowsフォームだからか日本語環境絡みなのか、Nameプロパティでオブジェクトを特定する場合は、FindElementByName()ではなく、FindElementByAccessibilityId()を使う必要がありました。逆に、FindElementByName()で読まれるのは、AccessibleNameプロパティのようです。FormデザイナーでAccessibleNameに適切な日本語記述がしてある場合、そちらを使うとテストコードが少し見やすくなるかも知れないですね。でもまぁ自分のアプリを評価する分にはコードと同じNameプロパティでWindowsElementオブジェクトを作って使う方が素直かも知れません。

private static WindowsElement TextBox1;
private static WindowsElement Button1;
private static WindowsElement CheckBox1;

[ClassInitialize]
public static void ClassInitialize(TestContext context) {
            // Create session to launch a Calculator window
            Setup(context);

           //オブジェクトのNameプロパティで選択
           TextBox1= session.FindElementByAccessibilityId("TextBox1"); 
           CheckBox1= session.FindElementByAccessibilityId("CheckBox1"); 

           //AccessibleNameプロパティで選択
           Button1= session.FindElementByName("ボタン1"); 
}

[TestMethod]
public void Test() {
            //ボタンをクリック
            Button1.Click();

            //テキストボックスに文字入力
            TextBox1.SendKeys("あいうえお"); //キー入力

            //スリープ(文字入力後は少し待たないと、.Textプロパティの戻り値が途中経過みたいになってFailするぽい
            Thread.Sleep(TimeSpan.FromSeconds(1));

            //テキストボックスの内容が期待通りか確認
            Assert.AreEqual("あいうえお", TextBox1.Text);

            //チェックボックスの状態は.Selectedプロパティ
            Assert.IsTrue(CheckBox1.Selected); //チェックされてればパス
            CheckBox1.Click(); //クリックして状態を変更
            Assert.IsTrue(CheckBox1.Selected); //ここでフェイル

            //その他、inspect.exeで取得できるプロパティは.GetAttribute()メソッドを使う
            Assert.AreEqual("あいうえお", TextBox1.GetAttribute("Value.Value").ToString());

        }

テキストボックスコントールはこれで取得/検証できたんですが、Labelコントロールだと表示内容はinspect.exeの画面ではどこにも出ていませんので、現状お手上げです。結果をLabelで出している場合、チェックができません。どなたか情報をお持ちでしたら是非コメントいただけれると有り難いです。

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?