【发布时间】:2014-11-04 13:51:27
【问题描述】:
我的网络项目“CalculatorWeb”中有一个网页,我在其中放置了一个 ID 为“txtNum1”的文本框
<!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”的文本框的正确方向
【问题讨论】:
-
只是初步猜测 - 不应该在步骤之间共享
driver?
标签: c# selenium bdd specflow nosuchelementexception