我的网络项目“CalculatorWeb”中有一个网页,我在其中放置了一个 ID 为“txtNum1”的文本框</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>
没什么特别的,所以页面加载如下
现在我为我的项目“Calculator.Specs”创建了一个功能文件“BrowserTest1.feature”。代码如下
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 集成所需的所有参考资料
当我运行单元测试时,网页加载正常,但是当我尝试查找 ID 为“txtNum1”的元素时,即使页面上确实存在具有该 ID 的文本框,我也会遇到失败的测试
异常详情如下所示
任何人都可以指出我缺少什么以使 Selenium 在页面上找到 ID 为“txtNum1”的文本框的正确方向</p>