Skip to content

Commit d47cb55

Browse files
committed
Added login test
1 parent 945bab7 commit d47cb55

15 files changed

+476
-35
lines changed

Framework/BasePage.cs

+52-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
using Framework.Navigation;
22
using OpenQA.Selenium;
33
using OpenQA.Selenium.Support.UI;
44
using System;
@@ -7,10 +7,19 @@
77

88
namespace Framework
99
{
10-
internal abstract class BasePage
10+
internal abstract class BasePage : IMastHead
1111
{
1212
protected readonly IWebDriver Driver;
1313
//Locators to find elements on the page
14+
private readonly By BasePageLink = By.LinkText("Automation");
15+
private readonly By SearchText = By.Id("woocommerce-product-search-field-0");
16+
private readonly By HomeLink = By.LinkText("Home");
17+
private readonly By CartLink = By.LinkText("Cart");
18+
private readonly By CheckoutLink = By.LinkText("Checkout");
19+
private readonly By MyAccountLink = By.LinkText("My account");
20+
private readonly By ViewCartLink = By.Id("site-header-cart");
21+
private readonly By SearchButton = By.CssSelector("[@value='Search']");
22+
1423
protected BasePage(IWebDriver driver)
1524
{
1625
Driver = driver;
@@ -29,7 +38,7 @@ protected void Click(By locator)
2938
}
3039
protected void EnabledClick(By locator)
3140
{
32-
if(IsEnabled(locator))
41+
if (IsEnabled(locator))
3342
Find(locator).Click();
3443
}
3544
protected void Type(By locator, string inputText)
@@ -84,7 +93,7 @@ protected void DisplayedSelect(By locator, string inputText)
8493
protected void Check(By locator, string check)
8594
{
8695
if (!string.IsNullOrEmpty(check))
87-
Find(locator).Click();
96+
Find(locator).Click();
8897
}
8998
protected void EnabledCheck(By locator, string check)
9099
{
@@ -119,7 +128,8 @@ protected bool IsDisplayed(By locator)
119128
try
120129
{
121130
return Find(locator).Displayed;
122-
} catch (NoSuchElementException)
131+
}
132+
catch (NoSuchElementException)
123133
{
124134
return false;
125135
}
@@ -131,7 +141,8 @@ protected bool IsDisplayed(By locator, int maxWaitTime)
131141
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(maxWaitTime));
132142
wait.Until(ExpectedConditions.ElementIsVisible(locator));
133143
return true;
134-
} catch (WebDriverTimeoutException)
144+
}
145+
catch (WebDriverTimeoutException)
135146
{
136147
return false;
137148
}
@@ -143,7 +154,8 @@ protected bool IsDisplayed(By locator, string text, int maxWaitTime)
143154
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(maxWaitTime));
144155
wait.Until(ExpectedConditions.TextToBePresentInElementValue(locator, text));
145156
return true;
146-
} catch (WebDriverTimeoutException)
157+
}
158+
catch (WebDriverTimeoutException)
147159
{
148160
return false;
149161
}
@@ -153,7 +165,8 @@ protected bool IsEnabled(By locator)
153165
try
154166
{
155167
return Find(locator).Enabled;
156-
} catch (InvalidElementStateException)
168+
}
169+
catch (InvalidElementStateException)
157170
{
158171
return false;
159172
}
@@ -173,7 +186,37 @@ protected void AcceptPopup()
173186
try
174187
{
175188
Driver.SwitchTo().Alert().Accept();
176-
} catch (NoAlertPresentException e) { }
189+
}
190+
catch (NoAlertPresentException e) { }
191+
}
192+
public void GoToBasePage()
193+
{
194+
Click(BasePageLink);
195+
}
196+
public void Search()
197+
{
198+
Click(SearchText);
177199
}
200+
public void Home()
201+
{
202+
Click(HomeLink);
203+
}
204+
public void Cart()
205+
{
206+
Click(CartLink);
207+
}
208+
public void Checkout()
209+
{
210+
Click(CheckoutLink);
211+
}
212+
public void MyAccount()
213+
{
214+
Click(MyAccountLink);
215+
}
216+
public void ViewCart()
217+
{
218+
Click(ViewCartLink);
219+
}
220+
178221
}
179222
}

Framework/Framework.projitems

+5
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)BasePage.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)Navigation\IMastHead.cs" />
14+
<Compile Include="$(MSBuildThisFileDirectory)Pages\HomePage.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)Pages\MyAccountPage.cs" />
16+
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ExcelLibrary.cs" />
17+
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ScreenShot.cs" />
1318
</ItemGroup>
1419
</Project>

Framework/Navigation/IMastHead.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Framework.Navigation
2+
{
3+
interface IMastHead
4+
{
5+
void GoToBasePage();
6+
void Search();
7+
void Home();
8+
void Cart();
9+
void Checkout();
10+
void MyAccount();
11+
void ViewCart();
12+
}
13+
}

Framework/Pages/HomePage.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using OpenQA.Selenium;
5+
using NUnit.Framework;
6+
using UI_Tests.Data;
7+
8+
namespace Framework.Pages
9+
{
10+
class HomePage : BasePage
11+
{
12+
private readonly By PageTitle = By.CssSelector(".woocommerce-products-header__title");
13+
private readonly By OrderByDropdown = By.CssSelector("[@name='orderby']");
14+
public HomePage(IWebDriver driver) : base(driver) { }
15+
public bool PageDisplays() => IsDisplayed(PageTitle, 10);
16+
public HomePage GoTo(string url)
17+
{
18+
Visit(url);
19+
Assert.That(PageDisplays());
20+
return this;
21+
}
22+
}
23+
}

Framework/Pages/MyAccountPage.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using OpenQA.Selenium;
5+
using NUnit.Framework;
6+
using UI_Tests.Data;
7+
8+
namespace Framework.Pages
9+
{
10+
class MyAccountPage : BasePage
11+
{
12+
private readonly By PageTitle = By.LinkText("My account");
13+
private readonly By LoggInContent = By.CssSelector(".woocommerce-MyAccount-content");
14+
private readonly By UsernameText = By.Id("username");
15+
private readonly By PasswordText = By.Id("password");
16+
private readonly By LoginButton = By.CssSelector("input[name='login']");
17+
public MyAccountPage(IWebDriver driver) : base(driver) { }
18+
public bool PageDisplays() => IsDisplayed(PageTitle, 10);
19+
public bool UserContentDisplays() => IsDisplayed(LoggInContent, 10);
20+
public MyAccountPage Login(string username, string password)
21+
{
22+
Type(UsernameText, username);
23+
Type(PasswordText, password);
24+
Click(LoginButton);
25+
return this;
26+
}
27+
}
28+
}

Framework/Utilities/ExcelLibrary.cs

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Data;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using ExcelDataReader;
8+
using DocumentFormat.OpenXml.Packaging;
9+
using Ex = Microsoft.Office.Interop.Excel;
10+
using System.Xml.Linq;
11+
12+
namespace Framework
13+
{
14+
public class Library
15+
{
16+
public DataTable ExcelToDataTable(string fileName, string tableName)
17+
{
18+
//open file and returns as Stream
19+
var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
20+
21+
//Createopenxmlreader via ExcelReaderFactory
22+
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //.xlsx
23+
24+
//Set the First Row as Column Name
25+
//excelReader.IsFirstRowAsColumnNames = true;
26+
27+
//Return as DataSet
28+
using (DataSet result = excelReader.AsDataSet())
29+
{
30+
//Get all the Tables
31+
DataTableCollection table = result.Tables;
32+
33+
//Store it in DataTable
34+
DataTable resultTable = table[tableName];
35+
36+
excelReader.Close();
37+
38+
//return
39+
return resultTable;
40+
}
41+
}
42+
43+
public List<Datacollection> DataCol = new List<Datacollection>();
44+
45+
public void PopulateInCollection(string fileName, string tableName)
46+
{
47+
DataTable table = new Library().ExcelToDataTable(fileName, tableName);
48+
49+
//Iterate through the rows and columns of the Table
50+
for (int row = 1; row <= table.Rows.Count; row++)
51+
{
52+
for (int col = 0; col < table.Columns.Count; col++)
53+
{
54+
var dtTable = new Datacollection
55+
{
56+
RowNumber = row,
57+
ColName = table.Columns[col].ColumnName,
58+
ColValue = table.Rows[row - 1][col].ToString()
59+
};
60+
//Add all the details for each row
61+
DataCol.Add(dtTable);
62+
}
63+
}
64+
}
65+
66+
public string Data(int rowNumber, string columnName)
67+
{
68+
try
69+
{
70+
//Retriving Data using LINQ to reduce iterations
71+
string data = (from colData in DataCol
72+
where colData.ColName == columnName && colData.RowNumber == rowNumber
73+
select colData.ColValue).SingleOrDefault();
74+
75+
return data;
76+
}
77+
catch (Exception)
78+
{
79+
return null;
80+
}
81+
}
82+
83+
public void OpenAndSave(string fileName)
84+
{
85+
var lastOpened = File.GetLastWriteTime(fileName).Date;
86+
87+
Debug.WriteLine("File: " + fileName);
88+
89+
if (lastOpened != DateTime.Today)
90+
{
91+
var src = new FileInfo(fileName);
92+
93+
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(src.FullName, true))
94+
{
95+
var calculationProperties = spreadSheet.WorkbookPart.Workbook.CalculationProperties;
96+
calculationProperties.ForceFullCalculation = true;
97+
calculationProperties.FullCalculationOnLoad = true;
98+
}
99+
100+
//Use Excel automation to open and save the workbook, thereby running the calculation engine.
101+
var app = new Ex.Application();
102+
//string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
103+
Ex.Workbook book = app.Workbooks.Open(src.FullName);
104+
book.Save();
105+
book.Close();
106+
app.Quit();
107+
}
108+
}
109+
110+
public string[] GetTable(string testClass)
111+
{
112+
string[] tableQuery = new string[2];
113+
114+
var table = XDocument.Load("../../../Tables.xml").Descendants("Table");
115+
116+
Debug.WriteLine("TestClass: " + testClass);
117+
Debug.WriteLine("Table: " + table);
118+
119+
var xElements = table as IList<XElement> ?? table.ToList();
120+
var stateQuery = xElements
121+
.Where(t => (t.Element("TestClass")?.Value) == testClass)
122+
.Select(t => t.Element("State")?.Value).Single();
123+
124+
var sheetQuery = xElements
125+
.Where(t => (t.Element("TestClass")?.Value) == testClass)
126+
.Select(t => t.Element("Sheet")?.Value).Single();
127+
128+
tableQuery[0] = stateQuery;
129+
tableQuery[1] = sheetQuery;
130+
131+
return tableQuery;
132+
}
133+
}
134+
135+
public class Datacollection
136+
{
137+
public int RowNumber { get; set; }
138+
public string ColName { get; set; }
139+
public string ColValue { get; set; }
140+
}
141+
}

Framework/Utilities/JiraManager.cs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Linq;
2+
using System.Xml.Linq;
3+
using Atlassian.Jira;
4+
using System.Threading.Tasks;
5+
6+
namespace Framework
7+
{
8+
class JiraManager
9+
{
10+
Jira jira => Jira.CreateRestClient("https://support.vitalchek.com/", "davetestuser", "@K<uBVnv5W(64+z?");
11+
12+
public async Task UpdateJira(string key, string testStatus, string testName, string screenShotName)
13+
{
14+
var issue = await jira.Issues.GetIssueAsync(key);
15+
16+
switch (testStatus)
17+
{
18+
case "Passed":
19+
if (issue.Status.Name == "PASS")
20+
break;
21+
await issue.WorkflowTransitionAsync("PASS");
22+
await issue.SaveChangesAsync();
23+
break;
24+
case "Failed":
25+
if (issue.Status.Name == "FAILED")
26+
break;
27+
await issue.WorkflowTransitionAsync("FAIL");
28+
issue.AddAttachment(screenShotName); //"../../../TestResults/" + testName + "_" + testStatus + "_" + System.DateTime.Now.ToString("dd_MMMM") + ".png"
29+
issue.Assignee= "JHearn";
30+
await issue.SaveChangesAsync();
31+
break;
32+
}
33+
}
34+
35+
public string GetKey(string fullName)
36+
{
37+
var ticketQuery =
38+
XDocument.Load("../../../Jiras.xml").Descendants("Issue")
39+
.Where(t => (t.Element("TestName")?.Value) == fullName)
40+
.Select(t => t.Element("Key")?.Value).Single();
41+
42+
return ticketQuery;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)