Skip to content

Commit d080e00

Browse files
RathoreRathore
authored andcommitted
Added the code to get locator namek
1 parent 9371528 commit d080e00

10 files changed

Lines changed: 286 additions & 7 deletions

File tree

SeleniumWebdriver/BaseClasses/BaseClass.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using SeleniumWebdriver.ComponentHelper;
1515
using SeleniumWebdriver.Configuration;
1616
using SeleniumWebdriver.CustomException;
17+
using SeleniumWebdriver.Reports;
1718
using SeleniumWebdriver.Settings;
1819
using TechTalk.SpecFlow;
1920

@@ -116,7 +117,8 @@ private static PhantomJSDriverService GetPhantomJsDrvierService()
116117
public static void InitWebdriver(TestContext tc)
117118
{
118119
ObjectRepository.Config = new AppConfigReader();
119-
120+
Reporter.GetReportManager();
121+
Reporter.AddTestCaseMetadataToHtmlReport(tc);
120122
switch (ObjectRepository.Config.GetBrowser())
121123
{
122124
case BrowserType.Firefox:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using OpenQA.Selenium;
2+
using OpenQA.Selenium.Firefox;
3+
using OpenQA.Selenium.Remote;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Drawing;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace SeleniumWebdriver.ComponentHelper
13+
{
14+
public class CustomWebElement : RemoteWebElement
15+
{
16+
private string ElementName = "Default Name";
17+
public string Name
18+
{
19+
set
20+
{
21+
ElementName = value;
22+
}
23+
}
24+
25+
public CustomWebElement(RemoteWebDriver parentDriver, string id) : base(parentDriver,id)
26+
{
27+
28+
}
29+
30+
public override string ToString()
31+
{
32+
return ElementName;
33+
}
34+
35+
36+
}
37+
}

SeleniumWebdriver/PageObject/HomePage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public class HomePage : PageBase
1818
#region WebElement
1919

2020
[FindsBy(How = How.Id, Using = "quicksearch_main")]
21-
private IWebElement QuickSearchTextBox;
21+
public IWebElement QuickSearchTextBox;
2222
//private IWebElement QuickSearchTextBox => driver.FindElement(By.Id("quicksearch_main"));
2323

2424
[FindsBy(How = How.Id, Using = "find")]
2525
[CacheLookup]
26-
private IWebElement QuickSearchBtn;
26+
public IWebElement QuickSearchBtn;
2727
//private IWebElement QuickSearchBtn => driver.FindElement(By.Id("find"));
2828

2929
[FindsBy(How = How.LinkText, Using = "File a Bug")]
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using AventStack.ExtentReports;
2+
using AventStack.ExtentReports.Reporter;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using NLog;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace SeleniumWebdriver.Reports
13+
{
14+
public class Reporter
15+
{
16+
private static readonly Logger TheLogger = LogManager.GetCurrentClassLogger();
17+
private static ExtentReports ReportManager { get; set; }
18+
private static string ApplicationDebuggingFolder => @"C:\Data\report";
19+
20+
private static string HtmlReportFullPath { get; set; }
21+
22+
public static string LatestResultsReportFolder { get; set; }
23+
24+
private static TestContext MyTestContext { get; set; }
25+
26+
private static ExtentTest CurrentTestCase { get; set; }
27+
28+
private static bool IsExtenetReportStarted = false;
29+
30+
public static void StartReporter()
31+
{
32+
TheLogger.Trace("Starting a one time setup for the entire" +
33+
" .CreatingReports namespace." +
34+
"Going to initialize the reporter next...");
35+
CreateReportDirectory();
36+
var htmlReporter = new ExtentHtmlReporter(HtmlReportFullPath);
37+
ReportManager = new ExtentReports();
38+
ReportManager.AttachReporter(htmlReporter);
39+
}
40+
41+
private static void CreateReportDirectory()
42+
{
43+
var filePath = Path.GetFullPath(ApplicationDebuggingFolder);
44+
LatestResultsReportFolder = Path.Combine(filePath, DateTime.Now.ToString("MMdd_HHmm"));
45+
Directory.CreateDirectory(LatestResultsReportFolder);
46+
47+
HtmlReportFullPath = $"{LatestResultsReportFolder}\\TestResults.html";
48+
TheLogger.Trace("Full path of HTML report=>" + HtmlReportFullPath);
49+
}
50+
51+
public static void AddTestCaseMetadataToHtmlReport(TestContext testContext)
52+
{
53+
MyTestContext = testContext;
54+
CurrentTestCase = ReportManager.CreateTest(MyTestContext.TestName);
55+
}
56+
57+
public static void LogPassingTestStepToBugLogger(string message)
58+
{
59+
TheLogger.Info(message);
60+
CurrentTestCase.Log(Status.Pass, message);
61+
}
62+
63+
public static void ReportTestOutcome(string screenshotPath)
64+
{
65+
var status = MyTestContext.CurrentTestOutcome;
66+
67+
switch (status)
68+
{
69+
case UnitTestOutcome.Failed:
70+
TheLogger.Error($"Test Failed=>{MyTestContext.FullyQualifiedTestClassName}");
71+
CurrentTestCase.AddScreenCaptureFromPath(screenshotPath);
72+
CurrentTestCase.Fail("Fail");
73+
break;
74+
case UnitTestOutcome.Inconclusive:
75+
CurrentTestCase.AddScreenCaptureFromPath(screenshotPath);
76+
CurrentTestCase.Warning("Inconclusive");
77+
break;
78+
case UnitTestOutcome.Unknown:
79+
CurrentTestCase.Skip("Test skipped");
80+
break;
81+
default:
82+
CurrentTestCase.Pass("Pass");
83+
break;
84+
}
85+
86+
ReportManager.Flush();
87+
}
88+
89+
public static void LogTestStepForBugLogger(Status status, string message)
90+
{
91+
TheLogger.Info(message);
92+
CurrentTestCase.Log(status, message);
93+
}
94+
95+
public static void GetReportManager()
96+
{
97+
if (!IsExtenetReportStarted)
98+
{
99+
IsExtenetReportStarted = true;
100+
StartReporter();
101+
}
102+
}
103+
}
104+
}
105+

SeleniumWebdriver/SeleniumWebdriver.csproj

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<Reference Include="ExcelDataReader.DataSet, Version=3.3.0.0, Culture=neutral, PublicKeyToken=93517dbe6a4012fa, processorArchitecture=MSIL">
4444
<HintPath>..\packages\ExcelDataReader.DataSet.3.3.0\lib\net45\ExcelDataReader.DataSet.dll</HintPath>
4545
</Reference>
46+
<Reference Include="ExtentReports, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
47+
<HintPath>..\packages\ExtentReports.3.1.2\lib\ExtentReports.dll</HintPath>
48+
</Reference>
49+
<Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
50+
<HintPath>..\packages\HtmlAgilityPack.1.4.9.5\lib\Net45\HtmlAgilityPack.dll</HintPath>
51+
</Reference>
4652
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
4753
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
4854
<Private>True</Private>
@@ -51,6 +57,15 @@
5157
<HintPath>..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
5258
</Reference>
5359
<Reference Include="Microsoft.CSharp" />
60+
<Reference Include="MongoDB.Bson, Version=2.4.0.70, Culture=neutral, processorArchitecture=MSIL">
61+
<HintPath>..\packages\MongoDB.Bson.2.4.0\lib\net45\MongoDB.Bson.dll</HintPath>
62+
</Reference>
63+
<Reference Include="MongoDB.Driver, Version=2.4.0.70, Culture=neutral, processorArchitecture=MSIL">
64+
<HintPath>..\packages\MongoDB.Driver.2.4.0\lib\net45\MongoDB.Driver.dll</HintPath>
65+
</Reference>
66+
<Reference Include="MongoDB.Driver.Core, Version=2.4.0.70, Culture=neutral, processorArchitecture=MSIL">
67+
<HintPath>..\packages\MongoDB.Driver.Core.2.4.0\lib\net45\MongoDB.Driver.Core.dll</HintPath>
68+
</Reference>
5469
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
5570
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
5671
</Reference>
@@ -60,6 +75,9 @@
6075
<Reference Include="Protractor, Version=0.11.0.0, Culture=neutral, processorArchitecture=MSIL">
6176
<HintPath>..\packages\Protractor.0.11.0\lib\net40\Protractor.dll</HintPath>
6277
</Reference>
78+
<Reference Include="RazorEngine, Version=3.9.0.0, Culture=neutral, PublicKeyToken=9ee697374c7e744a, processorArchitecture=MSIL">
79+
<HintPath>..\packages\RazorEngine.3.9.0\lib\net45\RazorEngine.dll</HintPath>
80+
</Reference>
6381
<Reference Include="SeleniumExtras.PageObjects, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
6482
<HintPath>..\packages\DotNetSeleniumExtras.PageObjects.3.11.0\lib\net45\SeleniumExtras.PageObjects.dll</HintPath>
6583
</Reference>
@@ -71,12 +89,18 @@
7189
<Reference Include="System.Data" />
7290
<Reference Include="System.Drawing" />
7391
<Reference Include="System.IO.Compression" />
92+
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
93+
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.0.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
94+
</Reference>
7495
<Reference Include="System.Runtime.Serialization" />
7596
<Reference Include="System.ServiceModel" />
7697
<Reference Include="System.Transactions" />
7798
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
7899
<HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
79100
</Reference>
101+
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
102+
<HintPath>..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll</HintPath>
103+
</Reference>
80104
<Reference Include="System.XML" />
81105
<Reference Include="TechTalk.SpecFlow, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0778194805d6db41, processorArchitecture=MSIL">
82106
<HintPath>..\packages\SpecFlow.2.0.0\lib\net45\TechTalk.SpecFlow.dll</HintPath>
@@ -110,6 +134,7 @@
110134
<Compile Include="ComponentHelper\ButtonHelper.cs" />
111135
<Compile Include="ComponentHelper\CheckBoxHelper.cs" />
112136
<Compile Include="ComponentHelper\ComboBoxHelper.cs" />
137+
<Compile Include="ComponentHelper\CustomWebElement.cs" />
113138
<Compile Include="ComponentHelper\FileHelper.cs" />
114139
<Compile Include="ComponentHelper\GenericHelper.cs" />
115140
<Compile Include="ComponentHelper\JavaScriptExecutor.cs" />
@@ -154,6 +179,7 @@
154179
<Compile Include="PageObject\EnterBug.cs" />
155180
<Compile Include="PageObject\HomePage.cs" />
156181
<Compile Include="PageObject\LoginPage.cs" />
182+
<Compile Include="Reports\Reporter.cs" />
157183
<Compile Include="Settings\AppConfigKeys.cs" />
158184
<Compile Include="Settings\ObjectRepository.cs" />
159185
<Compile Include="StepDefinition\Arguments.cs" />
@@ -190,6 +216,7 @@
190216
<Compile Include="TestScript\RadAutoCompleteBox\TC-AutoSuggestComboBox.cs" />
191217
<Compile Include="TestScript\RadioButton\HandleRadioButton.cs" />
192218
<Compile Include="TestScript\ScreenShot\TakeScreenShots.cs" />
219+
<Compile Include="TestScript\StringToByClass\TestPage.cs" />
193220
<Compile Include="TestScript\TestClassContext\TestClassContext.cs" />
194221
<Compile Include="TestScript\TextBox\TestTextBox.cs" />
195222
<Compile Include="TestScript\WebDriverWaiter\TestWebDeriverWait.cs" />
@@ -224,9 +251,11 @@
224251
<Generator>SpecFlowSingleFileGenerator</Generator>
225252
<LastGenOutput>TestFeature2.feature.cs</LastGenOutput>
226253
</None>
254+
<Content Include="extent-config.xml" />
227255
<Content Include="NLog.config">
228256
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
229257
</Content>
258+
<None Include="license" />
230259
<None Include="NLog.xsd">
231260
<SubType>Designer</SubType>
232261
</None>
@@ -290,10 +319,10 @@
290319
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
291320
</PropertyGroup>
292321
<Error Condition="!Exists('..\packages\Selenium.WebDriver.IEDriver.3.11.1\build\Selenium.WebDriver.IEDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.IEDriver.3.11.1\build\Selenium.WebDriver.IEDriver.targets'))" />
293-
<Error Condition="!Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.42.0.1\build\Selenium.WebDriver.ChromeDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.ChromeDriver.2.42.0.1\build\Selenium.WebDriver.ChromeDriver.targets'))" />
322+
<Error Condition="!Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.43.0\build\Selenium.WebDriver.ChromeDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.ChromeDriver.2.43.0\build\Selenium.WebDriver.ChromeDriver.targets'))" />
294323
</Target>
295324
<Import Project="..\packages\Selenium.WebDriver.IEDriver.3.11.1\build\Selenium.WebDriver.IEDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.IEDriver.3.11.1\build\Selenium.WebDriver.IEDriver.targets')" />
296-
<Import Project="..\packages\Selenium.WebDriver.ChromeDriver.2.42.0.1\build\Selenium.WebDriver.ChromeDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.42.0.1\build\Selenium.WebDriver.ChromeDriver.targets')" />
325+
<Import Project="..\packages\Selenium.WebDriver.ChromeDriver.2.43.0\build\Selenium.WebDriver.ChromeDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.43.0\build\Selenium.WebDriver.ChromeDriver.targets')" />
297326
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
298327
Other similar extension points exist, see Microsoft.Common.targets.
299328
<Target Name="BeforeBuild">

SeleniumWebdriver/TestScript/PageObject/TestPageObject.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Reflection;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Microsoft.VisualStudio.TestTools.UnitTesting;
78
using OpenQA.Selenium;
89
using OpenQA.Selenium.Support.UI;
10+
using SeleniumExtras.PageObjects;
11+
using SeleniumWebdriver.BaseClasses;
912
using SeleniumWebdriver.ComponentHelper;
1013
using SeleniumWebdriver.PageObject;
1114
using SeleniumWebdriver.Settings;
@@ -20,14 +23,29 @@ public void TestPage()
2023
{
2124
NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite());
2225
HomePage homePage = new HomePage(ObjectRepository.Driver);
26+
Console.WriteLine(DisplayElementName(homePage, "homePage.QuickSearchTextBox"));
2327
LoginPage loginPage = homePage.NavigateToLogin();
2428
EnterBug enterBug = loginPage.Login(ObjectRepository.Config.GetUsername(), ObjectRepository.Config.GetPassword());
2529
BugDetail bugDetail = enterBug.NavigateToDetail();
2630
bugDetail.SelectFromSeverity("trivial");
2731
ButtonHelper.ClickButton(By.XPath("//div[@id='header']/ul[1]/li[11]/a"));
2832
}
2933

30-
34+
public string DisplayElementName(PageBase aPageInstance,string element)
35+
{
36+
var fieldInfo = GetFieldInfo(aPageInstance, element);
37+
var memeberInfo = fieldInfo.GetCustomAttributes(true);
38+
var attribute = memeberInfo[0] as FindsByAttribute;
39+
return string.Format("How : {0} using : {1}", attribute.How, attribute.Using);
40+
}
41+
42+
public FieldInfo GetFieldInfo(PageBase aPageInstance, string element)
43+
{
44+
var elementName = element.Split('.');
45+
Type aPageType = aPageInstance.GetType();
46+
var fieldInofs = aPageType.GetFields();
47+
return fieldInofs.FirstOrDefault((x) => x.Name.Equals(elementName[1], StringComparison.OrdinalIgnoreCase));
48+
}
3149

3250

3351

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using OpenQA.Selenium;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SeleniumWebdriver.TestScript.StringToByClass
9+
{
10+
public class TestPage
11+
{
12+
private IWebDriver webDriver;
13+
14+
protected static string FileABug = "(By.Id(\"enter_bug\")), fileabug";
15+
protected static string aFileABug = "By.Id,enter_bug,fileabug";
16+
17+
public static List<string> GetElementAndValue(string ele)
18+
{
19+
return ele.Split(',').ToList();
20+
}
21+
22+
public static void ClickButton(string locator)
23+
{
24+
List<string> LocatorAndValue = GetElementAndValue(locator);
25+
By aLocator = GetLocator(LocatorAndValue);
26+
}
27+
28+
private static By GetLocator(List<string> aLocatorAndValue)
29+
{
30+
if (aLocatorAndValue[0].Equals("By.Id"))
31+
return By.Id(aLocatorAndValue[1]);
32+
else
33+
throw new Exception("Invalid Locator");
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)