Java Appium TestNG测试:无法从辅助菜单列表中选择:NoTouchElementException

Java Appium TestNG测试:无法从辅助菜单列表中选择:NoTouchElementException,java,android,automated-tests,testng,appium,Java,Android,Automated Tests,Testng,Appium,我正在使用maven、testNG和appium桌面构建一个示例appium框架 我想构建并运行一个“简单测试”;只是为了确保在我开始做诸如添加“页面对象”和log4j等复杂的事情之前,我已经准备好了正确的“testNG周长” 我创建了一个非常简单的测试来使用android执行拖放测试 流程来自应用程序主菜单>点击视图>点击拖放>执行拖放 查看下面的菜单 现在我可以从主菜单中选择视图,但不能从视图菜单中点击拖放。我一直收到以下错误消息,据我所知,选择视图的语法是“looks ok” 如果语法错误

我正在使用maven、testNG和appium桌面构建一个示例appium框架

我想构建并运行一个“简单测试”;只是为了确保在我开始做诸如添加“页面对象”和log4j等复杂的事情之前,我已经准备好了正确的“testNG周长”

我创建了一个非常简单的测试来使用android执行拖放测试

流程来自应用程序主菜单>点击视图>点击拖放>执行拖放

查看下面的菜单

现在我可以从主菜单中选择视图,但不能从视图菜单中点击拖放。我一直收到以下错误消息,据我所知,选择视图的语法是“looks ok”

如果语法错误

我做错了什么? 我怎么修理它? TestNG代码

   public class DragnDropTest {
static AndroidDriver<MobileElement> driver;

    @BeforeTest
    public AndroidDriver<MobileElement> initDriver() throws MalformedURLException{



        DesiredCapabilities caps =new DesiredCapabilities();
        caps.setCapability("deviceName", "JacquelineNexus5");
        caps.setCapability("platformName", "Android");
        caps.setCapability("platformVersion", "8.0");
        caps.setCapability("automationName", "UiAutomator2");
        caps.setCapability("appPackage", "com.example.android.apis");
        caps.setCapability("appActivity", "ApiDemos");

        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        return driver;
    }




    @Test 
    //Scenario:  Perform Drag n drop function

    //Given I am on the Home screen 
    //When I  tap  the 'Views'  menu item
    //Then I will see the Views menu 
    //When I am in the 'view menu' I will select 'drag n drop'
    //Then I will see 4 dots in the drag n drop page
    //When  I select the 1st dot and drag it
    //Then I will drop it on the dot below

    public void DragAndDrop(){

        // tap Views  from main menu 
        driver.findElementsById("android:id/text1").get(11).click();


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

        TouchAction t = new TouchAction (driver);

        //Tap Drag and drop from Views Menu
        t.tap(driver.findElementByXPath("//android.widget.EditText[@index= '8']")).perform();

        t.longPress(driver.findElementById("io.appium.android.apis:id/drag_dot_1")).
        moveTo(driver.findElementById("io.appium.android.apis:id/drag_dot_3")).release().perform();
        }

    }   

若您看到appium服务器日志,它会说根据传递的定位器找不到元素

org.openqa.selenium.NoSuchElementException:使用给定的搜索参数在页面上找不到元素。警告:服务器未提供任何stacktrace信息 命令持续时间或超时:0毫秒

由于您已经在测试中添加了60秒隐式等待,但appium不接受它,所以您必须在代码中通过以下功能

    caps.setCapability("newCommandTimeout", "2000");
注意-确保您的xpath是正确的

我还有一个建议,你可以在这里看到官方的appium拖放功能单元测试

它能够找出我做错了什么。我很简单

我不应该用这个:

//Tap Drag and drop from Views Menu
 t.tap(driver.findElementByXPath("//android.widget.EditText[@index= '8']")).perform();
因为eclipse不明白我在做什么,所以我修改了代码,使其不那么复杂

//click on drag and drop menu
driver.findElementByXPath("//android.widget.TextView[@text='Drag and Drop']").click();
下面是TestNG中正确的拖放场景,我已经尝试过了,效果非常好:

public class DragnDropTest {
static AndroidDriver<MobileElement> driver;

    @BeforeTest
    public AndroidDriver<MobileElement> initDriver() throws MalformedURLException{


        DesiredCapabilities caps =new DesiredCapabilities();
        caps.setCapability("deviceName", "JacquelineNexus5");
        caps.setCapability("platformName", "Android");
        caps.setCapability("platformVersion", "8.0");
        caps.setCapability("automationName", "UiAutomator2");
        caps.setCapability("appPackage", "com.example.android.apis");
        caps.setCapability("appActivity", "ApiDemos");
        caps.setCapability("newCommandTimeout", "2000");

        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        return driver;
    }


    @Test 
    //Scenario:  Perform Drag n drop function

    //Given I am on the Home screen 
    //When I  tap  the 'Views'  menu item
    //Then I will see the Views menu 
    //When I am in the 'view menu' I will select 'drag n drop'
    //Then I will see 4 dots in the drag n drop page
    //When  I select the 1st dot and drag it
    //Then I will drop it on to the dot below

    public void DragAndDrop(){

        // tap Views  from main menu 
        driver.findElementsById("android:id/text1").get(11).click();


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


        //click on drag and drop menu
        driver.findElementByXPath("//android.widget.TextView[@text='Drag and Drop']").click();

        TouchAction t = new TouchAction (driver);

        t.longPress(driver.findElementsByClassName("android.view.View").get(0)).
        moveTo(driver.findElementsByClassName("android.view.View").get(2)).release().perform();

        }

    }

谢谢你,伙计。我添加了这个功能,它就像帮派杀手一样工作。是的,正确的功能确实可以很好地驱动测试。
public class DragnDropTest {
static AndroidDriver<MobileElement> driver;

    @BeforeTest
    public AndroidDriver<MobileElement> initDriver() throws MalformedURLException{


        DesiredCapabilities caps =new DesiredCapabilities();
        caps.setCapability("deviceName", "JacquelineNexus5");
        caps.setCapability("platformName", "Android");
        caps.setCapability("platformVersion", "8.0");
        caps.setCapability("automationName", "UiAutomator2");
        caps.setCapability("appPackage", "com.example.android.apis");
        caps.setCapability("appActivity", "ApiDemos");
        caps.setCapability("newCommandTimeout", "2000");

        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        return driver;
    }


    @Test 
    //Scenario:  Perform Drag n drop function

    //Given I am on the Home screen 
    //When I  tap  the 'Views'  menu item
    //Then I will see the Views menu 
    //When I am in the 'view menu' I will select 'drag n drop'
    //Then I will see 4 dots in the drag n drop page
    //When  I select the 1st dot and drag it
    //Then I will drop it on to the dot below

    public void DragAndDrop(){

        // tap Views  from main menu 
        driver.findElementsById("android:id/text1").get(11).click();


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


        //click on drag and drop menu
        driver.findElementByXPath("//android.widget.TextView[@text='Drag and Drop']").click();

        TouchAction t = new TouchAction (driver);

        t.longPress(driver.findElementsByClassName("android.view.View").get(0)).
        moveTo(driver.findElementsByClassName("android.view.View").get(2)).release().perform();

        }

    }