3

我的网络项目“CalculatorWeb”中有一个网页,我在其中放置了一个 ID 为“txtNum1”的文本框</p>

<!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>

没什么特别的,所以页面加载如下

网页预览

现在我为我的项目“Calculator.Specs”创建了一个功能文件“BrowserTest1.feature”。代码如下

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'

然后我为单元测试实现编写一个文件 BrowserTest.cs

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);
        }
    }
}

据我所知,我确实拥有 Visual Studio IDE 到 SpecFlow/Selenium 集成所需的所有参考资料

参考资料部分

当我运行单元测试时,网页加载正常,但是当我尝试查找 ID 为“txtNum1”的元素时,即使页面上确实存在具有该 ID 的文本框,我也会遇到失败的测试

异常消息

异常详情如下所示

异常详情

任何人都可以指出我缺少什么以使 Selenium 在页面上找到 ID 为“txtNum1”的文本框的正确方向</p>

4

3 回答 3

2

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

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

另一种选择是将驱动程序对象存储在ScenarioContext.Current(或FeatureContext.Current)中。这些是字典,因此您可以这样做:

ScenarioContext.Current["webDriver"] = driver;

这可以正常工作,但是每次取出驱动程序时都必须强制转换,因为字典只接受对象,但是无论它们在哪个文件中,您都可以在步骤之间共享驱动程序实例。

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

可以在此处的答案中看到设置此设置的示例

于 2014-11-04T14:24:47.187 回答
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"));
于 2014-11-04T14:01:21.617 回答
0

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

于 2014-11-04T15:41:45.540 回答