我在我的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