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);
            }
        }
Exemplo n.º 2
0
        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 == 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);
            }
        }