Skip to content

Commit

Permalink
Merge pull request 2gis#160 from 2gis/clear-command
Browse files Browse the repository at this point in the history
Add clear command
Fix driver crashing on empty request
  • Loading branch information
NickAb authored and bayandin committed Oct 7, 2016
2 parents 6d10f80 + edde0e8 commit bf48659
Show file tree
Hide file tree
Showing 22 changed files with 158 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ publish/
*.pubxml

# NuGet Packages
**/NuGet.exe
packages
*.nupkg
## TODO: If the tool you use requires repositories.config
Expand Down Expand Up @@ -184,4 +185,4 @@ UpgradeLog*.htm
*.bim_*.settings

# Microsoft Fakes
FakesAssemblies/
FakesAssemblies/
33 changes: 31 additions & 2 deletions Winium/TestApp.Test/py-functional/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def test_attribute_set(self, attribute, value):
assert str(value) == element.get_attribute(attribute)


class TestBasicInput(WuaTestCase):
class TestBasicInput(UsesSecondTab):
__shared_session__ = False

def test_send_keys_to_element(self):
Expand All @@ -285,6 +285,34 @@ def test_send_keys_to_element(self):
element.send_keys(actual_input)
assert actual_input.replace(Keys.ENTER, '\r\n') == element.text

def test_send_keys_to_number_input(self, second_tab):
"""
POST /session/:sessionId/element/:id/value Send a sequence of key strokes to an element.
TODO: test magic keys
"""
actual_input = '123'
element = second_tab.find_element_by_id('NumericInput')
element.send_keys(actual_input)
assert actual_input == element.text

def test_clear_number_input(self, second_tab):
"""
POST /session/:sessionId/element/:id/value Send a sequence of key strokes to an element.
TODO: test magic keys
"""
actual_input = '123'
element = second_tab.find_element_by_id('NumericInput')
element.send_keys(actual_input)
element.clear()
assert '' == element.text

def test_clear_element(self):
actual_input = 'Some test string' + Keys.ENTER
element = self.driver.find_element_by_id('MyTextBox')
element.send_keys(actual_input)
element.clear()
assert '' == element.text

def test_send_keys_to_active_element(self):
element = self.driver.find_element_by_id('MyTextBox')
element.click()
Expand All @@ -308,7 +336,8 @@ def test_back(self):
def test_click_element(self):
element = self.driver.find_element_by_id('SetButton')
element.click()
assert 'CARAMBA' == self.driver.find_element_by_id('MyTextBox').text
actual_value = self.driver.find_element_by_id('MyTextBox').text
assert 'CARAMBA' == actual_value


@pytest.mark.skipif(True, reason="TODO")
Expand Down
1 change: 1 addition & 0 deletions Winium/TestApp/TestApp.WindowsPhone/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Button Content="Disabled"
IsEnabled="False"
AutomationProperties.AutomationId="DisabledBtn" />
<TextBox InputScope="Number" AutomationProperties.AutomationId="NumericInput"/>
</StackPanel>
</PivotItem>
</Pivot>
Expand Down
28 changes: 18 additions & 10 deletions Winium/Winium.StoreApps.Driver/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,27 @@ public void StartListening()
using (var stream = client.GetStream())
{
var acceptedRequest = HttpRequest.ReadFromStreamWithoutClosing(stream);
Logger.Debug("ACCEPTED REQUEST {0}", acceptedRequest.StartingLine);

var response = this.HandleRequest(acceptedRequest);
using (var writer = new StreamWriter(stream))
if (string.IsNullOrWhiteSpace(acceptedRequest.StartingLine))
{
try
{
writer.Write(response);
writer.Flush();
}
catch (IOException ex)
Logger.Warn("ACCEPTED EMPTY REQUEST");
}
else
{
Logger.Debug("ACCEPTED REQUEST {0}", acceptedRequest.StartingLine);

var response = this.HandleRequest(acceptedRequest);
using (var writer = new StreamWriter(stream))
{
Logger.Error("Error occured while writing response: {0}", ex);
try
{
writer.Write(response);
writer.Flush();
}
catch (IOException ex)
{
Logger.Error("Error occured while writing response: {0}", ex);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<mp:PhoneIdentity PhoneProductId="403a7f27-620f-4eb3-a1b5-909f9bc0eb25" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

<Properties>
<DisplayName>Winium.Storeapps.InnerServer.Tests</DisplayName>
<DisplayName>Winium.StoreApps.InnerServer.Tests</DisplayName>
<PublisherDisplayName>n.abalov</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
Expand All @@ -21,14 +21,14 @@
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App"
Executable="vstest.executionengine.appcontainer.exe"
<Application Id="App"
Executable="vstest.executionengine.appcontainer.exe"
EntryPoint="Microsoft.VisualStudio.TestPlatform.TestExecutor.AppContainer.App">
<m3:VisualElements
DisplayName="Winium.Storeapps.InnerServer.Tests"
DisplayName="Winium.StoreApps.InnerServer.Tests"
Square150x150Logo="Assets\Logo.png"
Square44x44Logo="Assets\SmallLogo.png"
Description="Winium.Storeapps.InnerServer.Tests"
Description="Winium.StoreApps.InnerServer.Tests"
ForegroundText="light"
BackgroundColor="#464646">
<m3:DefaultTile Wide310x150Logo="Assets\WideLogo.png" Square71x71Logo="Assets\Square71x71Logo.png"/>
Expand All @@ -39,5 +39,5 @@
</Applications>
<Capabilities>
<Capability Name="internetClientServer" />
</Capabilities>
</Package>
</Capabilities>
</Package>
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// 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("Winium.Storeapps.InnerServer.Tests")]
[assembly: AssemblyTitle("Winium.StoreApps.InnerServer.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Winium.Storeapps.InnerServer.Tests")]
[assembly: AssemblyProduct("Winium.StoreApps.InnerServer.Tests")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand Down
4 changes: 4 additions & 0 deletions Winium/Winium.StoreApps.InnerServer/Automator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public string ProcessCommand(string content)

commandToExecute = new ValueCommand { ElementId = elementId, KeyString = value };
}
else if (command.Equals(DriverCommand.ClearElement))
{
commandToExecute = new ClearCommand { ElementId = elementId };
}
else if (command.Equals(DriverCommand.GetElementText))
{
commandToExecute = new TextCommand { ElementId = elementId };
Expand Down
41 changes: 41 additions & 0 deletions Winium/Winium.StoreApps.InnerServer/Commands/ClearCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Winium.StoreApps.InnerServer.Commands
{
#region

using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Automation.Provider;
using Windows.UI.Xaml.Controls;

using Winium.StoreApps.Common;
using Winium.StoreApps.Common.Exceptions;
using Winium.StoreApps.InnerServer.Commands.Helpers;

#endregion

internal class ClearCommand : CommandBase
{
#region Public Properties

public string ElementId { get; set; }

#endregion

#region Public Methods and Operators

protected override string DoImpl()
{
var element = this.Automator.ElementsRegistry.GetRegisteredElement(this.ElementId);
var control = element.Element as Control;
if (control == null)
{
throw new AutomationException("Element referenced is not of control type.", ResponseStatus.UnknownError);
}

control.TrySetText(string.Empty);
return this.JsonResponse();
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Winium.StoreApps.InnerServer.Commands.Helpers
{
#region

using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Automation.Provider;
using Windows.UI.Xaml.Controls;

using Winium.StoreApps.Common;
using Winium.StoreApps.Common.Exceptions;

#endregion

internal static class ContorlExtensions
{
internal static void TrySetText(this Control element, string text)
{
// TODO Research why TextBox does not support IValueProvider
var provider = element.GetProviderOrDefault<IValueProvider>(PatternInterface.Value);

if (provider != null)
{
provider.SetValue(text);
}
else if (element is TextBox)
{
var textBox = element as TextBox;
textBox.Text = text;
textBox.SelectionStart = text.Length;
}
else if (element is PasswordBox)
{
var passwordBox = element as PasswordBox;
passwordBox.Password = text;
}
else
{
throw new AutomationException("Element does not support SendKeys.", ResponseStatus.UnknownError);
}

// TODO: new parameter - FocusState
element.Focus(FocusState.Pointer);
}
}
}
35 changes: 1 addition & 34 deletions Winium/Winium.StoreApps.InnerServer/Commands/ValueCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,10 @@ protected override string DoImpl()
throw new AutomationException("Element referenced is not of control type.", ResponseStatus.UnknownError);
}

TrySetText(control, this.KeyString);
control.TrySetText(this.KeyString);
return this.JsonResponse();
}

#endregion

#region Methods

private static void TrySetText(Control element, string text)
{
// TODO Research why TextBox does not support IValueProvider
var provider = element.GetProviderOrDefault<IValueProvider>(PatternInterface.Value);

if (provider != null)
{
provider.SetValue(text);
}
else if (element is TextBox)
{
var textBox = element as TextBox;
textBox.Text = text;
textBox.SelectionStart = text.Length;
}
else if (element is PasswordBox)
{
var passwordBox = element as PasswordBox;
passwordBox.Password = text;
}
else
{
throw new AutomationException("Element does not support SendKeys.", ResponseStatus.UnknownError);
}

// TODO: new parameter - FocusState
element.Focus(FocusState.Pointer);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
<Compile Include="AcceptedRequest.cs" />
<Compile Include="AutomationServer.cs" />
<Compile Include="Automator.cs" />
<Compile Include="Commands\ClearCommand.cs" />
<Compile Include="Commands\GetElementRectCommand.cs" />
<Compile Include="Commands\GetElementSizeCommand.cs" />
<Compile Include="Commands\Helpers\ControlExtensions.cs" />
<Compile Include="Commands\Helpers\IDictionaryExtensions.cs" />
<Compile Include="Commands\IsElementEnabledCommand.cs" />
<Compile Include="ElementsRegistry.cs" />
Expand Down
6 changes: 3 additions & 3 deletions Winium/Winium.StoreApps.Inspector/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public partial class MainWindow : INotifyPropertyChanged
{
#region Constants

private const string WiniumStoreappsDriver = "Winium.StoreApps.Driver";
private const string WiniumStoreAppsDriver = "Winium.StoreApps.Driver";

#endregion

Expand Down Expand Up @@ -215,7 +215,7 @@ protected void RaisePropertyChanged([CallerMemberName] string propertyName = nul

private static bool IsDriverRunning()
{
var pname = Process.GetProcessesByName(WiniumStoreappsDriver);
var pname = Process.GetProcessesByName(WiniumStoreAppsDriver);
return pname.Length != 0;
}

Expand Down Expand Up @@ -257,7 +257,7 @@ private bool ConnectToDriver(ICapabilities dc)
: string.Format(
CultureInfo.CurrentCulture,
"It seems, {0} is not running on {1}.\n\n{2}",
WiniumStoreappsDriver,
WiniumStoreAppsDriver,
this.CommandExecutor,
e.Message);
this.Dispatcher.Invoke(
Expand Down

0 comments on commit bf48659

Please sign in to comment.