Skip to content

Commit

Permalink
Merge pull request #7 from pmahend1/dev
Browse files Browse the repository at this point in the history
removed Resources.json added Edge Selenium driver nuget
  • Loading branch information
pmahend1 authored Sep 25, 2020
2 parents 9e74fff + ca99a0a commit ca9a6a7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 74 deletions.
14 changes: 14 additions & 0 deletions .Net Core/MSRewards/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ public static class Constants

public const string RememberMeCheckboxName = "KMSI";

public const string ID_P = "id_p";

public const string B_Results = "b_results";

public const string H2 = "h2";

public const string A = "a";

public const string IdSIButton9 = "idSIButton9";

public const string TitleDetailXPath = ".//div[@class='title-detail']";

public const string PointDetailXpath = ".//p[@class='pointsDetail c-subheading-3 ng-binding']";

public const string RewardsPageTitle = "Microsoft account | Home";

public const string WordsListUrl = "https://raw.githubusercontent.com/bevacqua/correcthorse/master/wordlist.json";
Expand Down
7 changes: 1 addition & 6 deletions .Net Core/MSRewards/MSRewards.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@
<StartupObject>MSRewards.Program</StartupObject>
</PropertyGroup>

<ItemGroup>
<Content Include="Resources.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.0-preview1" />
<PackageReference Include="Selenium.Firefox.WebDriver" Version="0.27.0" />
<PackageReference Include="Selenium.Support" Version="4.0.0-alpha05" />
<PackageReference Include="Selenium.WebDriver" Version="4.0.0-alpha05" />
<PackageReference Include="Selenium.WebDriver.MSEdgeDriver" Version="85.0.564.41" />
</ItemGroup>
</Project>
8 changes: 3 additions & 5 deletions .Net Core/MSRewards/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

namespace MSRewards
{
class Options
internal class Options
{
[Option('F', "use-firefox", Required = false, HelpText = "Use Firefox")]
public bool Firefox { get; set; }


[Option('E',"email", Required = true, HelpText = "Email ID")]
[Option('E', "email", Required = true, HelpText = "Email ID")]
public string Email { get; set; }

[Option('P', "password", Required = true, HelpText = "password")]
public string Password { get; set; }

}
}
}
135 changes: 79 additions & 56 deletions .Net Core/MSRewards/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CommandLine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
Expand All @@ -9,7 +8,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
Expand Down Expand Up @@ -75,19 +73,52 @@ private async Task Login(IWebDriver driverlocal, WebDriverWait localwait)

//page2
var passwordEntry = localwait?.Until(d => d.FindElement(By.Id(Constants.PasswordEntryId)));
var checkbox = driverlocal.FindElement(By.Name(Constants.RememberMeCheckboxName));
try
{
var checkbox = driverlocal.FindElement(By.Name(Constants.RememberMeCheckboxName));
checkbox?.Click();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
passwordEntry.SendKeys(password);

checkbox?.Click();

passwordEntry.SendKeys(Keys.Enter);

try
{
var dontShowThisAgain = localwait.Until(driver => driver.FindElement(By.Id(Constants.CheckboxId)));

dontShowThisAgain?.Click();

var yesButton = localwait.Until(d => d.FindElement(By.Id(Constants.IdSIButton9)));

yesButton?.Click();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
try
{
var yesButton = localwait.Until(d => d.FindElement(By.Id(Constants.IdSIButton9)));
yesButton?.Click();
}
catch (Exception ex2)
{

Debug.WriteLine(ex2.Message);
}

}

await Task.Delay(3000);

if (localwait.Until(e => e.Title.Equals(Constants.RewardsPageTitle)))
driverlocal.SwitchTo().DefaultContent();
}

private async Task Run(bool useFirefox = true)
private async Task Run(bool useFirefox = false)
{
wordList = DownloadJsonData<List<string>>(Constants.WordsListUrl);
DriverOptions driverOptions;
Expand All @@ -111,7 +142,8 @@ private async Task Run(bool useFirefox = true)

await Login(driver, wait);
var result = CheckBreakDown(driver, wait);
driver.Close();
driver?.Dispose();
driver?.Quit();
foreach (var keyvalue in result)
{
var current = keyvalue.Value.x;
Expand All @@ -121,6 +153,7 @@ private async Task Run(bool useFirefox = true)
{
Console.WriteLine("Starting Bing Search for " + keyvalue.Key);
await BingSearch(keyvalue.Key, current, expected, useFirefox);
Environment.Exit(0);
}
}
}
Expand All @@ -134,13 +167,13 @@ private async Task Run(bool useFirefox = true)

var userPointsBreakdown = waiter.Until(d => d.FindElement(By.Id(Constants.UserPointsBreakdownId)));

var titleDetailsList = waiter.Until(d => userPointsBreakdown.FindElements(By.XPath(".//div[@class='title-detail']")));
var titleDetailsList = waiter.Until(d => userPointsBreakdown.FindElements(By.XPath(Constants.TitleDetailXPath)));

foreach (var pointDetail in titleDetailsList)
{
var href = waiter.Until(p => pointDetail.FindElement(By.TagName("a")));
var href = waiter.Until(p => pointDetail.FindElement(By.TagName(Constants.A)));

var pointDetailsList = waiter.Until(d => pointDetail.FindElements(By.XPath(".//p[@class='pointsDetail c-subheading-3 ng-binding']")));
var pointDetailsList = waiter.Until(d => pointDetail.FindElements(By.XPath(Constants.PointDetailXpath)));
try
{
var pointSplits = pointDetailsList.FirstOrDefault()?.Text?.Replace(" ", "").Split("/");
Expand All @@ -164,13 +197,13 @@ private async Task Run(bool useFirefox = true)
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " \n" + ex.InnerException?.Message);
Console.WriteLine(ex.Message + Environment.NewLine + ex.InnerException?.Message);
}
}
return result;
}

private async Task BingSearch(RewardType rewardType, int current, int target, bool useFirefox = true)
private async Task BingSearch(RewardType rewardType, int current, int target, bool useFirefox = false)
{
try
{
Expand All @@ -179,63 +212,53 @@ private async Task BingSearch(RewardType rewardType, int current, int target, bo

if (rewardType == RewardType.EdgeBonus || !useFirefox)
{
using StreamReader r = new StreamReader("Resources.json");

string jsonString = r.ReadToEnd();
var jsonObject = JObject.Parse(jsonString);

r.Close();

if (jsonObject != null)
var options = new EdgeOptions
{
var edgeBrowser = JsonConvert.DeserializeObject<EdgeBrowser>(jsonObject["Edge"].ToString());
UseChromium = true,
};

var options = new EdgeOptions
{
UseChromium = true,
BinaryLocation = edgeBrowser.ExecutableName,
};
var edgeDriver = new EdgeDriver(options);
var edgeDriver = new EdgeDriver(options);

edgeDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
var edgeWait = new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(60));
edgeDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
var edgeWait = new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(60));

await Login(edgeDriver, edgeWait);
await Login(edgeDriver, edgeWait);

Search(edgeDriver, edgeWait, Constants.BingSearchURL + "Give me Edge points");
Search(edgeDriver, edgeWait, Constants.BingSearchURL + "Give me Edge points");

await Task.Delay(4000);
await Task.Delay(4000);

var id_p = edgeWait.Until(d => d.FindElement(By.Id("id_p")));
if (id_p != null)
{
id_p?.Click();
}
var id_p = edgeWait.Until(d => d.FindElement(By.Id(Constants.ID_P)));
if (id_p != null)
{
id_p?.Click();
}

while (current < target)
while (current < target)
{
Search(edgeDriver, edgeWait, Constants.BingSearchURL + wordList[rand.Next(wordList.Count)]);
current += 5;
if (current >= target)
{
Search(edgeDriver, edgeWait, Constants.BingSearchURL + wordList[rand.Next(wordList.Count)]);
current += 5;
if (current >= target)
{
var currentBreakDown = CheckBreakDown(edgeDriver, edgeWait);
var currentBreakDown = CheckBreakDown(edgeDriver, edgeWait);

if (currentBreakDown.ContainsKey(rewardType))
{
current = currentBreakDown[rewardType].x;
Console.WriteLine("{0} points of {1} completed", currentBreakDown[rewardType].x, currentBreakDown[rewardType].y);
}
if (currentBreakDown.ContainsKey(rewardType))
{
current = currentBreakDown[rewardType].x;
Console.WriteLine("{0} points of {1} completed", currentBreakDown[rewardType].x, currentBreakDown[rewardType].y);
}
}
edgeDriver.Close();
}
edgeDriver?.Dispose();
edgeDriver?.Quit();
//}
}
//Use Firefox
else
{
var options = new FirefoxOptions();
if (rewardType == RewardType.Mobile)
options.SetPreference(Constants.UserAgentKey, Constants.EdgeUserAgent);
options.SetPreference(Constants.UserAgentKey, Constants.MobileUserAgent);

options.SetPreference(Constants.PrivateBrowsingKey, true);
using var firefoxDriver = new FirefoxDriver(options);
Expand All @@ -246,7 +269,7 @@ private async Task BingSearch(RewardType rewardType, int current, int target, bo

await Task.Delay(4000);

var id_p = driverWait.Until(d => d.FindElement(By.Id("id_p")));
var id_p = driverWait.Until(d => d.FindElement(By.Id(Constants.ID_P)));
if (id_p != null)
{
id_p.Click();
Expand All @@ -267,30 +290,30 @@ private async Task BingSearch(RewardType rewardType, int current, int target, bo
}
}
}
firefoxDriver.Close();
firefoxDriver?.Dispose();
firefoxDriver?.Quit();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
}

}

private void Search(IWebDriver driver, WebDriverWait wait, string url)
{
try
{
driver.Navigate().GoToUrl(url);
wait.Until(e => e.FindElement(By.Id("b_results")));
wait.Until(e => e.FindElement(By.Id(Constants.B_Results)));

var result = driver.FindElement(By.TagName("h2"));
var result = driver.FindElement(By.TagName(Constants.H2));
Console.WriteLine(result?.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\n" + ex.InnerException?.Message);
Console.WriteLine(ex.Message + Environment.NewLine + ex.InnerException?.Message);
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions .Net Core/MSRewards/Resources.json

This file was deleted.

0 comments on commit ca9a6a7

Please sign in to comment.