Search code examples
javaclientserverappiumselenium-grid

How to run appium script in remote machine?


I have 2 desktop machines in which Appium script is saved in machine 1 and Appium got installed in machine 2 and android devices also connected in machine 2. now i want to take the script from the machine 1 and execute it in the devices connected in the machine 2. how to achieve this. please suggest me. The below code i am able to run same machine.i want to run on android device which is connected in another machine.Both machine ip segment is same segment.

package com.appiumproj.test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;

public class Appium {

AppiumDriver driver;

@BeforeClass
public void setUp() throws MalformedURLException{

     //Set up desired capabilities and pass the Android app-activity and app-package to Appium

   DesiredCapabilities capabilities = new DesiredCapabilities();
   capabilities.setCapability(MobileCapabilityType.VERSION, "5.0.2");
   capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
   capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"ZX1D62FPVQ");
   capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculatord");
   capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator"); 
   driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void testCal(){

    
   WebElement two=driver.findElement(By.name("2"));
    two.click();
    WebElement plus=driver.findElement(By.name("+"));
    plus.click();
    WebElement four=driver.findElement(By.name("4"));
    four.click();
    WebElement equalTo=driver.findElement(By.name("="));
    equalTo.click();
   
    WebElement results=driver.findElement(By.className("android.widget.EditText"));
   
    assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";

	System.out.println("Inside Test Function");
terClass
public void teardown(){
   
    
    driver.closeApp();
}
}
package com.appiumproj.test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;

public class Appium {

AppiumDriver driver;

@BeforeClass
public void setUp() throws MalformedURLException{

     //Set up desired capabilities and pass the Android app-activity and app-package to Appium

   DesiredCapabilities capabilities = new DesiredCapabilities();
   capabilities.setCapability(MobileCapabilityType.VERSION, "5.0.2");
   capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
   capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"ZX1D62FPVQ");
   capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculatord"); // This is package name of your app (you can get it from apk info app
   capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator"); // This is Launcher activity of your app (you can get it from apk info app)
   //Create AndroidDriver instance and connect to the Appium server.
   //It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities

   driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void testCal(){

    //locate the Text on the calculator by using By.name()
   WebElement two=driver.findElement(By.name("2"));
    two.click();
    WebElement plus=driver.findElement(By.name("+"));
    plus.click();
    WebElement four=driver.findElement(By.name("4"));
    four.click();
    WebElement equalTo=driver.findElement(By.name("="));
    equalTo.click();
    //locate the edit box of the calculator by using By.className()
    WebElement results=driver.findElement(By.className("android.widget.EditText"));
    //Check the calculated value on the edit box
    assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
/*
	System.out.println("Inside Test Function");
	driver.findElement(By.partialLinkText("More")).click();
	driver.findElement(By.xpath("//EditText[@text='Email Address']")).sendKeys("[email protected]");
	driver.findElement(By.xpath("//LinearLayout/EditText[2]")).sendKeys("Testerpwd");
	driver.findElement(By.xpath("//CheckBox")).click();
	driver.findElement(By.xpath("//Button[@text='Login']")).click();

	WebDriverWait wait = new WebDriverWait(driver,80);
	wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//Button[@text='Logout']"))); 
	driver.findElement(By.xpath("//Button[@text='Logout']")).click(); 
*/
}
@AfterClass
public void teardown(){
   
    //close the app
    driver.closeApp();
}
}


Solution

  • Give the exact Ip address on the Appium Instance running on Machine 2. By default it is 127.0.0.1, remove this and give its own IP address explicitly in Appium settings. This will work. Let me know if not.