I have a web page in my web project “CalculatorWeb” where I placed a single text box with 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>
Nothing fancy so the page loads up as below
Now I created a feature file “BrowserTest1.feature” to my project “Calculator.Specs”. The code as follows
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'
I then code a file BrowserTest.cs for unit tests implementation
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);
}
}
}
As far as I am aware I do have all the required references for Visual Studio IDE to SpecFlow/Selenium integration
When I do run the unit test the web page loads up fine , however I get a failing test when I try to find the element with ID “txtNum1” even though the text box with that ID does exist on the page
Exception details appear as follows
Can anybody point me in the right direction as to what am I missing to enable Selenium to find the text box on the page with ID “txtNum1”