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

Refactor element model #51

Merged
merged 12 commits into from
Jul 28, 2015
2 changes: 1 addition & 1 deletion Winium/TestApp.Test/py-functional/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
'TestApp.WindowsPhone_1.0.0.0_AnyCPU{0}.appx'.format(CONFIG_IDENTIFIER)

DESIRED_CAPABILITIES = {
"app": os.path.abspath(os.path.join(BASE_DIR, APPX_PATH))
"app": os.path.abspath(os.path.join(BASE_DIR, APPX_PATH)),
# "debugConnectToRunningApp": True
}
10 changes: 8 additions & 2 deletions Winium/TestApp.Test/py-functional/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# coding: utf-8
import pytest
from selenium.webdriver import Remote
from selenium.webdriver.support.wait import WebDriverWait
import config


class WuaTestCase:
class WuaTestCase(object):
"""
If True, then new session is created when test class is being setup,
i.e. one session is used for all test methods in class.
Expand Down Expand Up @@ -38,4 +40,8 @@ def setup_class(cls):
@classmethod
def teardown_class(cls):
if cls.__shared_session__:
WuaTestCase._destroy_session(cls)
WuaTestCase._destroy_session(cls)

@pytest.fixture
def waiter(self):
return WebDriverWait(self.driver, timeout=5)
39 changes: 27 additions & 12 deletions Winium/TestApp.Test/py-functional/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import pytest
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from tests import WuaTestCase


Expand Down Expand Up @@ -48,7 +47,8 @@ def test_get_page_source(self):
visual_root = next(root.iterfind('*'))
assert 'Windows.UI.Xaml.Controls.ScrollViewer' == visual_root.tag
assert sum(1 for _ in root.iterfind('*')) > 1, 'Page source should contain at both visual root and popups'
assert any(visual_root.iterfind('*')), 'Page source should contain at least one children of visual root'
first_child = next(visual_root.iterfind('*'), next)
assert first_child is not None, 'Page source should contain at least one children of visual root'

@pytest.mark.parametrize(("by", "value"), [
(By.ID, 'MyTextBox'),
Expand Down Expand Up @@ -164,17 +164,34 @@ def test_is_displayed(self):
class TestAlert(WuaTestCase):
# __shared_session__ = False

def test_get_alert_text(self):
@pytest.fixture
def second_tab(self, waiter):
pivots = self.driver.find_elements_by_class_name("Windows.UI.Xaml.Controls.Primitives.PivotHeaderItem")
pivots[1].click()
tab = self.driver.find_element_by_id('SecondTab')
waiter.until(EC.visibility_of(tab))
return tab

@pytest.fixture
def alert(self, second_tab, waiter):
second_tab.find_element_by_id('MsgBtn').click()
return waiter.until(EC.alert_is_present())

def test_get_alert_text(self, alert):
"""GET /session/:sessionId/alert_text Gets the text of the currently displayed alert"""
pytest.skip('TODO')
assert alert.text

def test_accept_alert(self):
def test_accept_alert(self, alert):
"""POST /session/:sessionId/accept_alert Accepts the currently displayed alert dialog."""
pytest.skip('TODO')
alert.accept()
textbox_value = self.driver.find_element_by_id('SecondTabTextBox').text
assert 'Accepted' == textbox_value

def test_dismiss_alert(self):
def test_dismiss_alert(self, alert):
"""POST /session/:sessionId/dismiss_alert Dismisses the currently displayed alert dialog."""
pytest.skip('TODO')
alert.dismiss()
textbox_value = self.driver.find_element_by_id('SecondTabTextBox').text
assert 'Dismissed' == textbox_value


class TestExecuteScript(WuaTestCase):
Expand Down Expand Up @@ -265,14 +282,12 @@ def test_touch_actions(self):


class TestAutoSuggestBox(WuaTestCase):
def test_select_suggest(self):
def test_select_suggest(self, waiter):
self.driver.execute_script("mobile: OnScreenKeyboard.Disable")

pivots = self.driver.find_elements_by_class_name("Windows.UI.Xaml.Controls.Primitives.PivotHeaderItem")
pivots[1].click()

waiter = WebDriverWait(self.driver, timeout=5)

autosuggestion_box = waiter.until(EC.presence_of_element_located((By.ID, 'MySuggestBox')))
autosuggestion_input = autosuggestion_box.find_element_by_class_name('Windows.UI.Xaml.Controls.TextBox')
autosuggestion_input.send_keys('A')
Expand Down
6 changes: 4 additions & 2 deletions Winium/TestApp/TestApp.WindowsPhone/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@
<PivotItem Header="second" AutomationProperties.AutomationId="SecondTab">
<StackPanel Orientation="Vertical">
<TextBox
x:Name="SecondTabTextBox"
Text ="Nice swipe!"
AutomationProperties.AutomationId="SecondTabTextBox" />


<AutoSuggestBox x:Name="Suggestions"
AutomationProperties.AutomationId ="MySuggestBox"
ItemsSource="{Binding }"
TextChanged="SuggestionsTextChanged"
SuggestionChosen="SuggestionsSuggestionChosen"></AutoSuggestBox>
<PasswordBox AutomationProperties.AutomationId="MyPasswordBox" PlaceholderText="Password"/>
<Button Content="Show Msg"
Click="ButtonClick"
AutomationProperties.AutomationId="MsgBtn" />
</StackPanel>
</PivotItem>
</Pivot>
Expand Down
16 changes: 15 additions & 1 deletion Winium/TestApp/TestApp.WindowsPhone/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void SetButtonOnClick(object sender, RoutedEventArgs e)

private void MainPageOnLoaded(object sender, RoutedEventArgs e)
{
AutomationServer.Instance.InitializeAndStart(Frame);
AutomationServer.Instance.InitializeAndStart();
}

private void GoAppBarButtonOnClick(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -128,5 +128,19 @@ private void SuggestionsTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextCha
sender.ItemsSource = suggestions;
}
}

private async void ButtonClick(object sender, RoutedEventArgs e)
{
var noWifiDialog = new ContentDialog
{
Title = "No wifi connection",
Content = "Check connection and try again",
PrimaryButtonText = "Ok",
SecondaryButtonText = "Cancel"
};
var result = await noWifiDialog.ShowAsync();

this.SecondTabTextBox.Text = result == ContentDialogResult.Primary ? "Accepted" : "Dismissed";
}
}
}
20 changes: 5 additions & 15 deletions Winium/Winium.StoreApps.InnerServer/AutomationServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.UI.Xaml;

using Winium.StoreApps.Common;

Expand Down Expand Up @@ -38,16 +37,14 @@ public class AutomationServer
#region Public Methods and Operators

/// <summary>
/// Initializes and starts <see cref="AutomationServer"/> on default port (9998) with specified parameters.
/// Initializes and starts <see cref="AutomationServer"/> on default port (9998).
/// </summary>
/// <remarks>
/// Use it in conjuction with <see cref="Instance"/> to simplify inclusion of server in tested app.
/// </remarks>
/// <param name="visualRoot">
/// </param>
public void InitializeAndStart(UIElement visualRoot)
public void InitializeAndStart()
{
this.SetAutomator(visualRoot);
this.automator = new Automator();
this.Start(9998);
}

Expand All @@ -57,21 +54,14 @@ public void InitializeAndStart(UIElement visualRoot)
/// <remarks>
/// Use it in conjuction with <see cref="Instance"/> to simplify inclusion of server in tested app.
/// </remarks>
/// <param name="visualRoot">
/// </param>
/// <param name="port">
/// </param>
public void InitializeAndStart(UIElement visualRoot, int port)
public void InitializeAndStart(int port)
{
this.SetAutomator(visualRoot);
this.automator = new Automator();
this.Start(port);
}

public void SetAutomator(UIElement visualRoot)
{
this.automator = new Automator(visualRoot);
}

public async void Start(int port)
{
if (this.isServerActive)
Expand Down
46 changes: 7 additions & 39 deletions Winium/Winium.StoreApps.InnerServer/Automator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;

using Winium.StoreApps.Common;
using Winium.StoreApps.InnerServer.Commands;
Expand All @@ -21,10 +21,10 @@ internal class Automator
{
#region Constructors and Destructors

public Automator(UIElement visualRoot)
public Automator()
{
this.VisualRoot = GetTrueVisualRoot(visualRoot);
this.WebElements = new AutomatorElements();
this.UiThreadDispatcher = Window.Current.Dispatcher;
this.ElementsRegistry = new ElementsRegistry();
this.DoAfterResponseOnce = null;
}

Expand All @@ -34,9 +34,9 @@ public Automator(UIElement visualRoot)

public Action DoAfterResponseOnce { get; set; }

public UIElement VisualRoot { get; private set; }
public CoreDispatcher UiThreadDispatcher { get; private set; }

public AutomatorElements WebElements { get; private set; }
public ElementsRegistry ElementsRegistry { get; private set; }

#endregion

Expand Down Expand Up @@ -89,13 +89,9 @@ public string ProcessCommand(string content)
{
commandToExecute = new ElementsCommand { ElementId = elementId };
}
else if (command.Equals(DriverCommand.ClickElement))
{
commandToExecute = new ClickCommand { ElementId = elementId };
}
else if (command.Equals(DriverCommand.SendKeysToElement))
{
var values = ((JArray)parameters["value"]).ToObject<List<string>>();
var values = parameters["value"].ToObject<List<string>>();
var value = string.Empty;
if (values.Any())
{
Expand Down Expand Up @@ -159,33 +155,5 @@ public string ProcessCommand(string content)
}

#endregion

#region Methods

/// <summary>
/// Walk visual tree upwards til we find actual visual root.
/// This fixes incorrect locations being returned for elements when app screen is scrolled up to make place for onscreen keyboards, etc (issue #34 ).
/// </summary>
/// <param name="visualRoot">
/// </param>
/// <returns>
/// The <see cref="UIElement"/>.
/// </returns>
private static UIElement GetTrueVisualRoot(UIElement visualRoot)
{
var root = visualRoot;
while (true)
{
var parent = VisualTreeHelper.GetParent(root) as UIElement;
if (parent == null)
{
return root;
}

root = parent;
}
}

#endregion
}
}
87 changes: 0 additions & 87 deletions Winium/Winium.StoreApps.InnerServer/AutomatorElements.cs

This file was deleted.

Loading