Yuval Levy
Yuval Levy

Reputation: 2516

C# Starting Appium programmatically - target machine actively refused it

I want to start Appium server programmatically using C#. when I use Appium window to start Appium manually, It Starts successfully: Appium window

But when I run it automatically often I get an exception:

"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"

This is the c# code for starting Appium Server:

    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));

I read those questions but it didn't helped me:

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

Why when I start is manually Appium start successfully, but when I Start it the same programmatically I get refused?

Upvotes: 4

Views: 6119

Answers (4)

Meer
Meer

Reputation: 798

With the latest Appium server, it's possible to start driver instance programatically when client driver is initialised with capabilities/driver options passed.

I have Appium server 1.17.1 in my Mac Catalina, using Appium.WebDriver 4.1.1 client library for .NET (C#).

//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);

No need to start appium server from command line.

Upvotes: 0

Yogee
Yogee

Reputation: 1442

'target machine actively refused it' exception (of TCP/IP) means, IP address can be reached but no connection could be made to server on the specified port.

How to Debug

Step 1: Check if your server is listening to the port you are trying to connect:

On server machine, use this command (you need administrator privileges):

netstat -ab > D:\portscan.txt

It will take a while (sometime a minute or so) but once the command execution is done, it will create a portscan.txt file in your D: drive. Search your port in that file and check if it's open.

Step 2: If port is open but still you cannot connect. It means firewall has blocked it. Add exception in firewall(on server side) and you should be able to connect to the port in most cases.

Upvotes: 0

juhlila
juhlila

Reputation: 1122

Here is what I did to start and kill appium progamatically, in ruby.

I created a config file that starts appium and checks when its up

  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

The method above only starts appium server, not the driver. To start the driver I created the two methods listed: I call it on my RSpec hooks, on before suite.

  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

Here is my spec config file, that makes everything works togheter

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

Upvotes: 0

anuja jain
anuja jain

Reputation: 1387

This might be because server instance is already running at the port 4723 try changing port number to something like 5555 or any random 4 digit number.

Upvotes: 3

Related Questions