我是Appium的新手,我试图执行一个执行点击操作的简单程序。但是单击操作并未发生。这是代码:
package com.android.touchactionss;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
public class Sample {
public static void main(String[] args) throws InterruptedException, MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "xiaomi-2014818-204648717d62");
cap.setCapability("version", "5.1.1");
cap.setCapability("appActivity", "com.mediamushroom.copymydata.app.EasyMigrateActivity");
cap.setCapability("appPackage", "com.mediamushroom.copymydata");
AndroidDriver<?> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
Thread.sleep(5000);
try{
System.out.println("STARTED");
driver.findElementByAndroidUIAutomator(
"new UiSelector().resourceId(\"com.mediamushroom.copymydata:id/NextButton\")");
//driver.findElement(By.id("//*[@resource-id='com.mediamushroom.copymydata:id/NextButton']"));
System.out.println("ENDED");
}
catch(Exception exception){
exception.printStackTrace();
}
Thread.sleep(5000);
driver.quit();
}
}
没有引发异常,但是没有发生单击操作。我尝试使用driver.findElement(By.id(""))
和driver.findElementByAndroidUIAutomator()
方法。但是他们都不起作用。我已经附加了对象属性屏幕。
答案 0 :(得分:1)
首先,添加以下导入:
import io.appium.java_client.android.AndroidElement;
接下来更改您的代码:
AndroidDriver<?> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
收件人:
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
您可能需要将URL更改为0.0.0.0,但这取决于您的Appium Server设置。他们可能是正确的,现在是这样。
最后,您需要使用以下方法单击元素:
driver.findElement(By.id("com.mediamushroom.copymydata:id/NextButton")).click();
答案 1 :(得分:0)
您好,以下几行是示例,尝试使用一些测试框架,Junit,TestNg,在该示例中,我删除了main,TestNG:
启动Appium服务器:
appium --address 127.0.0.1 --port 4723
当Appium服务器正在运行时,请调用以下代码:
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class TestAppium {
AndroidDriver<MobileElement> driver;
@BeforeTest
public void setup() {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "emulator-5554"); //used emulator, but should be set devices guid in Your case "xiaomi-2014818-204648717d62"
cap.setCapability("version", "5.1.1");
cap.setCapability("appActivity", "com com.mediamushroom.copymydata.app.EasyMigrateActivity");
cap.setCapability("appPackage", "com.mediamushroom.copymydata");
try {
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void testAppiumSimulator() {
MobileElement element = driver.findElement(By.id("NextButton"));
element.click();
// do some Assertion
Assert.assertTrue(//some condition//);
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
这是一个简单的Appium测试...
希望这会有所帮助,