Skip to content

Instantly share code, notes, and snippets.

@SeanKilleen
Created December 8, 2020 14:57
Show Gist options
  • Save SeanKilleen/16552bbd65bc53f21695950cedc5a296 to your computer and use it in GitHub Desktop.
Save SeanKilleen/16552bbd65bc53f21695950cedc5a296 to your computer and use it in GitHub Desktop.
A quick SpecFlow / Selenium / ChromeDriver example to answer a question on the mailing list.
# Modify to suit your needs
Feature: Logins
As a user
I want to be able to login
So that I can see my customized profile information
Scenario Outline: Logging in a registered user
Given I am on the home page
When I enter the username <username>
And I enter the password <password>
And I click login
Then I should be authenticated
Examples:
| username | password |
| user1 | password1 |
| user2 | password2 |
using System;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using TechTalk.SpecFlow;
namespace _20201208SpecFlowDemo
{
[Binding]
public sealed class UserLoginSteps : IDisposable
{
private readonly IWebDriver _driver;
private const string HOME_PAGE_URL = "https://seankilleen.com";
private readonly Uri _homePageUri;
private readonly ScenarioContext _scenarioContext;
public UserLoginSteps(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
_homePageUri = new Uri(HOME_PAGE_URL);
_driver = new ChromeDriver(); // TODO: Add specific driver location if not in bin folder.
}
[Given(@"I am on the home page")]
public void GivenIAmOnTheHomePage()
{
_driver.Navigate().GoToUrl(_homePageUri);
}
[When(@"I enter the username (.*)")]
public void WhenIEnterTheUsernameUser(string username)
{
UsernameField().SendKeys(username);
}
[When(@"I enter the password (.*)")]
public void WhenIEnterThePasswordPassword(string password)
{
PasswordField().SendKeys(password);
}
[When(@"I click login")]
public void WhenIClickLogin()
{
LoginButton().Click();
}
[Then(@"I should be authenticated")]
public void ThenIShouldBeAuthenticated()
{
var manageProfileImage = ManageProfileImage();
Assert.That(manageProfileImage, Is.Not.Null); // TODO: Replace with your own test
}
[AfterScenario]
public void AfterScenario()
{
LogoutOfSite();
}
private void LogoutOfSite()
{
MoveOverManageProfileImage();
ManageProfileImage().Click();
ProfileLogoutButton().Click();
}
private IWebElement ProfileLogoutButton()
{
return _driver.FindElement(By.XPath("/html/body/div/main/nav/div/div[2]/ul/li[4]/div/a[3]"));
}
private void MoveOverManageProfileImage()
{
var clickNavbarProfileImage = new Actions(_driver);
clickNavbarProfileImage.MoveToElement(ManageProfileImage());
clickNavbarProfileImage.Build().Perform();
}
public void Dispose()
{
_driver?.Dispose();
}
private IWebElement ManageProfileImage()
{
return _driver.FindElement(By.XPath("//*[@id='navbarDropdownProfile']/img"));
}
private IWebElement LoginButton()
{
return _driver.FindElement(By.CssSelector("#account > div:nth-child(7) > div > button"));
}
private IWebElement PasswordField()
{
return _driver.FindElement(By.Id("Input_Password"));
}
private IWebElement UsernameField()
{
return _driver.FindElement(By.Id("Input_Email"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment