我在我的web项目“CalculatorWeb”里我把一个文本框ID为“txtNum1”“没有这样的元素”异常使用Selenium用C#
<!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'
我再为单元测试执行
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);
}
}
}
代码的文件BrowserTest.cs据我所知,我有所有需要引用的Visual Studio IDE到SpecFlow /硒整合
当我运行单元测试网页加载了罚款,但我得到一个失败的测试时,我试图找到ID为“txtNum1”即使与该ID的文本框并在页面
异常详细信息如下所示
任何人可以点上存在的元素我在正确的方向,因为我错过了什么,使Selenium找到ID为“txtNum1”页面上的文本框
只是第一个猜测 - 不是驱动程序共享脚步? – 2014-11-04 13:54:06