如何在java中创建一个appium android设备“后退按钮”

时间:2014-08-10 12:29:33

标签: java android selenium-webdriver appium

Appium版本为:1.2.0.1

我看到了一些代码,例如:

// click back button
HashMap swipeObject = new HashMap();
swipeObject.put("keycode", 82);
((JavascriptExecutor)driver).executeScript("mobile: keyevent", swipeObject);

// click back button
HashMap<String, Integer> keycode = new HashMap<String, Integer>();
keycode.put("keycode", 4);
((JavascriptExecutor)driver).executeScript("mobile: keyevent", keycode);

但他们不工作,任何想法如何?

3 个答案:

答案 0 :(得分:18)

您可以使用driver.navigate().back();即可。

答案 1 :(得分:0)

  AndroidDriver driver;
    DesiredCapabilities capabilities=new DesiredCapabilities();
    capabilities.setCapability("automationName", "Appium");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion","4.2");
    capabilities.setCapability("deviceName","SANJJSKFY899DUCU8MZ");
    //below line wen u want to install - asumming u already installed
    //capabilities.setCapability("app", "D:\\Appium_Udemy\\apk files\\BitbarSampleApp.apk");

    capabilities.setCapability("appPackage","com.bitbar.testdroid");
    capabilities.setCapability("appActivity","com.bitbar.testdroid.BitbarSampleApplicationActivity");


    driver=new AndroidDriver(new URL("http://127.0.0.1:5000/wd/hub"), capabilities);

    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);



    driver.findElementByXPath("//android.widget.RadioButton[@index=1]").click();
    //driver.findElementByXPath("//android.widget.EditText[@text()='Please type your name to proceed']").sendKeys("hi manju");
    driver.findElementByXPath("//android.widget.EditText[@index=3]").sendKeys("hi manju");
    Thread.sleep(2000);

    //to press back button of device
    driver.navigate().back();

    //driver.findElementByXPath("//android.widget.Button[@text()='Answer']").click();
    driver.findElementByXPath("//android.widget.Button[@index=0]").click();

答案 2 :(得分:0)

Android设备的“后退”按钮,请使用以下代码

首先导入以下软件包

import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;

使用以下功能

driver.pressKey(new KeyEvent(AndroidKey.BACK));

((PressesKey) driver).pressKey(new KeyEvent(AndroidKey.BACK));