/// <summary>
 /// Call back method to inform this vm which item is selected
 /// </summary>
 /// <param name="node">node that the selection was changed</param>
 private void vm_SelectionChanged(UIAutomatorNodeVM node)
 {
     if (null != node && node.IsSelected)
     {
         SelectedNode = node;
     }
 }
        private void _ExecuteTapCommand()
        {
            UIAutomatorNodeVM vmCurrent = SelectedNode;

            try
            {
                _Driver.FindElementById(SelectedNode.Id).Click();
            }
            catch (Exception e)
            {
                MessageBox.Show(String.Format("Error performing tap action: {0}", e.Message), "Inspector Action Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                _ExecuteRefreshCommand();
            }
        }
        private void _RefreshItems(object state)
        {
            bool firstTime = false;

            // start the driver
            if (_Driver == null)
            {
                firstTime = true;
                Message   = "Attempting to connect to Appium";

                try
                {
                    Dictionary <string, object> capsDef = new Dictionary <string, object>();

                    // Only set automation name if it isn't equal to the default
                    if (_Settings.AutomationName != "Appium")
                    {
                        capsDef.Add("automationName", _Settings.AutomationName);
                    }

                    if (_Settings.UseDeviceName && _Settings.DeviceName != "")
                    {
                        capsDef.Add("deviceName", _Settings.DeviceName);
                    }

                    if (!_Settings.UseAndroidBrowser)
                    {
                        if (_Settings.UseApplicationPath && _Settings.ApplicationPath != "")
                        {
                            capsDef.Add("app", _Settings.ApplicationPath);
                        }

                        if (_Settings.UseAndroidActivity && _Settings.AndroidActivity != "")
                        {
                            capsDef.Add("appActivity", _Settings.AndroidActivity);
                        }

                        if (_Settings.UseAndroidPackage && _Settings.AndroidPackage != "")
                        {
                            capsDef.Add("appPackage", _Settings.AndroidPackage);
                        }

                        if (_Settings.UseAndroidWaitForActivity && _Settings.AndroidWaitForActivity != "")
                        {
                            capsDef.Add("appWaitActivity", _Settings.AndroidWaitForActivity);
                        }

                        if (_Settings.UseAndroidWaitForPackage && _Settings.AndroidWaitForPackage != "")
                        {
                            capsDef.Add("appWaitPackage", _Settings.AndroidWaitForPackage);
                        }
                    }
                    else
                    {
                        capsDef.Add("browserName", _Settings.AndroidBrowser);
                    }

                    if (_Settings.UseAndroidDeviceReadyTimeout && _Settings.AndroidDeviceReadyTimeout.ToString() != "")
                    {
                        capsDef.Add("deviceReadyTimeout", _Settings.AndroidDeviceReadyTimeout.ToString());
                    }

                    if (_Settings.UseCoverageClass && _Settings.CoverageClass != "")
                    {
                        capsDef.Add("androidCoverage", _Settings.CoverageClass);
                    }

                    if (_Settings.UseAndroidIntentAction && _Settings.AndroidIntentAction != "")
                    {
                        capsDef.Add("intentAction", _Settings.AndroidIntentAction);
                    }

                    if (_Settings.UseAndroidIntentCategory && _Settings.AndroidIntentCategory != "")
                    {
                        capsDef.Add("intentCategory", _Settings.AndroidIntentCategory);
                    }

                    if (_Settings.UseAndroidIntentFlags && _Settings.AndroidIntentFlags != "")
                    {
                        capsDef.Add("intentFlags", _Settings.AndroidIntentFlags);
                    }

                    if (_Settings.UseAndroidIntentArguments && _Settings.AndroidIntentArguments != "")
                    {
                        capsDef.Add("optionalIntentArguments", _Settings.AndroidIntentArguments);
                    }

                    // Include the platform if any of the capabilities were set
                    if (capsDef.Count != 0 && _Settings.PlatformName != "")
                    {
                        capsDef.Add("platformName", _Settings.PlatformName);
                    }

                    _Driver = new AppiumDriver(new Uri(String.Format("http://{0}:{1}/wd/hub", _Settings.IPAddress, _Settings.Port)), new DesiredCapabilities(capsDef));
                }
                catch
                {
                    MessageBox.Show("Failed to connect to the server. Please check that it is running.", "Inspector Connection Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    Message = "Failed to connect to Appium";
                }

                if (_Driver == null)
                {
                    if (_Settings.UseRemoteServer)
                    {
                        Message = "Failed to connect to remote Appium server";
                    }
                    else
                    {
                        Message = "Failed to connect to Appium";
                    }

                    return;
                }
            }

            Message = "Updating";

            string pageSource = null;

            // grab the page source and parse it
            try
            {
                if (!string.IsNullOrWhiteSpace(pageSource = _Driver.PageSource))
                {
                    _CleanUpRoot();
                    var root = _ConvertToUIAutomatorNode(pageSource);
                    _RootNode = new NodeTree <UIAutomatorNodeVM>();
                    UIAutomatorNodeVM vm = new UIAutomatorNodeVM(root, vm_SelectionChanged);
                    _RootNode.Add(vm);
                    FirePropertyChanged(() => RootNode);
                }
                else
                {
                    Message = "Error getting page source";
                }
            }
            catch (Exception e)
            {
                Message = String.Format("Error getting source page: {0}", e.Message);
            }

            if (firstTime)
            {
                // added sleep timer here since the setup I had was pulling in the "previous" picture
                System.Threading.Thread.Sleep(300);
            }

            // grab the image
            Screenshot screenshot = null;

            try
            {
                screenshot = _Driver.GetScreenshot();
            }
            catch (Exception e)
            {
                Message = String.Format("Error getting screenshot: {0}", e.Message);
            }
            finally
            {
                if (screenshot != null)
                {
                    ImageByteArray = screenshot.AsByteArray;

                    if (ImageByteArray == null)
                    {
                        Message = "Error getting screenshot";
                    }
                }
                else
                {
                    Message = "Error getting screenshot";
                }
            }

            // Show success message if the "Updating" message was not changed
            if (Message.Equals("Updating"))
            {
                Message = string.Format("Last successfully updated on {0}", DateTime.Now);
            }
        }
        private void _RefreshItems(object state)
        {
            bool firstTime = false;
            // start the driver
            if (!_Driver.IsStarted)
            {
                firstTime = true;
                string errorMessage;
                if (_Settings.UseRemoteServer)
                {
                    Message = "Attempting to connect to remote server at " + _Settings.IPAddress;
                } else
                    Message = "Starting Selenium Driver";
                if (!_Driver.Start(out errorMessage))
                {
                    Console.WriteLine("error: {0}", errorMessage);
                    if (_Settings.UseRemoteServer)
                    {
                        Message = "Failed to connect (please check the configuration and that the remote server is online).";
                    } else
                    {
                        Message = "Error Starting Selenium Driver";
                    }
                    return;
                }
            }
            Message = "Updating";

            string pageSource = null;
            // grab the page source and parse it
            if (!string.IsNullOrWhiteSpace(pageSource = _Driver.GetPageSource()))
            {
                _CleanUpRoot();
                var root = _ConvertToUIAutomatorNode(pageSource);
                _RootNode = new NodeTree<UIAutomatorNodeVM>();
                UIAutomatorNodeVM vm = new UIAutomatorNodeVM(root, vm_SelectionChanged);
                _RootNode.Add(vm);
                FirePropertyChanged(() => RootNode);
            }
            else
            {
                Message = "Error getting page source";
            }

            if (firstTime)
            {
                // added sleep timer here since the setup I had was pulling in the "previous" picture
                System.Threading.Thread.Sleep(300);
            }

            // grab the image
            ImageByteArray = _Driver.GetScreenshot();
            if (ImageByteArray == null)
            {
                Message = "Error getting screenshot";
            }

            // Show success message if the "Updating" message was not changed
            if (Message.Equals("Updating"))
            {
                Message = string.Format("Last successfully updated on {0}", DateTime.Now);
            }
        }
 /// <summary>
 /// Call back method to inform this vm which item is selected
 /// </summary>
 /// <param name="node">node that the selection was changed</param>
 private void vm_SelectionChanged(UIAutomatorNodeVM node)
 {
     if (null != node && node.IsSelected)
     {
         SelectedNode = node;
     }
 }
 public bool SendKeys(UIAutomatorNodeVM node, string value)
 {
     var button = _Driver.FindElementByName(node.Id);
     try
     {
         button.SendKeys(value);
         return true;
     }
     catch (Exception ex)
     {    // Probably this item does not support keyboard input (the return from Appium 1.0.2 is too generic to recognise difference between unclickable and a system error).
         Console.WriteLine("Failed to execute click : {0}", ex.Message);
     }
     return false;
 }
 public bool Tap(UIAutomatorNodeVM node)
 {
     var button = _Driver.FindElementByName(node.Id);
     bool SuccessfullTap = false;
     try
     {
         button.Click();
         SuccessfullTap = true;
     }
     catch (Exception ex)
     {    // It's probable this item does not support tapping.
         Console.WriteLine("Failed to tap element : {0} (will try alternate method)", ex.Message);
     }
     if (!SuccessfullTap)
     {
         try
         {
             // TODO: Alternate method of tapping.
             /*                    _Driver.Driver.Mouse.Click( button.Location.X);
                                 d.SingleTap(button);
                                 _ExecuteRefreshCommand();
                                 SuccessfullTap = true;  */
         }
         catch (Exception ex)
         {
             Console.WriteLine("Failed to use mouse to Tap element : {0}", ex.Message);
         }
     }
     return SuccessfullTap;
 }
        private void _RefreshItems(object state)
        {
            bool firstTime = false;
            // start the driver
            if (_Driver == null)
            {
                firstTime = true;
                Message = "Attempting to connect to Appium";

                try
                {
                    Dictionary<string, object> capsDef = new Dictionary<string, object>();

                    // Only set automation name if it isn't equal to the default
                    if (_Settings.AutomationName != "Appium")
                    {
                        capsDef.Add("automationName", _Settings.AutomationName);
                    }

                    if (_Settings.UseDeviceName && _Settings.DeviceName != "")
                    {
                        capsDef.Add("deviceName", _Settings.DeviceName);
                    }

                    if (!_Settings.UseAndroidBrowser)
                    {
                        if (_Settings.UseApplicationPath && _Settings.ApplicationPath != "")
                        {
                            capsDef.Add("app", _Settings.ApplicationPath);
                        }

                        if (_Settings.UseAndroidActivity && _Settings.AndroidActivity != "")
                        {
                            capsDef.Add("appActivity", _Settings.AndroidActivity);
                        }

                        if (_Settings.UseAndroidPackage && _Settings.AndroidPackage != "")
                        {
                            capsDef.Add("appPackage", _Settings.AndroidPackage);
                        }

                        if (_Settings.UseAndroidWaitForActivity && _Settings.AndroidWaitForActivity != "")
                        {
                            capsDef.Add("appWaitActivity", _Settings.AndroidWaitForActivity);
                        }

                        if (_Settings.UseAndroidWaitForPackage && _Settings.AndroidWaitForPackage != "")
                        {
                            capsDef.Add("appWaitPackage", _Settings.AndroidWaitForPackage);
                        }
                    }
                    else
                    {
                        capsDef.Add("browserName", _Settings.AndroidBrowser);
                    }

                    if (_Settings.UseAndroidDeviceReadyTimeout && _Settings.AndroidDeviceReadyTimeout.ToString() != "")
                    {
                        capsDef.Add("deviceReadyTimeout", _Settings.AndroidDeviceReadyTimeout.ToString());
                    }

                    if (_Settings.UseCoverageClass && _Settings.CoverageClass != "")
                    {
                        capsDef.Add("androidCoverage", _Settings.CoverageClass);
                    }

                    if (_Settings.UseAndroidIntentAction && _Settings.AndroidIntentAction != "")
                    {
                        capsDef.Add("intentAction", _Settings.AndroidIntentAction);
                    }

                    if (_Settings.UseAndroidIntentCategory && _Settings.AndroidIntentCategory != "")
                    {
                        capsDef.Add("intentCategory", _Settings.AndroidIntentCategory);
                    }

                    if (_Settings.UseAndroidIntentFlags && _Settings.AndroidIntentFlags != "")
                    {
                        capsDef.Add("intentFlags", _Settings.AndroidIntentFlags);
                    }

                    if (_Settings.UseAndroidIntentArguments && _Settings.AndroidIntentArguments != "")
                    {
                        capsDef.Add("optionalIntentArguments", _Settings.AndroidIntentArguments);
                    }

                    // Include the platform if any of the capabilities were set
                    if (capsDef.Count != 0 && _Settings.PlatformName != "")
                    {
                        capsDef.Add("platformName", _Settings.PlatformName);
                    }

                    _Driver = new AppiumDriver(new Uri(String.Format("http://{0}:{1}/wd/hub", _Settings.IPAddress, _Settings.Port)), new DesiredCapabilities(capsDef));
                }
                catch
                {
                    MessageBox.Show("Failed to connect to the server. Please check that it is running.", "Inspector Connection Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    Message = "Failed to connect to Appium";
                }

                if (_Driver == null)
                {
                    if (_Settings.UseRemoteServer)
                    {
                        Message = "Failed to connect to remote Appium server";
                    }
                    else
                    {
                        Message = "Failed to connect to Appium";
                    }

                    return;
                }
            }

            Message = "Updating";

            string pageSource = null;
            // grab the page source and parse it
            try
            {
                if (!string.IsNullOrWhiteSpace(pageSource = _Driver.PageSource))
                {
                    _CleanUpRoot();
                    var root = _ConvertToUIAutomatorNode(pageSource);
                    _RootNode = new NodeTree<UIAutomatorNodeVM>();
                    UIAutomatorNodeVM vm = new UIAutomatorNodeVM(root, vm_SelectionChanged);
                    _RootNode.Add(vm);
                    FirePropertyChanged(() => RootNode);
                }
                else
                {
                    Message = "Error getting page source";
                }
            }
            catch (Exception e)
            {
                Message = String.Format("Error getting source page: {0}", e.Message);
            }

            if (firstTime)
            {
                // added sleep timer here since the setup I had was pulling in the "previous" picture
                System.Threading.Thread.Sleep(300);
            }

            // grab the image
            Screenshot screenshot = null;
            try
            {
                screenshot = _Driver.GetScreenshot();
            }
            catch (Exception e)
            {
                Message = String.Format("Error getting screenshot: {0}", e.Message);
            }
            finally
            {
                if (screenshot != null)
                {
                    ImageByteArray = screenshot.AsByteArray;

                    if (ImageByteArray == null)
                    {
                        Message = "Error getting screenshot";
                    }
                }
                else
                {
                    Message = "Error getting screenshot";
                }
            }
            
            // Show success message if the "Updating" message was not changed
            if (Message.Equals("Updating"))
            {
                Message = string.Format("Last successfully updated on {0}", DateTime.Now);
            }
        }
        private void _RefreshItems(object state)
        {
            bool firstTime = false;
            // start the driver
            if (!_Driver.IsStarted)
            {
                firstTime = true;
                string errorMessage;
                Message = "Starting Selenium Driver";
                if (!_Driver.Start(out errorMessage))
                {
                    Console.WriteLine("error: {0}", errorMessage);
                    Message = "Error Starting Selenium Driver";
                    return;
                }
            }
            Message = "Updating";

            string pageSource = null;
            // grab the page source and parse it
            if (!string.IsNullOrWhiteSpace(pageSource = _Driver.GetPageSource()))
            {
                _CleanUpRoot();
                var root = _ConvertToUIAutomatorNode(pageSource);
                _RootNode = new ObservableCollection<UIAutomatorNodeVM>();
                UIAutomatorNodeVM vm = new UIAutomatorNodeVM(root, vm_SelectionChanged);
                _RootNode.Add(vm);
                FirePropertyChanged(() => RootNode);
            }

            if (firstTime)
            {
                // added sleep timer here since the setup I had was pulling in the "previous" picture
                System.Threading.Thread.Sleep(300);
            }

            // grab the image
            ImageByteArray = _Driver.GetScreenshot();
            Message = string.Format("Last updated on {0}", DateTime.Now);
        }