Appium Notes
Appium is an open sourced mobile automation tool for testing navitive,hybrid,mobile app and browsers..
Appium internally uses WebDriver json wire protocol(which selenium does) to test the apps.
Actually appium server acts as mediator between mobile application and Appium client libraries.
Appium Client Libraries code is wrapping as json and sending to the appium server through json wire protocol.--->then appium server sending request to the mobile through UI Automator(kind of engine)--->automator response received by appium server and server sending response to the client libraries
Appium Architecture
Appium is an open-source automation tool primarily designed for automating native, hybrid, and mobile web applications on iOS and Android platforms. Here's an overview of its architecture:
Appium Server:
Appium uses a client-server architecture. It listens for requests from clients (like Selenium WebDriver) and executes the commands on mobile devices.
It is built on top of the Node.js platform and acts as a middleman between the client libraries (WebDriver) and the mobile platform (iOS/Android).
Appium Client Libraries:
Appium provides client libraries in different languages (Java, Ruby, Python, C#, etc.) that allow you to write test scripts. These client libraries use the JSON Wire Protocol or W3C WebDriver Protocol to communicate with the Appium server.
Mobile JSON Wire Protocol:
Appium's client-server communication is based on the JSON Wire Protocol, which is also used by Selenium WebDriver for browser automation.
Automation Engines:
iOS: Appium uses Apple’s XCUITest to interact with iOS applications. It runs test cases on devices/emulators.
Android: For Android, Appium uses UIAutomator2 (or Espresso) as the backend to interact with the applications. These engines interpret Appium commands and perform actions on the mobile device.
Appium Session:
Every interaction with the Appium server happens in the form of a session. When a client connects to the server, it starts a session and sends automation commands to the server to execute.
Appium Inspector:
Appium provides a graphical user interface (GUI) tool called Appium Inspector to inspect elements of a mobile app. It allows you to find UI elements in your app and determine their properties for test automation.
1. What is Appium?
Answer: Appium is an open-source tool used for automating mobile apps (native, hybrid, and mobile web) on both iOS and Android platforms. It provides cross-platform test automation by using the WebDriver protocol.
2. What are the advantages of using Appium?
Answer:
It supports multiple platforms (iOS, Android).
It supports multiple languages like Java, Python, C#, etc.
No need to modify or recompile the app under test.
It integrates with many CI/CD tools.
Appium allows reusability of code across platforms (iOS/Android).
3. Which mobile OS platforms does Appium support?
Answer: Appium supports iOS (11 and above) and Android (4.2 and above).
4. What are the limitations of Appium?
Answer:
Limited support for gestures and interactions in older Android versions.
iOS testing requires a macOS machine.
Limited performance testing support.
Complex setups in some environments.
5. What is the difference between Native App, Hybrid App, and Mobile Web App?
Answer:
Native App: Built specifically for one platform (iOS or Android).
Hybrid App: A mix of web and native elements.
Mobile Web App: Web applications accessed via mobile browsers.
6. How does Appium handle native and hybrid apps?
Answer: Appium can switch between web and native contexts in hybrid apps. In a hybrid app, Appium automates the web content via the WebDriver protocol and the native elements using UIAutomator2 (Android) or XCUITest (iOS).
7. What is DesiredCapabilities in Appium?
Answer: DesiredCapabilities is a class in Appium that helps configure and pass necessary parameters like device name, platform version, app package, etc., to the Appium server before starting the test.
8.How can you inspect elements in Appium?
Answer:
For Android, use UIAutomatorViewer or Appium Inspector.
For iOS, use Appium Inspector or Xcode’s Accessibility Inspector
9. How do you handle multiple sessions in Appium?
Answer: Appium can handle multiple sessions by specifying different device ports for each session. The udid of each device should be specified in DesiredCapabilities.
10. What is the Appium Desired Capability for launching an app on an Android device?
Answer:
javaCopy code
DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("deviceName", "emulator-5554"); caps.setCapability("platformName", "Android"); caps.setCapability("appPackage", "com.example.android"); caps.setCapability("appActivity", ".MainActivity");
11. How can you run Appium tests in parallel?
Answer: Appium supports parallel execution using TestNG or JUnit and specifying different devices using udid in the DesiredCapabilities.
12. What is the role of the Appium server?
Answer: The Appium server acts as a middleman between the client and the mobile devices. It receives automation commands from the client via JSON Wire Protocol or W3C WebDriver protocol, interprets them, and sends them to the respective automation engines like UIAutomator2 for Android or XCUITest for iOS.
13. What are common exceptions you face in Appium, and how do you handle them?
Answer:
NoSuchElementException: Use better locator strategies or add waits.
ElementNotVisibleException: Ensure the element is in view before interacting.
StaleElementReferenceException: Re-locate the element before interacting.
14. How do you perform a scroll operation in Appium?
Answer: On Android:
bashCopy code
((AndroidDriver) driver).findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"TextToFind\"));");
15. What is the difference between Appium and Selenium?
Answer:
Appium is used for mobile app testing (iOS/Android), while Selenium is used for web browser automation.
Appium works with mobile platforms and native apps; Selenium only works with web applications.
appium server Run:
npm install -g appium
appium serverAppium driver list:
appium driver list
appium driver install UIAutomator2
appium driver install XCUITest@4.4.2UIAutomator2:
UIAutomator is Ui testing framework designed by google to faciliate automation in emulator or android device.Appium leverage its uiautomator with its own wrapper and came up with UIAutomator 2
UiAutomator2Options options=new UIAutomator2Options();
options.setApp("asjdnjasd.apk");
options.setDeviceName("SAngar");
AndroidDriver driver=new AndroidDriver(new URI("127.0.0.1:4723").toURL(),options);
driver.quit();Start and stop the appium server automatically:
AppiumDriverLocalService service=new AppiumServiceBuilder().withAppiumJS("main.js").withIpAddress("127.0.0.1").usingPort(4723).build();
service.start();
UIAutomator2Options options=new UIAutomator2Options();
options.setDeviceName("Sangar");
options.setApp("asdjn.apk");
AndroidDriver driver=new AndroidDriver(service,options);
driver.quit();
service.stop();Appium Inspector Configuration:
Capacity builder:
appium:app--->location of apk
appium:deviceName--->Sangar
platformName--->Android
appium:automationName--->UIAutomator2
driver.manage().timeouts().implicitlyWait(Duration.ofseconds(30));Locators:
By.id,By.className,By.partialLinkText,By.linkText,By.name,By.tagName
AppiumBy.accessibilityId(),AppiumBy.className()
AppiumBy.androidUiAutomator("new UIScrollable()")
SendValues:
driver.findElement(AppiumBy.accessibilityId("sangar")).sendKeys("sangar");
.click()
.getText();
Appium Gestures:
LongClick Gesture:
JavaScriptExecutor js=(JavaScriptExecutor)dirver;
js.executeScript("mobile:longClickGesture",
ImmutableMap.of("elementId",
((RemoteWebElement)driver.findelement(By.id("sanga")).getId(),"duration","2000")))Click/Tap Gesture:
js.executeScript("mobile:clickGesture",
ImmutableMap.of("elementId",
((RemoteWebElement)driver.findelement(By.id("sangar"))).getId())Swipe Gesture:
js.executeScript("mobile:swipeGesture",
ImmutableMap.of("elementId",((RemoteWebElement)element).getId(),
"direction","left","percent","0.75"));
)Scroll Gesture:
Scroll till particular text present
driver.findElement
(AppiumBy.androidUIAutomator("
new UiScrollable(new UiSelector()).scrollIntoView(text(\"text\"));"));
)Scroll till last
boolean value;
do{
value=(Boolean)js.executeScript("mobile:scrollGesture",
ImmutableMap.of("left","100","top","100","width","200","height","200","direction","down")
);
}while(value);Drag Gesture:
js.executeScript("mobile:dragGesture",
ImmutableMap.of("elementId",((RemoteWebElement)driver.findelement(By.id("sanga"))).getId(),
"endX",100,"endY",300));KeyActions:
clipBoard:
driver.setClipBoard("sangar");
driver.findElement(by.id("sa")).sendKeys(driver.getClipBoard());
driver.pressKey(new KeyEvent(AndroidKey.ENTER));
driver.keyRelease(new KeyEvent(AndroidKey.ENTER))driver keys
driver.pressKey(new KeyEvent(AndroidKey.ENTER));
driver.pressKey(new KeyEvent(AndroidKey.HOME));
driver.pressKey(new KeyEvent(AndroidKey.BACK));Device Rotation:
DeviceRotation rotation=new DeviceRotation(0,0,90);
driver.rotation(rotation);invoking App directly:
js.executeScript("mobile:startActivity",
ImmutableMap.of("intent","packagename/activityName"));
)How to get activity:
open that page in android studio
Now run
adb devices used to find the devices running,if it real device it android deviceadb shell dumpsys window | find "mCurrentFocus"
this will show the current activity.this can be placed in above script to start the app by
directly landing on that pagehidining KeyBoard:
driver.hideKeyBoard();
toast Message:
toast message always present in android.widget.toast from that if we get name attribute
sample script
String taostMessage=driver.findElement(By.xpath("//android.widget.toast")).getAttribute("name");
Assert.assertEquals(toastMessage,"Please Enter your name");
Hybrid App:
Browser Rendering in App is called Hybrid App.
once browser is launched inside app,we have to get the id of that session app,we have to get the id of that session
Set<String> all=driver.getContextHandles();
for(String c:all){
if(!c.contains("native")){
driver.context(c);
break;
}
}
driver,findelement(By.xpath("sangar")).sendKeys("sangar"+Keys.ENTER);
driver.context("NATIVEAPP");In order to automate Chrome(browser) related functions we have to set the browser path in options.
options.setChromeDriverExecutable("sanagjn");we can use browser for inspecting mobile elements by cahnging screen to mobile screen
IOS Apps:
XCUITestOptions options=new XCUITestOptions();
options.setDeviceName("iphone 13 pro");
options.setPlatformVersion("15.5");
options.setApp("jasnfja.app");
options.setWDALaunchtimeout(Duration.ofSecond(100))
IOSDriver driver=new IOSDriver(new URI("127.0.0.1:4723").toURL(),options);NEw Locators:
iosClassChain and iosNSPredicateString
driver.findElement(AppiumBy.iosClassChaing("**/tagname[`label=='asdanag'`]"));
driver.findElement(appiumBy.iosNSPredicateString("type=='sangar" AND value=='sanga'))
driver.findElemnt
(AppiumBy.iosNSPRedicateString("type=='sangar' AND value BEGINSWITH[c] 'sangar'" )).clikc();
driver.findElement(AppiumBy.iosNS PRedicateString("type=='sangar' aND EndsWith[c] 'kumar'")).click()Long Click Gesture:
js.executeScript("mobile:touchAndHold",ImmutableMap.of("element",((RemoteWebElemnt)element).getId(),"duration","2000"));Scroll:
js.executeScript("mobile:scroll",
ImmutableMap.of("element",(RemoteWebElement)element)).getId(),"direction","down"));
Sliding:
usually sliding can be validated if value attribute is 0 it is in beginning stage
if attribute value is 100% it will be in final stage
Invoking inbuild app:
we have to use activity for ios apps
search google for bundle id of ios albums
js.executeScript("mobile:launchApp",ImmutableMap.of("bundleId","com.apple.mob"));
driver.findElement(AppiumBy.iosNSPredicateString("label=='ALl photos'")).click();
//to get count of all photos
get XCUIElementTypeCell
driver.findElements(AppiumBy.iosclassChain("**/XCUIElementTypeCell")).size();Swipe:
js.executeScript("mobile:swipe",ImmutableMap.of("direction","left"))
For Real DeviceS:
once real device connected run script
adb devices
this gives AndroidDevice..
options.setDeviceName("Android Device");
POM:
@AndroidFindBy(id,accessibiltyId,iosClassChain,iosNSPredicateString)
@AndroidFindBys
@AndroidFindAllclass LoginPage{
@AndroidFindBy(id="sangar")
private WebElement element;
public LoginPage(AndroidDriver driver){
this.driver=driver;
PageFactory.initElements(new AppiumFieldDecorator(driver),this);
}
}for IOS LOgin page:
@iosXCUITFindBy
@iosXcuitFindBys
@iosXCUITFindAllAppium driver also can be mentioned instead of androiddriver,iosdriver
4. Pinch Open Gesture
javaCopy code
((JavascriptExecutor) driver).executeScript("mobile: pinchOpenGesture", ImmutableMap.of( "elementId", ((RemoteWebElement) element).getId(), "percent", 1.5 // zoom scale ));
5. Fling Gesture
javaCopy code
((JavascriptExecutor) driver).executeScript("mobile: flingGesture", ImmutableMap.of( "elementId", ((RemoteWebElement) element).getId(), "direction", "down", "speed", 500 ));
IN Extent Report attaching screenshot:
in Listener under ontestFailure include screenshot attaching functionos
public void onTestFailure(ITestResult result){
driver=(AndroidDriver)result.getTestClass().getRealClass().getField('driver');
getScreenshot(driver)
}
BrowserStack:
convert project to browser stock --->integreate with automated SDK
check the yaml file-->it contains username,access key platforms ,platform parallel,browser name ,deviec name
once browser stack integreated,it changed something in pom.xml file also so if we want to run in local we have to use below command while running through mvvn test
browserstack_automation=false mvn test -PRegression -Durl

Comments
Post a Comment