def start(**args)

      platform = args[:platform]
      file_path = args[:file_path]

      # Validate environment variable
      if ENV['platform'].nil?
        if platform.nil?
          puts 'No platform detected in environment and none passed to start...'
          exit
        end
        ENV['platform'] = platform
      end

      sleep 3

      # Appium ports
      ios_port = 4725
      android_port = 4727
      default_port = 4725

      platform = ENV['platform']

      # Platform is required
      check_platform platform

      platform = platform.downcase
      ENV['BASE_DIR'] = Dir.pwd

      # Check if multithreaded for distributing tests across devices
      threads = ENV['THREADS'].nil? ? 1 : ENV['THREADS'].to_i
      parallel = threads != 1

      if platform != 'all'
        pid = fork do
          if !parallel
            ENV['SERVER_URL'] = "http://0.0.0.0:#{default_port}/wd/hub"
            @server.start_single_appium platform, default_port
          else
            ENV['SERVER_URL'] = 'http://localhost:4444/wd/hub'
            @server.launch_hub_and_nodes platform
          end
          setup(platform, file_path, threads, parallel)
        end

        puts "PID: #{pid}"
        setup_signal_handler(pid)
        Process.waitpid(pid)
      else # Spin off 2 sub-processes, one for Android connections and another for iOS,
        # each with redefining environment variables for the server url, number of threads and platform
        ios_pid = fork do
          ENV['THREADS'] = threads.to_s
          if parallel
            ENV['SERVER_URL'] = 'http://localhost:4444/wd/hub'
            puts 'Start iOS'
            @server.launch_hub_and_nodes 'ios'
          else
            ENV['SERVER_URL'] = "http://0.0.0.0:#{ios_port}/wd/hub"
            puts 'Start iOS'
            @server.start_single_appium 'ios', ios_port
          end
          setup('ios', file_path, threads, parallel)
        end

        android_pid = fork do
          ENV['THREADS'] = threads.to_s
          if parallel
            ENV['SERVER_URL'] = 'http://localhost:4444/wd/hub'
            puts 'Start Android'
            @server.launch_hub_and_nodes 'android'
          else
            ENV['SERVER_URL'] = "http://0.0.0.0:#{android_port}/wd/hub"
            puts 'Start Android'
            @server.start_single_appium 'android', android_port
          end
          setup('android', file_path, threads, parallel)
        end

        puts "iOS PID: #{ios_pid}\nAndroid PID: #{android_pid}"
        setup_signal_handler(ios_pid, android_pid)
        [ios_pid, android_pid].each { |process_pid| Process.waitpid(process_pid) }
      end

      # Kill any existing Appium and Selenium processes
      kill_process 'appium'
      kill_process 'selenium'
    end
def start(**args)

      platform = args[:platform]
      file_path = args[:file_path]

      # Validate environment variable
      if ENV['platform'].nil?
        if platform.nil?
          puts 'No platform detected in environment and none passed to start...'
          exit
        end
        ENV['platform'] = platform
      end

      sleep 3

      # Appium ports
      ios_port = 4725
      android_port = 4727
      default_port = 4725

      platform = ENV['platform']

      # Platform is required
      check_platform platform

      platform = platform.downcase
      ENV['BASE_DIR'] = Dir.pwd

      # Check if multithreaded for distributing tests across devices
      threads = ENV['THREADS'].nil? ? 1 : ENV['THREADS'].to_i
      parallel = threads != 1

      if platform != 'all'
        pid = fork do
          if !parallel
            ENV['SERVER_URL'] = "http://0.0.0.0:#{default_port}/wd/hub"
            @server.start_single_appium platform, default_port
          else
            ENV['SERVER_URL'] = 'http://localhost:4444/wd/hub'
            @server.launch_hub_and_nodes platform
          end
          setup(platform, file_path, threads, parallel)
        end

        puts "PID: #{pid}"
        setup_signal_handler(pid)
        Process.waitpid(pid)
      else # Spin off 2 sub-processes, one for Android connections and another for iOS,
        # each with redefining environment variables for the server url, number of threads and platform
        ios_pid = fork do
          ENV['THREADS'] = threads.to_s
          if parallel
            ENV['SERVER_URL'] = 'http://localhost:4444/wd/hub'
            puts 'Start iOS'
            @server.launch_hub_and_nodes 'ios'
          else
            ENV['SERVER_URL'] = "http://0.0.0.0:#{ios_port}/wd/hub"
            puts 'Start iOS'
            @server.start_single_appium 'ios', ios_port
          end
          setup('ios', file_path, threads, parallel)
        end

        android_pid = fork do
          ENV['THREADS'] = threads.to_s
          if parallel
            ENV['SERVER_URL'] = 'http://localhost:4444/wd/hub'
            puts 'Start Android'
            @server.launch_hub_and_nodes 'android'
          else
            ENV['SERVER_URL'] = "http://0.0.0.0:#{android_port}/wd/hub"
            puts 'Start Android'
            @server.start_single_appium 'android', android_port
          end
          setup('android', file_path, threads, parallel)
        end

        puts "iOS PID: #{ios_pid}\nAndroid PID: #{android_pid}"
        setup_signal_handler(ios_pid, android_pid)
        [ios_pid, android_pid].each { |process_pid| Process.waitpid(process_pid) }
      end

      # Kill any existing Appium and Selenium processes
      kill_process 'appium'
      kill_process 'selenium'
    end
Fire necessary appium server instances and Selenium grid server if needed.