Now let’s look on how to start Appium Server Programmatically.
So here we use two Appium classes
1.AppiumDriverLocalService
2.AppiumServiceBuilder
Exceptions can be :
1.AppiumServerHasNotBeenStartedLocallyException
2.InvalidNodeJSInstance
3.InvalidServerInstanceException
1.AppiumDriverLocalService: Here I am going to list only few simple methods which can be used with this class.
Modifier and Type
|
Method and description
|
static AppiumDriverLocalService
|
buildDefaultService()
|
static AppiumDriverLocalService
| |
boolean
|
isRunning()
|
void
|
start() Starts the defined appium server
|
void
|
stop() Stops this service is it is currently running.
|
URL
|
getUrl()
|
2.AppiumServiceBuilder: Similarly here some of the methods that can be used .
Modifier and Type
|
Method and description
|
AppiumServiceBuilder
|
usingAnyFreePort()Configures the appium server to start on any available port.
|
AppiumServiceBuilder
|
usingDriverExecutable(File nodeJSExecutable)
Sets which Node.js the builder will use.
|
AppiumServiceBuilder
|
usingPort(int port)
Sets which port the appium server should be started on.A value of 0 indicates that any free port may be used.
|
AppiumServiceBuilder
|
withAppiumJS(File appiumJS)
|
AppiumServiceBuilder
|
withLogFile(File logFile)
Configures the appium server to write log to the given file.
|
Since we need to start Appium server before testcase run lets use @BeforeClass annotation.
Try this::
@BeforeClass
AppiumDriverLocalService service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().usingAnyFreePort().usingDriverExecutable(new File("path to node")).withAppiumJS(new File("path to appium.js")));
To start the server:
service.start()
To stop the server which can be called after all execution of testcases:
service.stop()
For Ubuntu Users::
How to find path to node?
Open Terminal and type which node .If node.js is installed it will show output similar
/home/deepa/.linuxbrew/bin/node
Note :@BeforeClass and @AfterClass should be defined as static.
AppiumDriverLocalService should be static.
stop() and start() should be defined as void.
To start the server programmatically you have to remove this line in your previous program
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
and replace with
driver = new AndroidDriver(new URL(service_url),cap);
Where service_url =service.getUrl().toString();
For test with below code use this apk:APK to be downloaded for this example
Complete code:(Changed code marked in blue w.r.t First Test Case in my blog:Your First TestCase)
package com.test.helloworld; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.remote.MobileCapabilityType; import io.appium.java_client.remote.MobilePlatform; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; public class AppiumTest { AndroidDriver driver; static AppiumDriverLocalService service; static String service_url; @BeforeClass public static void appiumServer() throws Exception { service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().usingPort(0).usingDriverExecutable(new File("path to node")).withAppiumJS(new File("path to appium.js"))); service_url = service.getUrl().toString(); service.start(); } @Before public void testCaseSetUp() throws MalformedURLException { File appDir = new File("src"); File app = new File(appDir, "app-debug.apk"); DesiredCapabilities cap = new DesiredCapabilities(); cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID); cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device"); cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100"); cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); driver = new AndroidDriver(new URL(service_url),cap); //fresh installs the app everytime if fullReset to True cap.setCapability("fullReset", true); } @Test public void testHelloWorld() throws Exception { //find text HelloWorld and assert WebElement text1 = driver.findElement(By.xpath("//android.widget.TextView[@index='0']")); Assert.assertEquals("HelloWorld", text1.getText()); //Click Menu and asset settings driver.findElementByName("More options").click(); Assert.assertEquals("Settings", driver.findElementById("title").getText()); } @After public void testCaseTearDown() { if (driver != null) driver.quit(); } @AfterClass public static void stopServer(){ service.stop(); } }
Now run the test (since I have used Android Studio here is the screenshot of the same)

No comments:
Post a Comment