1-UI Automator viewer- to find locators
2-androidUIautomator
driver.findElementByAndroidUIAutomator("text(\"Views\")").click();
AndroidDriver<AndroidElement> driver;
#########################################################################
File f= new File("src");
File fs= new File(f,"ApiDemos-debug.apk");
DesiredCapabilities cp = new DesiredCapabilities();
cp.setCapability(MobileCapabilityType.DEVICE_NAME,"AndriodTest");
cp.setCapability(MobileCapabilityType.AUTOMATION_NAME,"Uiautomator2");
cp.setCapability(MobileCapabilityType.APP,fs.getAbsolutePath());
driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cp);
//xpath id className, androidUIautomator
/* xpath Syntax
* //tagName[@attribute='value'].
driver.findElementByXPath("//android.widget.TextView[@text='Preference']").click();
driver.findElementByXPath("//android.widget.TextView[@text='3. Preference dependencies']").click();
driver.findElementById("android:id/checkbox").click();
driver.findElementByXPath("(//android.widget.RelativeLayout)[2]").click();
driver.findElementByClassName("android.widget.EditText").sendKeys("hello");
driver.findElementsByClassName("android.widget.Button").get(1).click();
driver.findElementByAndroidUIAutomator("text(\"Views\")").click();
#################################################################################
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import io.appium.java_client.TouchAction;
import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import static java.time.Duration.ofSeconds;
import static io.appium.java_client.touch.offset.ElementOption.element;
Public class gestures extend base {
public static void main(String[] args) throws MalformedURLException {
// TODO Auto-generated method stub
AndroidDriver<AndroidElement> driver=capabilities();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElementByXPath("//android.widget.TextView[@text='Views']").click();
//Tap-GESTURES
TouchAction t =new TouchAction(driver);
WebElement expandList= driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']");
t.tap(tapOptions().withElement(element(expandList))).perform();
driver.findElementByXPath("//android.widget.TextView[@text='1. Custom Adapter']").click();
WebElement pn= driver.findElementByXPath("//android.widget.TextView[@text='People Names']");
//Long Press
t.longPress(longPressOptions().withElement(element(pn)).withDuration(ofSeconds(2))).release().perform();
//Thread.sleep(2000);
System.out.println(driver.findElementById("android:id/title").isDisplayed());
SWIPE
//long press //on element// 2 sec// move to another element and you release-CALENDER
TouchAction t=new TouchAction(driver);
WebElement first=driver.findElementByXPath("//*[@content-desc='15']");
WebElement second=driver.findElementByXPath("//*[@content-desc='45']");
t.longPress(longPressOptions().withElement(element(first)).withDuration(ofSeconds(2))).moveTo(element(second)).release().perform();
SCROLLING
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"WebView\"));");
DRAG AND DROP
WebElement source=driver.findElementsByClassName("android.view.View").get(0);
WebElement destination=driver.findElementsByClassName("android.view.View").get(1);
TouchAction t = new TouchAction(driver);
//longpress(source)/move(destination)//release
//t.longPress(longPressOptions().withElement(element(source))).moveTo(element(destination)).release().perform();
t.longPress(element(source)).moveTo(element(destination)).release().perform();
####################################################################
How to install APK in Emulators.
C:\Users\abc\AppData\Local\Android\Sdk\platform-tools>adb install C:\Users\abc\Downloads\General-Store.apk
Performing Streamed Install
Success
####################################################################
E-commerce App practice exercise:- Hybrid APP
public class ecommerce_tc_1_1 extends baseCode{
public static void main(String[] args) throws MalformedURLException {
AndroidDriver<AndroidElement> driver=capabilities();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// driver.findElement(By.id("com.androidsample.generalstore:id/nameField")).sendKeys("Hello");
Hide Keyboard
// driver.hideKeyboard();
driver.findElement(By.xpath("//*[@text='Female']")).click();
driver.findElement(By.id("android:id/text1")).click();
DropDown Scroll to particular list
driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"Jordan 6 Rings\").instance(0))"));
// driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"" + containedText + "\").instance(0))"));
driver.findElement(By.xpath("//*[@text='Argentina']")).click();
driver.findElement(By.id("com.androidsample.generalstore:id/btnLetsShop")).click();
Toast Message- error message
String toastMessage=driver.findElement(By.xpath("//android.widget.Toast[1]")).getAttribute("name");
System.out.println(toastMessage);
Assert.assertEquals("Please enter your name", toastMessage);//Actual validation
//name attribute for toast messages will have content
driver.findElement(By.xpath("//*[@text='Argentina']")).click();
driver.findElement(By.id("com.androidsample.generalstore:id/btnLetsShop")).click();
driver.findElements(By.xpath("//*[@text='ADD TO CART']")).get(0).click();
driver.findElements(By.xpath("//*[@text='ADD TO CART']")).get(0).click();
driver.findElement(By.id("com.androidsample.generalstore:id/appbar_btn_cart")).click();
Thread.sleep(4000);
//Mobile Gestures
WebElement checkbox = driver.findElement(By.className("android.widget.CheckBox"));
TouchAction t = new TouchAction(driver);
t.tap(tapOptions().withElement(element(checkbox))).perform();
driver.findElement(By.id("com.androidsample.generalstore:id/btnProceed")).click();
Thread.sleep(7000);
NAtiveApp view to Hybrid Web View
Set<String> contexts = driver.getContextHandles();
for (String contextName : contexts)
{
System.out.println(contextName);
}
driver.context("WEBVIEW_com.androidsample.generalstore");
driver.findElement(By.name("q")).sendKeys("hello");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER)
driver.pressKey(new KeyEvent(AndroidKey.BACK)) driver.context("NATIVE_APP");
}}
public class BaseCode {
public static AndroidDriver<AndroidElement> capabilities() throws MalformedURLException
{
AndroidDriver<AndroidElement> driver;
// TODO Auto-generated method stub
File appDir = new File("src");
File app = new File(appDir, "General-Store.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
//Emulator
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "AndriodTest");
//android Device
// capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");
//capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,"Chrome");
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"Uiautomator2");
//capabilities.setCapability("chromedriverExecutable","C:\\Selenium-drivers\\chromedriver\\chromedriver.exe");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
Comments
Post a Comment