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>
派手なものは何もないので、ページは以下のように読み込まれます
ここで、機能ファイル「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>