Example code

This commit is contained in:
programmingPug
2020-08-15 22:08:58 -04:00
parent 416fc3a7eb
commit f0a48e25f4
16 changed files with 611 additions and 0 deletions

17
pom.sln Normal file
View File

@@ -0,0 +1,17 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pom", "pom\pom.csproj", "{EC01B297-C25B-446E-96A4-E2153A5305CC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EC01B297-C25B-446E-96A4-E2153A5305CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC01B297-C25B-446E-96A4-E2153A5305CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC01B297-C25B-446E-96A4-E2153A5305CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC01B297-C25B-446E-96A4-E2153A5305CC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

BIN
pom/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,20 @@
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace pom.Components
{
public class LandingComponent
{
private IWebDriver webDriver;
private WebDriverWait webDriverWait;
private IJavaScriptExecutor jsExecutor;
public LandingComponent(IWebDriver _webDriver)
{
webDriver = _webDriver;
webDriverWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30));
jsExecutor = (IJavaScriptExecutor)webDriver;
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace pom.Components
{
public class LoginComponent
{
private IWebDriver webDriver;
private WebDriverWait webDriverWait;
private IJavaScriptExecutor jsExecutor;
public LoginComponent(IWebDriver _webDriver)
{
webDriver = _webDriver;
webDriverWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30));
jsExecutor = (IJavaScriptExecutor)webDriver;
}
public void GoToLogin()
{
webDriver.Navigate().GoToUrl("Some Site");
}
public void EnterUserCredentials(string username, string password)
{
webDriver.FindElement(By.Name("username")).SendKeys(username);
webDriver.FindElement(By.Name("password")).SendKeys(password);
}
public void SubmitLoginForm()
{
webDriver.FindElement(By.Name("login")).Click();
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace pom.Components
{
public class NavigationComponent
{
private IWebDriver webDriver;
private WebDriverWait webDriverWait;
public NavigationComponent(IWebDriver _webDriver)
{
webDriver = _webDriver;
webDriverWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30));
}
public string GetNavAccountName()
{
//Example of getting an account name
var accountNav = webDriver.FindElement(By.Id("account-nav")).FindElements(By.TagName("span"))[1];
string accountNavName = accountNav.GetAttribute("innerHTML");
return (accountNavName);
}
public void GoToVideos()
{
//Goes to videos page
}
}
}

View File

@@ -0,0 +1,37 @@
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace pom.Components
{
public class VideoComponent
{
private IWebDriver webDriver;
private WebDriverWait webDriverWait;
private IJavaScriptExecutor jsExecutor;
public VideoComponent(IWebDriver _webDriver)
{
webDriver = _webDriver;
webDriverWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30));
jsExecutor = (IJavaScriptExecutor)webDriver;
}
public void PlayVideo()
{
//Plays Video
}
public void PauseVideo()
{
//Pauses Video
}
public bool GetVideoPlayState()
{
//Get video play sate from player
return (false);
}
}
}

View File

@@ -0,0 +1,29 @@
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace pom.Components
{
public class VideosComponent
{
private IWebDriver webDriver;
private WebDriverWait webDriverWait;
private IJavaScriptExecutor jsExecutor;
public VideosComponent(IWebDriver _webDriver)
{
webDriver = _webDriver;
webDriverWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30));
jsExecutor = (IJavaScriptExecutor)webDriver;
}
public void SelectVideo()
{
webDriverWait.Until(drv => drv.FindElement(By.ClassName("video-loop-video-title")));
var videos = webDriver.FindElements(By.ClassName("video-loop-video-title"));
var video = videos[0];
video.Click();
}
}
}

BIN
pom/Features/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,59 @@
using FluentAssertions;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using pom.Components;
using TechTalk.SpecFlow;
namespace pom.Features.Login
{
[Binding]
public class Login
{
private IWebDriver webDriver;
private LoginComponent loginComponent;
private NavigationComponent navigationComponent;
[BeforeScenario]
public void SetupTest()
{
webDriver = GetWebDriver();
loginComponent = new LoginComponent(webDriver);
navigationComponent = new NavigationComponent(webDriver);
}
public IWebDriver GetWebDriver()
{
/* Yes we could put this into a factory and use DI but KISS for now. */
ChromeOptions options = new ChromeOptions();
options.AddArguments("start-maximized");
IWebDriver ChromeWebDriver = new ChromeDriver(options);
return ChromeWebDriver;
}
[Given(@"I am at the login page")]
public void GivenIAmAtTheLoginPage()
{
loginComponent.GoToLogin();
}
[Given(@"I have entered my credentials")]
public void AndIHaveEnteredMyCredentials()
{
loginComponent.EnterUserCredentials("Information", "Information");
}
[When(@"I press login")]
public void WhenIPressLogin()
{
loginComponent.SubmitLoginForm();
}
[Then(@"the I have logged into the application")]
public void ThenTheIHaveLoggedIntoTheApplication()
{
navigationComponent.GetNavAccountName().Should().Be("user");
}
}
}

View File

@@ -0,0 +1,10 @@
Feature: Login
In order to use the application
As a user
I want to log into the application
Scenario: Log into the application
Given I am at the login page
And I have entered my credentials
When I press login
Then the I have logged into the application

121
pom/Features/Login/Login.feature.cs generated Normal file
View File

@@ -0,0 +1,121 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by SpecFlow (https://www.specflow.org/).
// SpecFlow Version:3.3.0.0
// SpecFlow Generator Version:3.1.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
#region Designer generated code
#pragma warning disable
namespace pom.Features.Login
{
using TechTalk.SpecFlow;
using System;
using System.Linq;
[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "3.3.0.0")]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[NUnit.Framework.TestFixtureAttribute()]
[NUnit.Framework.DescriptionAttribute("Login")]
public partial class LoginFeature
{
private TechTalk.SpecFlow.ITestRunner testRunner;
private string[] _featureTags = ((string[])(null));
#line 1 "Login.feature"
#line hidden
[NUnit.Framework.OneTimeSetUpAttribute()]
public virtual void FeatureSetup()
{
testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "Login", "\tIn order to use the application\n\tAs a user\n\tI want to log into the application", ProgrammingLanguage.CSharp, ((string[])(null)));
testRunner.OnFeatureStart(featureInfo);
}
[NUnit.Framework.OneTimeTearDownAttribute()]
public virtual void FeatureTearDown()
{
testRunner.OnFeatureEnd();
testRunner = null;
}
[NUnit.Framework.SetUpAttribute()]
public virtual void TestInitialize()
{
}
[NUnit.Framework.TearDownAttribute()]
public virtual void TestTearDown()
{
testRunner.OnScenarioEnd();
}
public virtual void ScenarioInitialize(TechTalk.SpecFlow.ScenarioInfo scenarioInfo)
{
testRunner.OnScenarioInitialize(scenarioInfo);
testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs<NUnit.Framework.TestContext>(NUnit.Framework.TestContext.CurrentContext);
}
public virtual void ScenarioStart()
{
testRunner.OnScenarioStart();
}
public virtual void ScenarioCleanup()
{
testRunner.CollectScenarioErrors();
}
[NUnit.Framework.TestAttribute()]
[NUnit.Framework.DescriptionAttribute("Log into the application")]
public virtual void LogIntoTheApplication()
{
string[] tagsOfScenario = ((string[])(null));
System.Collections.Specialized.OrderedDictionary argumentsOfScenario = new System.Collections.Specialized.OrderedDictionary();
TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Log into the application", null, tagsOfScenario, argumentsOfScenario);
#line 6
this.ScenarioInitialize(scenarioInfo);
#line hidden
bool isScenarioIgnored = default(bool);
bool isFeatureIgnored = default(bool);
if ((tagsOfScenario != null))
{
isScenarioIgnored = tagsOfScenario.Where(__entry => __entry != null).Where(__entry => String.Equals(__entry, "ignore", StringComparison.CurrentCultureIgnoreCase)).Any();
}
if ((this._featureTags != null))
{
isFeatureIgnored = this._featureTags.Where(__entry => __entry != null).Where(__entry => String.Equals(__entry, "ignore", StringComparison.CurrentCultureIgnoreCase)).Any();
}
if ((isScenarioIgnored || isFeatureIgnored))
{
testRunner.SkipScenario();
}
else
{
this.ScenarioStart();
#line 7
testRunner.Given("I am at the login page", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given ");
#line hidden
#line 8
testRunner.And("I have entered my credentials", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
#line hidden
#line 9
testRunner.When("I press login", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
#line hidden
#line 10
testRunner.Then("the I have logged into the application", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then ");
#line hidden
}
this.ScenarioCleanup();
}
}
}
#pragma warning restore
#endregion

BIN
pom/Features/Videos/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,68 @@
using FluentAssertions;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using pom.Components;
using TechTalk.SpecFlow;
namespace pom.Features.Videos
{
[Binding]
public class Videos
{
private IWebDriver webDriver;
private LoginComponent loginComponent;
private NavigationComponent navigationComponent;
private VideosComponent videosComponent;
private VideoComponent videoComponent;
[BeforeScenario]
public void setupTest()
{
webDriver = GetWebDriver();
loginComponent = new LoginComponent(webDriver);
navigationComponent = new NavigationComponent(webDriver);
videosComponent = new VideosComponent(webDriver);
videoComponent = new VideoComponent(webDriver);
}
public IWebDriver GetWebDriver()
{
/* Yes we could put this into a factory and use DI but KISS for now. */
ChromeOptions options = new ChromeOptions();
options.AddArguments("start-maximized");
IWebDriver ChromeWebDriver = new ChromeDriver(options);
return ChromeWebDriver;
}
[Given(@"I have logged into the application")]
public void GivenIHaveLoggedIntoTheApplication()
{
loginComponent.GoToLogin();
loginComponent.EnterUserCredentials("", "");
loginComponent.SubmitLoginForm();
}
[Given(@"I have started playing a video")]
public void AndIHaveStartedPlayingAVideo()
{
navigationComponent.GoToVideos();
videosComponent.SelectVideo();
videoComponent.PlayVideo();
}
[When(@"I press pause")]
public void WhenIPressPause()
{
videoComponent.PauseVideo();
videoComponent.GetVideoPlayState().Should().Be(true);
}
[Then(@"the video should be paused")]
public void ThenTheVideoShouldBePaused()
{
videoComponent.GetVideoPlayState().Should().Be(true);
}
}
}

View File

@@ -0,0 +1,10 @@
Feature: Video Controls
In order to control a video
As a user
I want to manipulate video controls
Scenario: Pause video playback
Given I have logged into the application
And I have started playing a video
When I press pause
Then the video should be paused

121
pom/Features/Videos/Videos.feature.cs generated Normal file
View File

@@ -0,0 +1,121 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by SpecFlow (https://www.specflow.org/).
// SpecFlow Version:3.3.0.0
// SpecFlow Generator Version:3.1.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
#region Designer generated code
#pragma warning disable
namespace pom.Features.Videos
{
using TechTalk.SpecFlow;
using System;
using System.Linq;
[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "3.3.0.0")]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[NUnit.Framework.TestFixtureAttribute()]
[NUnit.Framework.DescriptionAttribute("Video Controls")]
public partial class VideoControlsFeature
{
private TechTalk.SpecFlow.ITestRunner testRunner;
private string[] _featureTags = ((string[])(null));
#line 1 "Videos.feature"
#line hidden
[NUnit.Framework.OneTimeSetUpAttribute()]
public virtual void FeatureSetup()
{
testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "Video Controls", "\tIn order to control a video\n\tAs a user\n\tI want to manipulate video controls", ProgrammingLanguage.CSharp, ((string[])(null)));
testRunner.OnFeatureStart(featureInfo);
}
[NUnit.Framework.OneTimeTearDownAttribute()]
public virtual void FeatureTearDown()
{
testRunner.OnFeatureEnd();
testRunner = null;
}
[NUnit.Framework.SetUpAttribute()]
public virtual void TestInitialize()
{
}
[NUnit.Framework.TearDownAttribute()]
public virtual void TestTearDown()
{
testRunner.OnScenarioEnd();
}
public virtual void ScenarioInitialize(TechTalk.SpecFlow.ScenarioInfo scenarioInfo)
{
testRunner.OnScenarioInitialize(scenarioInfo);
testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs<NUnit.Framework.TestContext>(NUnit.Framework.TestContext.CurrentContext);
}
public virtual void ScenarioStart()
{
testRunner.OnScenarioStart();
}
public virtual void ScenarioCleanup()
{
testRunner.CollectScenarioErrors();
}
[NUnit.Framework.TestAttribute()]
[NUnit.Framework.DescriptionAttribute("Pause video playback")]
public virtual void PauseVideoPlayback()
{
string[] tagsOfScenario = ((string[])(null));
System.Collections.Specialized.OrderedDictionary argumentsOfScenario = new System.Collections.Specialized.OrderedDictionary();
TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Pause video playback", null, tagsOfScenario, argumentsOfScenario);
#line 6
this.ScenarioInitialize(scenarioInfo);
#line hidden
bool isScenarioIgnored = default(bool);
bool isFeatureIgnored = default(bool);
if ((tagsOfScenario != null))
{
isScenarioIgnored = tagsOfScenario.Where(__entry => __entry != null).Where(__entry => String.Equals(__entry, "ignore", StringComparison.CurrentCultureIgnoreCase)).Any();
}
if ((this._featureTags != null))
{
isFeatureIgnored = this._featureTags.Where(__entry => __entry != null).Where(__entry => String.Equals(__entry, "ignore", StringComparison.CurrentCultureIgnoreCase)).Any();
}
if ((isScenarioIgnored || isFeatureIgnored))
{
testRunner.SkipScenario();
}
else
{
this.ScenarioStart();
#line 7
testRunner.Given("I have logged into the application", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given ");
#line hidden
#line 8
testRunner.And("I have started playing a video", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
#line hidden
#line 9
testRunner.When("I press pause", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
#line hidden
#line 10
testRunner.Then("the video should be paused", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then ");
#line hidden
}
this.ScenarioCleanup();
}
}
}
#pragma warning restore
#endregion

51
pom/pom.csproj Normal file
View File

@@ -0,0 +1,51 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="SpecFlow" Version="3.3.74" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="84.0.4147.3001" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
<PackageReference Include="SpecFlow.NUnit" Version="3.3.74" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Features\" />
<Folder Include="Features\Login\" />
<Folder Include="Features\Videos\" />
<Folder Include="Components\" />
</ItemGroup>
<ItemGroup>
<Compile Update="Features\Login\Login.feature.cs">
<DependentUpon>Login.feature</DependentUpon>
</Compile>
<Compile Update="Features\Videos\Videos.feature.cs">
<DependentUpon>Videos.feature</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<SpecFlowFeatureFiles Update="Features\Login\Login.feature">
<Visible>False</Visible>
<Generator>SpecFlowSingleFileGenerator</Generator>
<LastGenOutput>Login.feature.cs</LastGenOutput>
</SpecFlowFeatureFiles>
<SpecFlowFeatureFiles Update="Features\Videos\Landing.feature">
<Visible>False</Visible>
<Generator>SpecFlowSingleFileGenerator</Generator>
<LastGenOutput>Landing.feature.cs</LastGenOutput>
</SpecFlowFeatureFiles>
<SpecFlowFeatureFiles Update="Features\Videos\Videos.feature">
<Visible>False</Visible>
<Generator>SpecFlowSingleFileGenerator</Generator>
<LastGenOutput>Videos.feature.cs</LastGenOutput>
</SpecFlowFeatureFiles>
</ItemGroup>
</Project>