3

Web プロジェクト「CalculatorWeb」に、ID「txtNum1」のテキスト ボックスを 1 つ配置した Web ページがあります。</p>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="frmCalc">
        <div style="padding-top:20px;">
            My Sample Text Box <br/><br/>
            <input id="txtNum1" type="text" />
        </div>
    </form>
</body>
</html>

派手なものは何もないので、ページは以下のように読み込まれます

Web ページのプレビュー

ここで、機能ファイル「BrowserTest1.feature」をプロジェクト「Calculator.Specs」に作成しました。コードは次のとおりです

Feature: BrowserTest1
In order to check url
As browser
I want to be see url of page

@mytag
Scenario: Go to URL
    Given I have entered 'http://localhost:58529/'
    When I go to that url
    Then there is a text box on the page with id 'txtNum1'

次に、ユニット テストの実装用にファイル BrowserTest.cs をコーディングします。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using TechTalk.SpecFlow;

namespace Calculator.Specs
{
    [Binding]
    class BrowserTest
    {

        public string urlPath;


        [Given(@"I have entered 'http://localhost:58529/'")]
        protected void SetURL()
        {
            string URLPath = "http://localhost:58529/";
            urlPath = URLPath;
        }

        [When(@"I go to that url")]
        protected void NavigateToURL()
        {
             using (var driver = new ChromeDriver())
             {
                 driver.Navigate().GoToUrl(urlPath);
             }
        }


        [Then(@"there is a text box on the page with id 'txtNum1'")]
        protected void TxtBoxExists()
        {
            bool txtExists = false;
            using (var driver = new ChromeDriver())
            {
                IWebElement txtBox1 = driver.FindElement(By.Id("txtNum1"));

                if (txtBox1 != null)
                {
                    txtExists = true;
                }
            }
            Assert.AreEqual(true, txtExists);
        }
    }
}

私の知る限り、Visual Studio IDE から SpecFlow/Selenium への統合に必要な参照はすべて揃っています。

参考文献セクション

単体テストを実行すると、Web ページは正常に読み込まれますが、ID が「txtNum1」のテキスト ボックスがページに存在するにもかかわらず、ID が「txtNum1」の要素を見つけようとすると、テストに失敗します。

例外メッセージ

例外の詳細は次のように表示されます

例外の詳細

IDが「txtNum1」のページでSeleniumがテキストボックスを見つけられるようにするために何が欠けているかについて、誰かが私を正しい方向に向けることができますか</p>

4

3 に答える 3

1

ドライバーは少しゆっくり進む必要があります

driver.FindElementたとえば、次のように、各の前に遅延を追加します。

Thread.Sleep(2000);
IWebElement txtBox1 = driver.FindElement(By.Id("txtNum1"));

またはさらに良い:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("txtNum1")));
IWebElement txtBox1 = driver.FindElement(By.Id("txtNum1"));
于 2014-11-04T14:01:21.617 に答える
0

少しコードを調べた後、答えが見つかりました。この問題は、コード内の ChromeDriver の複数のインスタンスに関係していました。したがって、最初のインスタンスはページをロードしていましたが、ページ上のアイテムを見つけようとしたときに、ページのロードに既に使用されていたページの新しいインスタンスを使用していました。そのため、ページがロードされていない chrome ドライバーのこの 2 番目のインスタンスは、この chrome ドライバーのインスタンスにページがロードされていないため、アイテムが見つからないという例外を常に返していました。

于 2014-11-04T15:41:45.540 に答える