2014-11-04 75 views
3

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

没什么特别所以页面有一个网页时,负载高达如下

Web Page Preview

现在,我创建了一个功能文件“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 /硒整合

References Section

当我运行单元测试网页加载了罚款,但我得到一个失败的测试时,我试图找到ID为“txtNum1”即使与该ID的文本框并在页面

Exception Message

异常详细信息如下所示

Exception Details

任何人可以点上存在的元素我在正确的方向,因为我错过了什么,使Selenium找到ID为“txtNum1”页面上的文本框

+1

只是第一个猜测 - 不是驱动程序共享脚步? – 2014-11-04 13:54:06

回答

1

您需要使用步骤之间的驱动程序的同一个实例,否则驱动程序导航到页面与您试图从中获取元素的驱动程序不同。

你有几个选择。最简单的是为您的驱动程序实例创建一个字段,并在所有步骤中使用该字段。只要你的所有步骤都在同一个文件中,这将工作。

另一个选项是将驱动程序对象存储在ScenarioContext.Current(或FeatureContext.Current)中。这是字典,所以你可以这样做:

ScenarioContext.Current["webDriver"] = driver; 

这将好的工作,但你必须投每次你让司机出来作为字典只是需要的对象,但你就可以分享您的驱动程序实例之间的步骤,无论他们在哪个文件中。

第三个(和恕我直言最好)选项是创建一个WebDriverContext类,并在您的步骤类构造函数中接受此类。在那里创建您的驱动程序(在其构造函数中),然后每个步骤都可以使用WebDriverContext实例访问共享驱动程序。

设置此功能的例子可以在answer here

+0

Yeap就是那个。对网络驱动程序的共享实例设置很好 – Asif 2014-11-04 15:43:51

1

您的驱动程序只需要进行一点点慢

每个driver.FindElement之前添加一些延迟,例如有:

Thread.Sleep(2000); 
IWebElement txtBox1 = driver.FindElement(By.Id("txtNum1")); 

或者更好的是:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,10)); 
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("txtNum1"))); 
IWebElement txtBox1 = driver.FindElement(By.Id("txtNum1")); 
+0

感谢您的方便提示。当我们有一个大的页面加载时,这会非常有帮助,并且我们希望整个页面先加载,然后再搜索它上面的一个项目。虽然我的特殊问题是由于chrome驱动程序的额外实例。一个实例是加载页面,而另一个“using”语句用于检查页面上的项目。由于第二个实例没有加载页面,因此它总是返回没有找到的项目。 – Asif 2014-11-04 14:32:24

0

搜了一下代码检查后回答中可以看出。这个问题是与代码中的多个ChromeDriver实例有关。所以第一个实例是加载页面,但是当我试图在页面上找到一个项目时,我正在使用页面的新实例,而不是已经用于加载页面的实例。因此,没有加载页面的chrome驱动程序的第二个实例总是返回异常,但没有找到任何项目,因为在该驱动程序的此实例中没有加载页面

相关问题