【问题标题】:C# Starting Appium programmatically - target machine actively refused itC# 以编程方式启动 Appium - 目标机器主动拒绝它
【发布时间】:2016-09-12 10:07:53
【问题描述】:

我想使用 C# 以编程方式启动 Appium 服务器。 当我使用 Appium 窗口手动启动 Appium 时,它启动成功:

但是当我自动运行它时,我经常会遇到异常:

"An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll

Additional information: Unexpected error. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:4723"

这是启动Appium Server的c#代码:

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "C:/Program Files (x86)/Appium/node.exe";
    startInfo.Arguments = @"""C:/Program Files (x86)/Appium/node.exe lib/server/main.js"" --address 127.0.0.1 --port 4723 --session-override --platform-name Android --platform-version 23 --automation-name Appium --log-no-color";
    process.StartInfo = startInfo;
    process.Start();


    capabilities = new DesiredCapabilities();
    capabilities.SetCapability("deviceName", "Samsung S6");
    capabilities.SetCapability("platformName", "Android");

    capabilities.SetCapability("platformVersion", "5.0.2");
    capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");

    driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities, TimeSpan.FromSeconds(30));

我阅读了这些问题,但对我没有帮助:

Appium iOS automation using C#/Visual Studio No connection could be made because the target machine actively refused it 127.0.0.1:3446

为什么我启动的时候是手动 Appium 启动成功,但是当我以同样的方式启动时却被拒绝了?

【问题讨论】:

  • 我在 ruby​​ 中做到了这一点,现在当我开始我的 rspec 测试时,它会启动 appium,并在测试运行后将其杀死。不确定它是否有帮助,但如果你愿意,我可以用我的代码发布答案。这个错误是因为appium已经运行了,你可以调用killall node杀死它,如果它是一台mac机器,你必须在测试后输入代码来杀死appium,这样你就不会再遇到这个问题了
  • 是的,如果你能发布你的代码,谢谢

标签: c# selenium appium


【解决方案1】:

这可能是因为服务器实例已经在端口 4723 上运行,尝试将端口号更改为 5555 或任何随机的 4 位数字。

【讨论】:

  • 在尝试启动 Appium Server 之前如何检查它当前是否正在运行?
  • 目前我不知道如何检查,但我会研究并与您分享。更改端口对您有用吗?
【解决方案2】:

这是我在 ruby​​ 中以编程方式启动和杀死 appium 的操作。

我创建了一个启动 appium 并检查它何时启动的配置文件

  def run_appium_service
#this command kill appium if for some reason it wasnt killed after a
#previus execution, so i dont get the "is the server already running?"
#error when try to start, another way could be check if its up and start
#only if its not
    Process.fork { system 'killall node > /dev/null' }
    system '> tmp/appium.log'
    Process.fork { system 'appium --log-level debug > tmp/appium.log' }
    #wait appium load
    t = 10
    until !(File.readlines(project_home + "/tmp/appium.log").grep(/interface listener started/).empty?) or t == 0 do
      sleep(0.5)
      t -= 1
    end
  end

上面的方法只启动appium server,不启动驱动。 要启动驱动程序,我创建了列出的两种方法: 我在我的 RSpec 钩子上调用它,在套件之前。

  def capabilities(options = {})
    caps = Hash.new
    caps.default = {
        platformName: 'iOS',
        deviceName: get_device_name,
        app: app_ipa,
        launchTimeout: 100000,
        autoAcceptAlerts: false,
        newCommandTimeout: 12000,
        udid: get_udid,
        fullReset: false
    }
    caps.default.merge(options)
  end

  def start_appium_driver
    caps = capabilities
    $appium = Appium::Driver.new(caps: caps)
    $browser = $appium.start_driver
    Appium.promote_appium_methods RSpec::Core::ExampleGroup
  end

这是我的规范配置文件,它可以让一切工作在一起

module SpecHelper
  run_appium_service
  RSpec.configure do |config|
    config.before :suite do
      start_appium_driver
    end
    config.after :suite do
      if defined? driver
        driver_quit
      end
    end
  end
end

【讨论】:

    【解决方案3】:

    '目标机器主动拒绝'异常(TCP/IP)意味着可以访问IP地址但无法连接到指定端口上的服务器。

    如何调试

    第 1 步:检查您的服务器是否正在侦听您尝试连接的端口:

    在服务器机器上,使用这个命令(你需要管理员权限):

    netstat -ab > D:\portscan.txt
    

    这将需要一段时间(有时一分钟左右),但一旦命令执行完成,它将在您的 D: 驱动器中创建一个 portscan.txt 文件。在该文件中搜索您的端口并检查它是否已打开。

    第 2 步: 如果端口已打开,但仍无法连接。这意味着防火墙已阻止它。在防火墙中添加例外(在服务器端),您应该能够在大多数情况下连接到端口。

    【讨论】:

      【解决方案4】:

      使用最新的 Appium 服务器,可以在客户端驱动程序初始化并传递功能/驱动程序选项时以编程方式启动驱动程序实例。

      我的 Mac Catalina 中有 Appium 服务器 1.17.1,使用适用于 .NET (C#) 的 Appium.WebDriver 4.1.1 客户端库。

      //Initialise capabilities
      AppiumOptions appiumOptions = new AppiumOptions();
      
      //Declare capabilities
      ...
      
      //Initialise the iOS driver
      var iosDriver = new IOSDriver<IMobileElement<AppiumWebElement>>(appiumOptions);
      
      //Initialise the android driver
      var androidDriver = new AndroidDriver<IMobileElement<AppiumWebElement>>(appiumOptions);
      

      无需从命令行启动appium server。

      【讨论】:

        猜你喜欢
        • 2018-06-06
        • 1970-01-01
        • 2011-04-04
        • 2018-10-07
        • 2021-04-13
        • 2017-10-22
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多