Skip to content

Commit

Permalink
Add chrome user folder and make command re-runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktar Karpach committed Mar 17, 2018
1 parent c7d2685 commit eed328e
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SampleCommand\SampleCommand.cs" />
<Compile Include="SampleCommand\SampleCommandSettings.cs" />
<Compile Include="SampleCommand\SampleCommandSettingsForm.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
20 changes: 20 additions & 0 deletions Karpach.Remote.Keep.Command.Tests/Helpers/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Karpach.Remote.Keep.Command.Helpers;
using NUnit.Framework;

namespace Karpach.Remote.Keep.Command.Tests.Helpers
{
[TestFixture]
public class StringExtensionsTests
{
[TestCase(null, ExpectedResult = null)]
[TestCase("", ExpectedResult = null)]
[TestCase("C:\\Tmp\\", ExpectedResult = "C:/Tmp/")]
[TestCase("C:\\Tmp\\Default", ExpectedResult = "C:/Tmp/")]
[TestCase("C:\\Tmp\\Default\\", ExpectedResult = "C:/Tmp/")]
[TestCase("C:/Tmp/Default", ExpectedResult = "C:/Tmp/")]
public string NormalizeChromeProfileFolder_Tests(string path)
{
return path.NormalizeChromeProfileFolder();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AF8DFD8A-056D-4222-90AE-05EBD6FB10E4}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Karpach.Remote.Keep.Command.Tests</RootNamespace>
<AssemblyName>Karpach.Remote.Keep.Command.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\StringExtensionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NUnit">
<Version>3.10.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Karpach.Remote.Keep.Command\Karpach.Remote.Keep.Command.csproj">
<Project>{e2f6efe1-51f6-4f9e-a548-079ddc6352b2}</Project>
<Name>Karpach.Remote.Keep.Command</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
36 changes: 36 additions & 0 deletions Karpach.Remote.Keep.Command.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Karpach.Remote.Keep.Command.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Karpach.Remote.Keep.Command.Tests")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("af8dfd8a-056d-4222-90ae-05ebd6fb10e4")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
11 changes: 10 additions & 1 deletion Karpach.Remote.Keep.Command.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.13
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Karpach.Remote.Keep.Command", "Karpach.Remote.Keep.Command\Karpach.Remote.Keep.Command.csproj", "{E2F6EFE1-51F6-4F9E-A548-079DDC6352B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Karpach.Remote.Keep.Command.Runner", "Karpach.Remote.Keep.Command.Runner\Karpach.Remote.Keep.Command.Runner.csproj", "{4B5FCA92-BE5D-463F-A878-1E4AC4E2B6E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Karpach.Remote.Keep.Command.Tests", "Karpach.Remote.Keep.Command.Tests\Karpach.Remote.Keep.Command.Tests.csproj", "{AF8DFD8A-056D-4222-90AE-05EBD6FB10E4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,8 +23,15 @@ Global
{4B5FCA92-BE5D-463F-A878-1E4AC4E2B6E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B5FCA92-BE5D-463F-A878-1E4AC4E2B6E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B5FCA92-BE5D-463F-A878-1E4AC4E2B6E3}.Release|Any CPU.Build.0 = Release|Any CPU
{AF8DFD8A-056D-4222-90AE-05EBD6FB10E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF8DFD8A-056D-4222-90AE-05EBD6FB10E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF8DFD8A-056D-4222-90AE-05EBD6FB10E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF8DFD8A-056D-4222-90AE-05EBD6FB10E4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {736C96AB-D22C-4A36-A7C2-C027D7700F07}
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions Karpach.Remote.Keep.Command/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Text.RegularExpressions;

namespace Karpach.Remote.Keep.Command.Helpers
{
public static class StringExtensions
{
public static string NormalizeChromeProfileFolder(this string folder)
{
if (string.IsNullOrEmpty(folder))
{
return null;
}
string result = folder.Replace("\\", "/");
result = Regex.Replace(result, "/Default/?$", "/");
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\StringExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="KeepCommand.cs" />
<Compile Include="KeepCommandSettings.cs" />
Expand Down Expand Up @@ -117,16 +118,17 @@
<ItemGroup>
<None Include="Icon.ico" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<SsdtUnitTestVersion>3.1</SsdtUnitTestVersion>
</PropertyGroup>
<Import Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.Sql.UnitTesting.targets" Condition="$(VisualStudioVersion) != '15.0' And '$(SQLDBExtensionsRefPath)' != ''" />
<Import Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.Sql.UnitTesting.targets" Condition="$(VisualStudioVersion) != '15.0' And '$(SQLDBExtensionsRefPath)' != ''" />
<Import Project="..\packages\Selenium.WebDriver.ChromeDriver.2.36.0\build\Selenium.WebDriver.ChromeDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.36.0\build\Selenium.WebDriver.ChromeDriver.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<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>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.36.0\build\Selenium.WebDriver.ChromeDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.ChromeDriver.2.36.0\build\Selenium.WebDriver.ChromeDriver.targets'))" />
</Target>
</Project>
</Project>
61 changes: 38 additions & 23 deletions Karpach.Remote.Keep.Command/KeepCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Windows.Forms;
using Karpach.Remote.Commands.Base;
using Karpach.Remote.Commands.Interfaces;
using Karpach.Remote.Keep.Command.Helpers;
using NLog;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
Expand Down Expand Up @@ -41,6 +42,12 @@ public KeepCommand(Guid? id) : base(id)
{
options.AddArguments("--headless");
options.AddArguments("--disable-gpu");
}

string chromeProfileFolder = ((KeepCommandSettings) Settings).ChromeProfileFolder.NormalizeChromeProfileFolder();
if (!string.IsNullOrEmpty(chromeProfileFolder))
{
options.AddArguments($"--user-data-dir={chromeProfileFolder}");
}
return new ChromeDriver(driverService, options);
});
Expand Down Expand Up @@ -82,33 +89,41 @@ public override void RunCommand(params object[] parameters)
}
try
{
string url = "https://photos.google.com/login";
WebDriverWait wait;
ReadOnlyCollection<IWebElement> elements;
if (!string.Equals(_chromeDriver.Value.Url, url))
{
_chromeDriver.Value.Navigate().GoToUrl(url);
wait = new WebDriverWait(_chromeDriver.Value, TimeSpan.FromSeconds(KeepLoadTimeout));
elements = wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.CssSelector("#initialView #identifierId")));
if (elements.Count == 1)
{
if (!Login(elements[0]))
string keepUrl = $"https://keep.google.com/#LIST/{((KeepCommandSettings)Settings).ListId}";
if (!string.Equals(_chromeDriver.Value.Url, keepUrl, StringComparison.InvariantCultureIgnoreCase))
{
string photosUrl = "https://photos.google.com";
WebDriverWait wait;
ReadOnlyCollection<IWebElement> elements;
_chromeDriver.Value.Navigate().GoToUrl($"{photosUrl}/login");
if (!_chromeDriver.Value.Url.Contains(photosUrl))
{
wait = new WebDriverWait(_chromeDriver.Value, TimeSpan.FromSeconds(KeepLoadTimeout));
elements = wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.CssSelector("#initialView #identifierId")));
if (elements.Count == 1)
{
Logger.Log(LogLevel.Error, "Unable to login:\n" + _chromeDriver.Value.PageSource + "\n\n");
if (!Login(elements[0]))
{
Logger.Log(LogLevel.Error, "Unable to login:\n" + _chromeDriver.Value.PageSource + "\n\n");
return;
}
wait = new WebDriverWait(_chromeDriver.Value, TimeSpan.FromSeconds(KeepLoadTimeout * 5));
wait.Until(ExpectedConditions.UrlContains(photosUrl));
}
else
{
Logger.Log(LogLevel.Error, "Unable to load login page:\n" + _chromeDriver.Value.PageSource + "\n\n");
return;
}
wait = new WebDriverWait(_chromeDriver.Value, TimeSpan.FromSeconds(KeepLoadTimeout));
wait.Until(ExpectedConditions.UrlContains("https://photos.google.com"));
url = $"https://keep.google.com/#LIST/{((KeepCommandSettings)Settings).ListId}";
_chromeDriver.Value.Navigate().GoToUrl(url);
}
_chromeDriver.Value.Navigate().GoToUrl(keepUrl);
wait = new WebDriverWait(_chromeDriver.Value, TimeSpan.FromSeconds(KeepLoadTimeout));
elements = wait.Until(VisibleElementsByLocatedBy(By.XPath("//div[@aria-label='Remind me']"),1));
if (elements.Count != 1)
{
Logger.Log(LogLevel.Error, "Unable to load keep page:\n" + _chromeDriver.Value.PageSource + "\n\n");
return;
}
}
wait = new WebDriverWait(_chromeDriver.Value, TimeSpan.FromSeconds(KeepLoadTimeout));
elements = wait.Until(VisibleElementsByLocatedBy(By.XPath("//div[@aria-label='Remind me']"),1));
if (elements.Count != 1)
{
Logger.Log(LogLevel.Error, "Unable to load keep page:\n" + _chromeDriver.Value.PageSource + "\n\n");
return;
}
if (parameters != null && parameters.Length == 1)
{
Expand Down
1 change: 1 addition & 0 deletions Karpach.Remote.Keep.Command/KeepCommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class KeepCommandSettings : CommandSettingsBase
public string GoogleUserName { get; set; }
public string GooglePassword { get; set; }
public bool Headless { get; set; }
public string ChromeProfileFolder { get; set; }
}
}
Loading

0 comments on commit eed328e

Please sign in to comment.