使用 Specflow 进行简单的 Google 搜索

这是在 Google 中搜索的简单示例。它包含两部分,

  1. 特征文件
  2. 步骤定义文件

由于代码本身是自我解释的,因此我不会详细介绍。

特征文件

Feature:Google  Key word search

@mytag

Scenario: search Spec Flow in Google search bar
Given I have entered the Google Home page
And I have entered spec flow into google search bar
When I press search button
Then the result should be a new pages with results for spec flow

步骤定义文件

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using TechTalk.SpecFlow;
using static NUnit.Core.NUnitFramework;

namespace GoogleSearch.GoogleSearch
{
    [Binding]
    public class GoogleKeyWordSearchSteps
    {
          IWebDriver driver = new FirefoxDriver();
        [Given(@"I have entered the Google Home page")]
        public void GivenIHaveEnteredTheGoogleHomePage()
        {
            driver.Navigate().GoToUrl("https://www.google.co.nz");
        }
        
        [Given(@"I have entered spec flow into google search bar")]
        public void GivenIHaveEnteredSpecFlowIntoGoogleSearchBar()
        {
        driver.FindElement(By.XPath("/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[1]/div[3]/div/div[3]/div/input[1]")).SendKeys("Spec Flow");
        }
        
        [When(@"I press search button")]
        public void WhenIPressSearchButton()
        {
        driver.FindElement(By.XPath("/html/body/div/div[3]/form/div[2]/div[3]/center/input[1]")).Click();
        }
        
        [Then(@"the result should be a new pages with results for spec flow")]
        public void ThenTheResultShouldBeANewPagesWithResultsForSpecFlow()
        {
            //  Assert.AreEqual("Google", driver.Title);
        }
    }
}