Skip to content

Instantly share code, notes, and snippets.

@JasonArm
Created February 25, 2020 02:07
Show Gist options
  • Save JasonArm/eb6e7318fa05ff435008730d7c692e23 to your computer and use it in GitHub Desktop.
Save JasonArm/eb6e7318fa05ff435008730d7c692e23 to your computer and use it in GitHub Desktop.
Specflow Hook Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using SeleniumAutomation1.StepDefinitions;
using TechTalk.SpecFlow;
namespace SeleniumAutomation1.HookDefinitions
{
[Binding]
public class SeleniumHooks
{
protected static StepContext context;
public SeleniumHooks(StepContext context)
{
BaseSteps.context = context;
}
[BeforeScenario]
public void BeforeTestScenario()
{
context.driver = new ChromeDriver();
}
[AfterScenario]
public static void AfterTestRun()
{
if (context?.driver != null)
{
context.driver.Quit();
}
}
}
}
using OpenQA.Selenium;
using SeleniumAutomation1.PageObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeleniumAutomation1.StepDefinitions
{
public class StepContext
{
public IWebDriver driver;
public HomePage _homePage;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using SeleniumAutomation1.PageObjects;
using TechTalk.SpecFlow;
namespace SeleniumAutomation1.StepDefinitions
{
[Binding]
public class BaseSteps
{
protected static StepContext context;
protected HomePage _homePage;
public BaseSteps(StepContext context)
{
BaseSteps.context = context;
}
[Given(@"I have navigated to the Jupiter Toys website")]
public void GivenIHaveNavigatedToTheWebsite()
{
_homePage = new HomePage(context.driver);
context._homePage = _homePage;
_homePage.GotoBaseURL();
}
[When(@"I press on the ""(.*)"" button")]
public void WhenIPressOnTheButton(string buttonName)
{
_homePage.clickStartShopping();
}
[Then(@"I should see some things for sale")]
public void ThenIShouldSeeSomeThingsForSale()
{
// Do Something
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment