Skip to content

Commit 8093036

Browse files
New: Selenium example
1 parent 7615606 commit 8093036

File tree

6 files changed

+191
-1
lines changed

6 files changed

+191
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ var response = await testRunner.Execute(test);
108108
```
109109

110110
## Other examples:
111-
For more examples and use cases please check [test](https://github.com/VikashChauhan51/keyword-engine/tree/master/src/KeywordEngine.Sample/Tests).
111+
For more examples and use cases please check [test](https://github.com/VikashChauhan51/keyword-engine/tree/master/src/KeywordEngine.Sample/Tests) and [Samples](https://github.com/VikashChauhan51/keyword-engine/tree/master/src/samples/).
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33122.133
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeywordEngine.Selenium", "KeywordEngine.Selenium\KeywordEngine.Selenium.csproj", "{5B70B454-8E3D-4AB0-95C4-82C768EB01B3}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{5B70B454-8E3D-4AB0-95C4-82C768EB01B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B70B454-8E3D-4AB0-95C4-82C768EB01B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B70B454-8E3D-4AB0-95C4-82C768EB01B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B70B454-8E3D-4AB0-95C4-82C768EB01B3}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {9DCDAACB-E46F-4085-BF7A-929E67BD7BC6}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using KeywordEngine.Models;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using OpenQA.Selenium;
6+
using OpenQA.Selenium.Edge;
7+
using KeywordEngine.Selenium.Keywords;
8+
using KeywordEngine.Core;
9+
10+
namespace KeywordEngine.Selenium
11+
{
12+
[TestClass]
13+
public class EdgeDriverTest
14+
{
15+
// In order to run the below test(s),
16+
// please follow the instructions from http://go.microsoft.com/fwlink/?LinkId=619687
17+
// to install Microsoft WebDriver.
18+
19+
private IWebDriver _driver;
20+
TestCaseRunner testRunner;
21+
22+
[TestInitialize]
23+
public void EdgeDriverInitialize()
24+
{
25+
// Initialize edge driver
26+
var options = new EdgeOptions
27+
{
28+
PageLoadStrategy = PageLoadStrategy.Normal
29+
};
30+
_driver = new EdgeDriver(options);
31+
testRunner = new TestCaseRunner(Module.Export(typeof(SearchKeyword).Assembly));
32+
}
33+
34+
[TestMethod]
35+
public async Task SampleBingSearch()
36+
{
37+
38+
var test = new TestCase
39+
{
40+
Id = 1,
41+
Title = "search on bing",
42+
Steps = new List<TestStep>
43+
{
44+
new TestStep
45+
{
46+
Title="search step",
47+
Keyword=nameof(SearchKeyword),
48+
Index=0,
49+
Parameters=new List<Parameter>
50+
{
51+
new Parameter
52+
{
53+
Name="text",
54+
Value="keyword engine nuget"
55+
}
56+
}
57+
},
58+
new TestStep
59+
{
60+
Title="search step",
61+
Keyword=nameof(SearchKeyword),
62+
Index=1,
63+
Parameters=new List<Parameter>
64+
{
65+
new Parameter
66+
{
67+
Name="text",
68+
Value="keyword engine nuget by vikash chauhan"
69+
}
70+
}
71+
}
72+
}
73+
74+
};
75+
76+
var testContext = new Core.TestContext(new Dictionary<string, object>() { { nameof(IWebDriver), _driver } });
77+
var response = await testRunner.Execute(test, testContext);
78+
Assert.IsNotNull(response);
79+
80+
}
81+
82+
[TestCleanup]
83+
public void EdgeDriverCleanup()
84+
{
85+
_driver.Quit();
86+
}
87+
}
88+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="KeywordEngine" Version="1.0.1" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
12+
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
13+
<PackageReference Include="Selenium.WebDriver" Version="4.10.0" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using KeywordEngine.Abstraction;
4+
using KeywordEngine.Models;
5+
using KeywordEngine.Selenium.Pages;
6+
using OpenQA.Selenium;
7+
8+
namespace KeywordEngine.Selenium.Keywords;
9+
public class SearchKeyword : IActionKeyword
10+
{
11+
private readonly IWebDriver webDriver;
12+
private readonly string text;
13+
public SearchKeyword(string text, ITestContext testContext)
14+
{
15+
this.webDriver = testContext?.Data[nameof(IWebDriver)] as IWebDriver ?? throw new ArgumentNullException(nameof(IWebDriver));
16+
this.text = text;
17+
}
18+
19+
public Task<KeywordResponse> Execute()
20+
{
21+
new SearchPage(webDriver)
22+
.Goto()
23+
.Search(text);
24+
25+
return Task.FromResult(new KeywordResponse
26+
{
27+
Status = ResponseStatus.Executed,
28+
Message = $"{nameof(SearchKeyword)} keyword executed."
29+
});
30+
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+

2+
3+
using System.Threading.Tasks;
4+
using OpenQA.Selenium;
5+
6+
namespace KeywordEngine.Selenium.Pages;
7+
public class SearchPage
8+
{
9+
private readonly IWebDriver driver;
10+
private IWebElement searchTermInput => driver.FindElement(By.Id("sb_form_q"));
11+
public SearchPage(IWebDriver driver)
12+
{
13+
this.driver = driver;
14+
}
15+
16+
public SearchPage Goto()
17+
{
18+
driver.Url = "https://www.bing.com";
19+
return this;
20+
}
21+
22+
public SearchPage Search(string text)
23+
{
24+
searchTermInput.SendKeys(text);
25+
searchTermInput.SendKeys(Keys.Enter);
26+
27+
return this;
28+
}
29+
}

0 commit comments

Comments
 (0)