2/16/19

Handling Switches Using Appium



Handle Switches Using Emulator
import java.net.MalformedURLException;
import java.net.URL;

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

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class Example3HandleSwitchEmulator {

    public static void main(String[] args) throws MalformedURLException {
   
        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, 9.0);//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
       
        dc.setCapability(MobileCapabilityType.APP, "C:\\apkfiles\\AndroidUI.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
       
       
        //Handling switch element
       
        //1 round
        driver.findElementById("com.android.androidui:id/mySwitch").click();//click on the switch
       
        String val=driver.findElementById("com.android.androidui:id/switchStatus").getText();
       
        System.out.println(val);
       
        //2nd round
       
        driver.findElementById("com.android.androidui:id/mySwitch").click();
       
        val=driver.findElementById("com.android.androidui:id/switchStatus").getText();
       
        System.out.println(val);
       
        driver.quit();
       

    }

}

Handle Switches Using Real Device
import java.net.MalformedURLException;
import java.net.URL;

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

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class Example3HandleSwitchOnRealDevice {

    public static void main(String[] args) throws MalformedURLException {
   
        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.APP, "C:\\apkfiles\\AndroidUI.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
       
       
        //Handling switch element
       
        //1 round
        driver.findElementById("com.android.androidui:id/mySwitch").click();//click on the switch
       
        String val=driver.findElementById("com.android.androidui:id/switchStatus").getText();
       
        System.out.println(val);
       
        //2nd round
       
        driver.findElementById("com.android.androidui:id/mySwitch").click();
       
        val=driver.findElementById("com.android.androidui:id/switchStatus").getText();
       
        System.out.println(val);
       
        driver.quit();
       

    }

}


Handle Switches Using Xpath
import java.net.MalformedURLException;
import java.net.URL;

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

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class Example3HandleSwitchUsingXpathfromInspector {
    public static void main(String[] args) throws MalformedURLException {
       
        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.APP, "C:AndroidUI.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
       
       
        //Handling switch element
       
        //1 round : clicked on switch using xpath
        driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.Switch").click();
       
        String val=driver.findElementById("com.android.androidui:id/switchStatus").getText();
       
        System.out.println(val);
       
        //2nd round
       
        driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.Switch").click();
               
        val=driver.findElementById("com.android.androidui:id/switchStatus").getText();
       
        System.out.println(val);
       
        driver.quit();
       

    }

}

0 comments:

Post a Comment