0% found this document useful (0 votes)
35 views15 pages

Appium

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views15 pages

Appium

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Appium :

https://naveenautomationlabs.com/tag/appium/

Android-IOS single script:

https://naveenautomationlabs.com/appium-tutorials/

https://naveenautomationlabs.com/12-executing-test-case-in-appium-with-pom/

interview Q:

https://www.guru99.com/appium-interview-questions.html

Appium Architecture :

http://www.automationtestinghub.com/appium-tutorial/

https://www.edureka.co/blog/appium-architecture/

http://study-automation.blogspot.com/2017/08/appium-architecture-how-appium-works.html

WDA – webdriver agent


First Script :

http://www.automationtestinghub.com/first-appium-test-script/

Appium latest version – 1.16.0 stable version

Android latest version – 10

IOS latest version – 13.5.1

MAC OS – 10.15.5

Getting App Package and App activity :

http://www.automationtestinghub.com/apppackage-and-appactivity-name/

appPackage:

In very basic terms, appPackage is the technical name of the app which is provided by its developers. It’s
actually a top level package under which all the code for the app resides.

- Connect device to system- adb devices- unlock app and open app-

Now run this command: dumpsys window windows | grep -E ‘mCurrentFocus’

 And also with APK app info

Appium complete Tutorial Android :

http://www.automationtestinghub.com/appium-tutorial/

ADB logs Android - https://www.appdome.com/no-code-mobile-integration-knowledge-base/


gathering-android-device-logs-using-adb/

1. Enable USB debugging on your device.


2. Connect the device to computer
3. Use these commands:
Get the "device id"

adb devices
example:

$ adb devices
List of devices attached
5856423841563398 device
emulator-5554 device
To specify the device when using logcat

adb -s "device id" logcat


example:

$ adb -s 5856423841563398 logcat


--------- beginning of crash
03-31 15:56:51.174 13547 13547 E AndroidRuntime: FATAL EXCEPTION: main

IOS Logs : https://www.appdome.com/no-code-mobile-integration-knowledge-base/obtaining-ios-


device-logs/

https://appiumpro.com/editions/tag/All%20Devices

 Fullreset – true means uninstall the app and reinstall it


 Noreset – true means just clear the cache
fullReset = false | noReset = true ==> no changes to app state
fullReset = false | noReset = false ==> reset current app state by clearing cache
(android)
fullReset = true | noReset = true ==> no changes to app state
fullReset = true | no Reset = false ==> uninstalls app | reinstalls app | removes app on
session end

Desiredcap: http://appium.io/docs/en/writing-running-appium/caps/
1 package tests;

3 import java.net.MalformedURLException;

4 import java.net.URL;

6 import org.openqa.selenium.remote.DesiredCapabilities;

7 import io.appium.java_client.AppiumDriver;

8 import io.appium.java_client.MobileElement;

9 import io.appium.java_client.android.AndroidDriver;

10

11 public class AppiumTest {


public static void main(String[] args) {
12

13
//Set the Desired Capabilities
14
DesiredCapabilities caps = new DesiredCapabilities();
15
caps.setCapability("deviceName", "My Phone");
16
caps.setCapability("udid", "ENUL6303030010"); //Give Device ID of your mobile
17
phone
18
caps.setCapability("platformName", "Android");
19
caps.setCapability("platformVersion", "6.0");
20
caps.setCapability("appPackage", "com.android.vending");
21
caps.setCapability("appActivity",
22
"com.google.android.finsky.activities.MainActivity");
23
caps.setCapability("noReset", "true");
24

25
//Instantiate Appium Driver
26
try {
27
AppiumDriver<MobileElement> driver = new
28
AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
29

30
} catch (MalformedURLException e) {
31
System.out.println(e.getMessage());
32
}
33
}
34

IOS Internal logic in running scripts :

https://appiumpro.com/editions/41-how-to-test-real-ios-devices-with-appium-part-2
import io.appium.java_client.ios.IOSDriver;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Edition041_iOS_Real_Device {


private IOSDriver driver;

@Before
public void setUp() throws MalformedURLException {
DesiredCapabilities capabilities = new
DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "12.0.1");
capabilities.setCapability("deviceName", "iPhone 8");
capabilities.setCapability("udid", "auto");
capabilities.setCapability("bundleId", "<your bundle
id>");
capabilities.setCapability("xcodeOrgId", "<your org
id>");
capabilities.setCapability("xcodeSigningId", "iPhone
Developer");
capabilities.setCapability("updatedWDABundleId", "<bundle
id in scope of provisioning profile>");

driver = new IOSDriver<>(new


URL("http://localhost:4723/wd/hub"), capabilities);
}

@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}

@Test
public void testFindingAnElement() {
driver.findElementByAccessibilityId("Login Screen");
}
}
Identifying elements in Automatoriviewer: http://www.automationtestinghub.com/uiautomatorviewer-
inspect-elements/

1 //Find element using ID property

2 driver.findElement(By.id("com.android.vending:id/search_box_idle_text")).click();

3 //OR

4 driver.findElementById("com.android.vending:id/search_box_idle_text").click();

6 //Find element using Accessibility ID

7 driver.findElementByAccessibilityId("Search").click();

//Find element using XPath - By searching for content-desc


1
driver.findElement(By.xpath("//android.widget.ImageView[@content-desc = 'Search']")).click();
2
//OR
3
driver.findElement(By.xpath("//*[@content-desc = 'Search']")).click();
4

//Find element using ClassName property

3 List<MobileElement> elements = driver.findElements(By.className("android.widget.ImageView"));

4 for(MobileElement element : elements) {

5 if(element.getAttribute("contentDescription").equals("Search")) {

6 element.click();

7 break;

8 }
}

Find element using XPath - by searching for text


1
driver.findElement(By.xpath("//android.widget.EditText[@text = 'Search Google
2
Play']")).sendKeys("Google");
3
OR
4
driver.findElement(By.xpath("//*[@text = 'Search Google Play']")).sendKeys("Google");

Swipe :

public static void swipe(int fromX,int fromY,int toX,int


toY) {

TouchAction action = new TouchAction(driver);


action.press(PointOption.point(fromX,fromY))
.waitAction(new
WaitOptions().withDuration(Duration.ofMillis(3000)))
//you can change wait durations as per your requirement
.moveTo(PointOption.point(toX, toY))
.release()
.perform();
}

https://mahantesh-hadimani.blogspot.com/2019/08/how-to-swipe-up-or-scroll-up-in-appium.html

https://www.nextgenerationautomation.com/post/appium-interview-questions-answers

IOS Logs :

Using Xcode
1. First, install Xcode on your mac machine. Next, launch Xcode.
2. Connect your iOS device to the Mac through USB.
3. Launch Xcode. Go to Windows > Devices and Simulators.
4. Reproduce the problem you encountered.
5. Choose your device from the devices section on the left side of
the screen.
6. Click on the up-triangle on the bottom of the screen to view
device logs.
7. Click on the down arrow on the bottom right of the screen to
save the device logs as a file.
8. Select View Device Logs button under the Device
Information section on the right-hand panel to view crash logs.
9. Under Process column on the left, identify and select your app
and click on Crash Log to see the contents.
10. Right click the corresponding app entry on the Process
Column and click on Export Log to save the crash log.

To find out the bundle id of any existing app:

1. Connect your iOS device to your Mac with iOS wire


2. Open the Console app on Mac
3. Select your device under the Devices heading (top left)
4. Enter the name of your app in the search bar
5. Now launch the app in your iOS device
6. You will see the first log in the console like SpringBoard Bootstrapping
com.xxxx.xxx.kids with intent foreground-interactive
To get the UDID of a device
instruments -s devices

https://www.browserstack.com/guide/appium-ios-tutorial

https://techapple.net/2015/01/4-waysmethods-install-ipa-file-app-iphone-ipad-ipod-online-
offline-methods/

Xcode - latest stable release is version 13.1,


IOS – 15 is the latest version
Appium – 1.22 is the latest version

The latest version of Android OS is 11

Touch actions :

https://appium.io/docs/en/writing-running-appium/touch-actions/

Multi touch :

action0 = TouchAction().tap(el)

action1 = TouchAction().tap(el)
MultiAction().add(action0).add(action1).perform()

TouchAction().press(el0).moveTo(el1).release()

Scrolling
To allow access to this special feature, we override
the execute or executeScript methods in the driver, and prefix the command
with mobile:. See examples below:
To scroll, pass direction in which you intend to scroll as parameter.

avascriptExecutor js = (JavascriptExecutor) driver; HashMap<String, String>


scrollObject = new HashMap<String, String>(); scrollObject.put("direction",
"down"); js.executeScript("mobile: scroll", scrollObject);

Sample to scroll using direction and element.


JavascriptExecutor js = (JavascriptExecutor) driver; HashMap<String, String>
scrollObject = new HashMap<String, String>(); scrollObject.put("direction",
"down"); scrollObject.put("element", ((RemoteWebElement) element).getId());
js.executeScript("mobile: scroll", scrollObject);

Swiping
This is an XCUITest driver specific method that is similar to scrolling (for
reference, see https://developer.apple.com/reference/xctest/xcuielement).
This method has the same API as Scrolling, just replace "mobile: scroll" with
"mobile: swipe"

@FindBy is used to find and initiate WebElements, and to define MobileElements


we need to use annotations like @iOSXCUITFindBy and @AndroidFindBy

@iOSXCUITFindBy is used to declare variable as “IOSElement” which is for


Apple idevices

@AndroidFindBy is used to declare variable as “AndroidElement” which is for


Android based devices
List of locators available for @iOSXCUITFindBy

List of locators available for @AndroidFindBy

Sample code snippet below on @FindBy

@AndroidFindBy(id = "username")
AndroidElement loc_Username_android;

@iOSXCUITFindBy(accessibility = "username")
AndroidElement loc_Username_ios;

public class OS {
public static AppiumDriver<?> driver;

@BeforeMethod
public static void startTest(String platform) {
if(platform.equalsIgnoreCase("Android")) {
driver = startAppium_Android();
} else {
driver = startAppium_IOS();
}
}

/*
* This method is used for initiate the AppiumDriver with caps and connection
protocol
*/
public static AndroidDriver<?> startAppium_Android() {
// Initializing the Appium driver
try {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME,
"Android");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "5642c6b9");
cap.setCapability("appActivity",
"com.invoiceapp.InvoiceLaunchScreenAct");
cap.setCapability("appPackage", "com.invoiceapp");
cap.setCapability("autoLaunch", false);
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT,
500);
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME,
AutomationName.ANDROID_UIAUTOMATOR2);
// driver is declared as an AndroidDriver, which supports AndroidElements
driver = new AndroidDriver<AndroidElement>(new
URL("http://127.0.0.1:4723/wd/hub"), cap);
} catch (Exception e) {
e.printStackTrace();
}
//Returning the instance of the driver to the parent method
return (AndroidDriver<?>) driver;
}

/*
* This method is used for initiate the IOSDriver with caps and connection
protocol
*/
public static IOSDriver<?> startAppium_IOS() {
// Initializing the Appium driver
try {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION,
"12.0.1");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 8");
cap.setCapability(MobileCapabilityType.UDID,
"3jtn3j4n3ijn3ji4nrj34inrj34nr34nrk");
cap.setCapability("bundleId", "com.invoiceapp");
cap.setCapability("autoLaunch", false);
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME,
AutomationName.IOS_XCUI_TEST);
// driver is declared as an IOSDriver, which supports IOSElements
driver = new IOSDriver<IOSElement>(new
URL("http://127.0.0.1:4723/wd/hub"), cap);
} catch (Exception e) {
e.printStackTrace();
}
//Returning the instance of the driver to the parent method
return (IOSDriver<?>) driver;
}
}
Question: Write a method to wait for specific element to vanish on
mobile app before certain interval?
Answer:
public boolean
waitForElementToVanish(AppiumDr
iver driver, MobileElement
element, int maxWait) {
try {
int i = 0;
while(element.isDisplayed() &&
i<3) {
Thread.sleep(maxWait);
i++;
}
return !element.isDisplayed();
}
catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}

Question: Write a method to perform single tap using appium?


Answer:

public static void


performSingleTap(AppiumDriver<MobileElem
ent> driver, MobileElement element){
try {
TouchActions action = new
TouchActions(driver);
action.singleTap(element);
action.perform();
}
catch (Exception e) {
Log.fail("Unable to perform
Single Tap Operation" + e.getMessage(),
driver);
}
}
Question: Write a method to perform double tap using appium?
Answer:
public static void
performDoubleTap(AppiumDriver<MobileElem
ent> driver, MobileElement element){
try {
TouchActions action = new
TouchActions(driver);
action.doubleTap(element);
action.perform();
}
catch (Exception e) {
Log.fail("Unable to perform
Double Tap Operation" + e.getMessage(),
driver);
}
}

Question: Write a method to perform touch sequence from one


element to other element?
Answer:

public static void


performTouchSequence(AppiumDriver<MobileEle
ment> driver, MobileElement
element1,MobileElement element2){
try {
TouchAction action = new
TouchAction(driver);
action.press(element1);
action.moveTo(element2);
action.release();
action.perform();
}
catch (Exception e) {
Log.fail("Unable to perform
Touch Sequence Operation" +
e.getMessage());
}
}
Question: Write a method to perform multi touch sequence
containing different elements?
Answer:

public static void


performMultiTouch(AppiumDriver<MobileElem
ent> driver, MobileElement
element1,MobileElement
element2,MobileElement
element3,MobileElement element4){
try {
TouchAction actionOne = new
TouchAction(driver);
actionOne.press(element1);
actionOne.moveTo(element2);
actionOne.release();

TouchAction actionTwo = new


TouchAction(driver);
actionTwo.press(element3);
actionTwo.moveTo(element4);
actionTwo.release();

MultiTouchAction action = new


MultiTouchAction(driver);
action.add(actionOne);
action.add(actionTwo);
action.perform();
}
catch (Exception e) {
Log.fail("Unable to perform
Multi Touch Operation" + e.getMessage(),
driver);
}
}

You might also like