Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear command #160

Merged
merged 2 commits into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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