java - Appium TestNG 测试 : Unable to select from secondary Menu list: NoSuchElementException

标签 java android automated-tests testng appium

我正在使用 maven 和 testNG 以及 Appium Desktop 构建示例 appium 框架。

我想构建并运行一个“简单测试”;只是为了确保在开始执行添加“页面对象”和 log4j 等复杂操作之前我拥有正确的“testNG 边界”

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

流程是从应用程序主菜单>点击 View >点击拖放>执行拖放

查看下面的菜单

enter image description here

现在我可以从主菜单中选择“ View ”,但无法从 View 菜单中点击拖放。我不断收到以下错误消息,据我所知,选择 View “看起来不错”的语法。

如果语法错误

  1. 我做错了什么?
  2. 如何修复它?

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();
        }

    }   

Eclipse 控制台消息

[RemoteTestNG] detected TestNG version 6.11.0
Aug 20, 2018 12:17:26 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: DragAndDrop
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z'
System info: host: 'XXX-MacBook-Air.local', ip: '00.00.0.00', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.6', java.version: '1.8.0_71'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{appPackage=com.example.android.apis, deviceScreenSize=1080x1920, networkConnectionEnabled=true, statBarHeight=63, warnings={}, viewportRect={top=63, left=0, width=1080, height=1731}, databaseEnabled=false, deviceName=emulator-5554, platform=LINUX, deviceUDID=emulator-5554, appActivity=ApiDemos, desired={appActivity=ApiDemos, appPackage=com.example.android.apis, platformVersion=8.0, automationName=UiAutomator2, platformName=Android, deviceName=JacquelineNexus5}, platformVersion=8.0.0, pixelRatio=2.625, webStorageEnabled=false, locationContextEnabled=false, automationName=UiAutomator2, takesScreenshot=true, javascriptEnabled=true, deviceScreenDensity=420, deviceModel=Android SDK built for x86, deviceManufacturer=Google, deviceApiLevel=26, platformName=LINUX}]
Session ID: c2726815-8435-4b3d-b3c8-92f6845c616b
*** Element info: {Using=xpath, value=//android.widget.EditText[@index= '8']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:89)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:319)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:421)
    at io.appium.java_client.DefaultGenericMobileDriver.findElementByXPath(DefaultGenericMobileDriver.java:140)
    at io.appium.java_client.AppiumDriver.findElementByXPath(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElementByXPath(AndroidDriver.java:1)
    at android.DragnDropTest.DragAndDrop(DragnDropTest.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

最佳答案

如果您看到 appium 服务器日志,它表示根据传递的定位器未找到元素

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds

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

    caps.setCapability("newCommandTimeout", "2000");

注意 - 确保您的 xpath 正确。

我还有一个建议,您可以在此处查看用于拖放的官方 appium 单元测试功能

关于java - Appium TestNG 测试 : Unable to select from secondary Menu list: NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51929746/

相关文章:

c# - MSTest 中的数据驱动测试 - TestContext.DataRow 的问题

java - 如何使用 spring batch 读取具有关联的 JPA 实体

android - 在 onCreateView 与 onActivityCreated 的 fragment 中检索 Activity 范围的 ViewModel

ruby - 如何对使用 RSpec 的 REST API 的方法进行单元测试?

java - 滚动时出现惰性列表适配器问题

Android 以编程方式将联系人设置为收藏夹

testing - 如何向 Postman Collection Runner 输出动态值

java - 如何使 JOOQ codegen 与基于 Oracle 版本的重定义配合使用?

java - 使用子类化替换未实现接口(interface)的 Java 类

java - 日期时间格式化程序