LoginSignup
0
0

More than 3 years have passed since last update.

Microsoftの「UI test with Selenium」にあるサンプルが動かない2

Posted at

0.はじめに

以前(Microsoftの「UI test with Selenium」にあるサンプルが動かない)の続き。

1.調べたこと

以前の記事の最後にある、わからなかったことを調べた。
わからなかったことは、2点。

(1) コピってきたコードがよくわからない

executor.ExecuteScript("arguments[0].click();", element);

これの、arguments[0]ってのがイマイチよくわからない。
・・・・
このarguments[0]っていうのは、Javascriptの話だった。
引数のことを言っているらしい。なので、arguments[0]ってのは、elementのこと。
一応、簡単なテストをやってみた。

test.html
<html>
    <head>
        <title>テスト用HTML</title>
    </head>
    <body>
        <button type="button" onclick="alert('アラートメッセージ1')" id="test_element1">ボタン</button>
        <button type="button" onclick="alert('アラートメッセージ2')" id="test_element2">ボタン</button>
        <button type="button" onclick="alert('アラートメッセージ3')" id="test_element3">ボタン</button>
    </body>
</html>
ExecuteScriptTest
        [TestMethod]
        [TestCategory("Chrome")]
        public void ExecutorTest()
        {
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            //
            // ローカルのテスト用HTMLにアクセス
            //
            driver.Navigate().GoToUrl("file:///C:..(略).../test.html");
            //
            // ボタン1
            //
            IWebElement element1 = driver.FindElement(By.Id("test_element1"));
            IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
            //
            // ボタン2
            //
            IWebElement element2 = driver.FindElement(By.Id("test_element2"));
            IJavaScriptExecutor executor2 = (IJavaScriptExecutor)driver;
            //
            // ボタン1を選択
            //
            executor.ExecuteScript("arguments[0].click();", element1, element2);
            //
            // アラート内容の確認
            //
            var alert = driver.SwitchTo().Alert();
            Assert.IsTrue(alert.Text.Equals("アラートメッセージ1"), "Verified message of the alert");
            //
            // アラートを閉じる
            //
            alert.Accept();
            //
            // ボタン2をクリック
            //
            executor.ExecuteScript("arguments[1].click();", element1, element2);
            //
            // アラート内容の確認
            //
            alert = driver.SwitchTo().Alert();
            Assert.IsTrue(alert.Text.Equals("アラートメッセージ2"), "Verified message of the alert");
            //
            // アラートを閉じる
            //
            alert.Accept();
        }

ExecuteScriptの引数を2つにしたら、arguments[0]には1つ目に引数が、arguments[1]には2つめの引数が入るってことだね。

(2) デバッグじゃないと成功しない
予想通りwaitかけたらうまくいった。

        [TestMethod]
        [TestCategory("Chrome")]
        public void TheBingSearchTest()
        {
            //
            // Bingのページを表示
            //
            driver.Navigate().GoToUrl(appURL + "/");
            //
            // 検索窓に「Azure Pipelines」と入力
            //
            driver.FindElement(By.Id("sb_form_q")).SendKeys("Azure Pipelines");
            //
            // 検索をクリック
            //
            IWebElement element = driver.FindElement(By.Id("sb_form_go"));
            IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
            executor.ExecuteScript("arguments[0].click();", element);
            //
            // タイトルに「Azure Pipelines」が入っているページのうち、
            // 検索結果の先頭にあるものを選択
            //
            var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
            var findElement = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(
                            By.XPath("//ol[@id='b_results']/li/h2/a[contains(text(), 'Microsoft Azure')]")));
            findElement.Click();
            //
            // ページのタイトルに「Azure Pipelines」が含まれていたらテスト成功
            //
            Assert.IsTrue(driver.Title.Contains("Azure Pipelines"), "Verified title of the page");
        }

ここにたどり着くまでにいろいろ調べて、ちょっとはだけSeleniumに慣れた気がする。。。。
あとはデザインパターンとかいろいろ勉強して、Azure DevOpsとかに組み込むとこまでやりたいな・・・・

0
0
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
0