A solution of "Seach something on Google" in C# using Specflow

First, create a feature

Feature: GoogleSearch
@mytag
Scenario: Search in Google for something
Given I am in the Google Home
And I have entered  'selenium'
When I press Enter button
Then the result should be displayed with 'selenium' page

Secondly, generate step definitions of the feature.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using TechTalk.SpecFlow;

namespace GoogleSearch
{
    [Binding]
    public class GoogleSearchSteps
    {
        private IWebDriver driver;

        [Given(@"I am in the Google Home")]
        public void GivenIAmInTheGoogleHome()
        {
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.google.co.nz/");
        }
     
        [Given(@"I have entered  '(.*)'")]
        public void GivenIHaveEntered(string p0)
        {
            driver.FindElement(By.XPath(".//*[@id='lst-ib']")).SendKeys(p0);
        }
     
        [When(@"I press Enter button")]
        public void WhenIPressEnterButton()
        {
            driver.FindElement(By.XPath(".//*[@id='lst-ib']")).SendKeys(Keys.Enter);
        }
     
        [Then(@"the result should be displayed with '(.*)' page")]
        public void ThenTheResultShouldBeDisplayedWithPage(string p0)
        {
            //var pageTitle = driver.Title;
            //var expectedTitle = p0 + " - Google 搜索";
            if (driver.Title == p0 + " - Google 搜索")//if (pageTitle==expectedTitle)
                Console.WriteLine("Test Success");
            else
                Console.WriteLine("Test Failed");
        }
    }
}

0 comments:

Post a Comment