How to make reusable method of my DesiredCapabilities

Hi There,

I want to make reusable method of my " DesiredCapabilities" for use in my each test case so how can i make it as in POM formate to call in my all test case instead of hard coded again and again and my DesiredCapabilities like

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(“deviceName”, “3KA001443”);
cap.setCapability(“platformVersion”, “4.4”);
cap.setCapability(“platformName”, “Android”);
cap.setCapability(“app”, “E:\NetGear\insight_v3.0.19.2.apk”);
cap.setCapability(“fullReset”, false);
cap.setCapability(“noReset”, true);
// cap.setCapability(“apppackage”, “com.netgear.insight”);
//cap.setCapability(“appActivity”, “com.android.netgeargenie.view.MainActivity”);

URL url = new URL("http://127.0.0.1:4723/wd/hub");

driver = new AndroidDriver(url,cap);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
 System.out.println(" launching app"); 

please help me
because i created it in simple method but when i call it then i am getting error “Original error: Requested a new session but one was in progress”

so how can i resolved this plz guide me

A POM file is part of Maven. Maven is a build tool. It helps automate steps you can normally do manually. I believe doing what you want isn’t really a feature of Maven.

Maven will include dependencies into a project. So if you put your DesiredCapabilities into its own class, you can compile it into a jar file and use it in your project. Just like I can include the Java libraries and this lets me do things like System.out.println(""); I can include my own libraries and use them.

This might be a little overkill however. I usually just create a class in the project called TestBase. The TestBase have all the code for setting my DesiredCapabilities. Then for every test class I create I can extend TestBase. For example,

class MyTestSuite extends TestBase {
@Test
public void testNumberOne() {
// test goes here
}
}

The TestBase class would have something like:

class TestBase {
protected AppiumDriver driver;

@Before
public void setUp() {
// your DesiredCapabilities code and initializing the **driver** variable
}
}

This is less to do with Appium or even Maven and more about good Java programming practices.

Darrell

1 Like

If you are using TestNG, put the DesiredCapabilities code above in BeforeMethod or BeforeSuite based on your need.

Hi SireeshaSimakurthy,
Thanks for replying me.

I am using TestNG and also put DesiredCapabilities in before method but my concern is when ever my test case will fail in that case the current session has to kill and for next test case it has to again launch the application . means it has to start again when my test case has fail. for that i want to make reusable method

I did the following in my test suite like below and the functions I have used. Hope this helps

@BeforeTest
Start Appium

@BeforeMethod
setIOSDriver (This will set capabilities)

@AfterTest
Stop Appium

public static void startAppium() {
Boolean blnServiceStatus;

	System.out.println("Starting Appium");
	service = new AppiumServiceBuilder().usingDriverExecutable(new File("/usr/local/bin/node"))
			.withAppiumJS(new File("/usr/local/lib/node_modules/appium/build/lib/main.js"));

	// .usingDriverExecutable(new File(System.getProperty("user.home") +
	// "/local/bin/node"))
	// .withIPAddress("127.0.0.1").usingPort(4723));
	blnServiceStatus = service.build().isRunning();

	if (blnServiceStatus.equals(false)) {
		service.build().start();
		System.out.println("Appium server Started");
	}

	service_url = service.build().getUrl().toString();
	System.out.println(service_url);
}

public static void stopAppium() {

	System.out.println("Stopping Appium");
	try {
		service.build().stop();
	} catch (Exception e) {
		System.out.println("Appium server is already stopped");
	}
	System.out.println("Stopped Appium Server");

}

public void setIOSDriver(String ExecutionName, String rotatable, String bundleId, String app, String platformName,
		String platformVersion, String deviceName, String deviceID, String appiumUrl) throws MalformedURLException {

	System.out.println("In IOS Driver  ");
	// Set capabilities
	capabilitiesForAppium.setCapability(MobileCapabilityType.AUTOMATION_NAME, ExecutionName);
	capabilitiesForAppium.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "300000");
	capabilitiesForAppium.setCapability(MobileCapabilityType.ROTATABLE,
			rotatable.equalsIgnoreCase("true") == true ? true : false);
	capabilitiesForAppium.setCapability("rotatable", true);
	// capabilitiesForAppium.setCapability(MobileCapabilityType.BROWSER_NAME,"safari");
	capabilitiesForAppium.setCapability(MobileCapabilityType.APP,
			System.getProperty("user.dir") + "/Test.ipa");
	capabilitiesForAppium.setCapability("bundleId", bundleId);
	// capabilitiesForAppium.setCapability("autoAcceptAlerts", true);
	capabilitiesForAppium.setCapability(MobileCapabilityType.FULL_RESET, true);
	capabilitiesForAppium.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
	capabilitiesForAppium.setCapability(MobileCapabilityType.PLATFORM_VERSION, platformVersion);
	capabilitiesForAppium.setCapability(MobileCapabilityType.DEVICE_NAME, deviceName);
	capabilitiesForAppium.setCapability("maxTypingFrequency",3000);
	capabilitiesForAppium.setCapability("xcodeConfigFile",
			System.getProperty("user.dir") + "/resources/XCode_Config.xcconfig");

	capabilitiesForAppium.setCapability("udid", deviceID);

	if (!service_url.equals("Blank")) {
		appiumDriver = new IOSDriver(new URL(service_url), capabilitiesForAppium);
	} else {
		appiumDriver = new IOSDriver(new URL(appiumUrl), capabilitiesForAppium);
	}

}

For each @Test annotation, @BeforeMethod and @AfterMethod will be initiated. So irrespective of the status of the test case, Appium will start