-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageTestBase.cs
64 lines (59 loc) · 2.11 KB
/
PageTestBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using OLX_Selenium_Tests.Helpers;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace OLX_Selenium_Tests.PageTests
{
public abstract class PageTestBase : IDisposable
{
protected PageTestBase(string pageUrl)
{
url = pageUrl ?? throw new ArgumentNullException(nameof(pageUrl));
}
protected IWebDriver? driver;
protected readonly string url;
protected IWebDriver DriverInit()
{
driver = WebDriverFactory.Init(WebDriverType.CHROME);
driver.Navigate().GoToUrl(url);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.Manage().Window.Maximize();
return driver;
}
protected void DriverWaitForRedirection()
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.Url != url);
}
protected void DriverWaitForExpectedPageLoad(string pageUrl)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.Url.Substring(0, pageUrl.Length) == pageUrl.Substring(0, pageUrl.Length)); ;
}
public void Dispose()
{
driver?.Close();
GC.SuppressFinalize(this);
}
protected void TakeScreenshot(string testMethodName)
{
ITakesScreenshot? driverScreenshot = driver as ITakesScreenshot;
Screenshot? screenshot = driverScreenshot.GetScreenshot();
var directoryPath = $"./Screenshots";
if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); }
int filesCount = Directory.GetFiles(directoryPath).Length;
screenshot.SaveAsFile($"{directoryPath}\\{testMethodName}-{filesCount + 1}.png");
}
protected void UITest(string testMethodName, Action action)
{
try
{
action();
}
catch (Exception ex)
{
TakeScreenshot(testMethodName);
throw;
}
}
}
}