2014-11-04 74 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驅動程序的第二個實例總是返回異常,但沒有找到任何項目,因爲在該驅動程序的此實例中沒有加載頁面

相關問題