Skip to content

Commit a705382

Browse files
author
Ladislav Šesták
committed
Selenium core base
1 parent 5fb20de commit a705382

18 files changed

+1275
-0
lines changed

.gitattributes

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Riganti.Utils.Testing.SeleniumCore", "Riganti.Utils.Testing\Riganti.Utils.Testing.SeleniumCore.csproj", "{A02B34DF-6874-4DBD-8ED9-6C73CE4120AB}"
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+
{A02B34DF-6874-4DBD-8ED9-6C73CE4120AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A02B34DF-6874-4DBD-8ED9-6C73CE4120AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A02B34DF-6874-4DBD-8ED9-6C73CE4120AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A02B34DF-6874-4DBD-8ED9-6C73CE4120AB}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
using OpenQA.Selenium;
2+
using OpenQA.Selenium.Support.Extensions;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
6+
using System.ComponentModel.Design;
7+
using System.Drawing.Imaging;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
13+
namespace Riganti.Utils.Testing.SeleniumCore
14+
{
15+
public class BrowserWrapper
16+
{
17+
private readonly IWebDriver browser;
18+
public IWebDriver Browser => browser;
19+
20+
public BrowserWrapper(IWebDriver browser)
21+
{
22+
this.browser = browser;
23+
SetCssSelector();
24+
}
25+
26+
private Func<string, By> selectorPreprocessMethod;
27+
28+
public virtual Func<string, By> SelectorPreprocessor
29+
{
30+
get { return selectorPreprocessMethod; }
31+
set
32+
{
33+
if (value == null)
34+
{ throw new Exception("Wrong selector preprocess methode."); }
35+
selectorPreprocessMethod = value;
36+
}
37+
}
38+
39+
public void SetCssSelector()
40+
{
41+
selectorPreprocessMethod = By.CssSelector;
42+
}
43+
44+
/// <summary>
45+
/// Url of active browser tab.
46+
/// </summary>
47+
public string CurrentUrl => browser.Url;
48+
49+
/// <summary>
50+
/// Gives path of url of active browser tab.
51+
/// </summary>
52+
public string CurrentUrlPath => new Uri(CurrentUrl).GetLeftPart(UriPartial.Path);
53+
54+
/// <summary>
55+
/// Compares url with current url of browser.
56+
/// </summary>
57+
public bool CompareUrl(string url)
58+
{
59+
Uri uri1 = new Uri(url);
60+
Uri uri2 = new Uri(browser.Url);
61+
62+
var result = Uri.Compare(uri1, uri2,
63+
UriComponents.Scheme | UriComponents.Host | UriComponents.PathAndQuery,
64+
UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase);
65+
66+
return result == 0;
67+
}
68+
69+
public void Click(string selector)
70+
{
71+
First(selector).Click();
72+
Thread.Sleep(100);
73+
}
74+
75+
public void Submit(string selector)
76+
{
77+
First(selector).Submit();
78+
Thread.Sleep(100);
79+
}
80+
81+
public void NavigateToUrl(string url)
82+
{
83+
browser.Navigate().GoToUrl(url);
84+
}
85+
86+
public void NavigateBack()
87+
{
88+
browser.Navigate().Back();
89+
}
90+
91+
public void NavigateForward()
92+
{
93+
browser.Navigate().Forward();
94+
}
95+
96+
public void Refresh()
97+
{
98+
browser.Navigate().Refresh();
99+
}
100+
101+
public string GetAlertText()
102+
{
103+
var alert = browser.SwitchTo().Alert();
104+
return alert?.Text;
105+
}
106+
107+
public void ConfirmAlert()
108+
{
109+
browser.SwitchTo().Alert().Accept();
110+
Thread.Sleep(500);
111+
}
112+
113+
/// <summary>
114+
/// Finds all elements that satisfy the condition of css selector.
115+
/// </summary>
116+
/// <param name="selector"></param>
117+
/// <returns></returns>
118+
public ElementWrapperCollection FindElements(string selector)
119+
{
120+
return browser.FindElements(SelectorPreprocessor(selector)).ToElementsList(this, selector);
121+
}
122+
123+
public ElementWrapper FirstOrDefault(string selector)
124+
{
125+
var elms = FindElements(selector);
126+
return elms.FirstOrDefault();
127+
}
128+
129+
public ElementWrapper First(string selector)
130+
{
131+
return ThrowIfIsNull(FirstOrDefault(selector), $"Element not found. Selector: {selector}");
132+
}
133+
134+
public ElementWrapper SingleOrDefault(string selector)
135+
{
136+
return FindElements(selector).SingleOrDefault();
137+
}
138+
139+
public ElementWrapper Single(string selector)
140+
{
141+
return FindElements(selector).Single();
142+
}
143+
144+
public ElementWrapper ElementAt(string selector, int index)
145+
{
146+
return FindElements(selector).ElementAt(index);
147+
}
148+
149+
public ElementWrapper Last(string selector)
150+
{
151+
return FindElements(selector).Last();
152+
}
153+
154+
public ElementWrapper LastOrDefault(string selector)
155+
{
156+
return FindElements(selector).LastOrDefault();
157+
}
158+
159+
public void Blur()
160+
{
161+
var jsExecutor = browser as IJavaScriptExecutor;
162+
jsExecutor?.ExecuteScript("if(document.activeElement && document.activeElement.blur) {document.activeElement.blur()}");
163+
}
164+
165+
public void SendKeys(string selector, string text)
166+
{
167+
FindElements(selector).ForEach(s => s.SendKeys(text));
168+
}
169+
170+
public void ClearElementsContent(string selector)
171+
{
172+
FindElements(selector).ForEach(s => s.Clear());
173+
}
174+
175+
public T ThrowIfIsNull<T>(T obj, string message)
176+
{
177+
if (obj == null)
178+
{
179+
throw new NoSuchElementException(message);
180+
}
181+
return obj;
182+
}
183+
184+
/// <summary>
185+
/// Takes a screenshot and returns a full path to the file.
186+
/// </summary>
187+
///<param name="filename">Path where the screenshot is going to be saved.</param>
188+
///<param name="format">Default value is PNG.</param>
189+
public void TakeScreenshot(string filename, ImageFormat format = null)
190+
{
191+
((ITakesScreenshot)browser).GetScreenshot().SaveAsFile(filename, format);
192+
}
193+
194+
public void Dispose()
195+
{
196+
browser.Dispose();
197+
}
198+
}
199+
}

0 commit comments

Comments
 (0)