【问题标题】:Appium TestNG Test : Unable to select from secondary Menu list: NoSuchElementExceptionAppium TestNG 测试:无法从二级菜单列表中选择:NoSuchElementException
【发布时间】:2019-01-26 12:35:12
【问题描述】:

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

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

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

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

下面的视图菜单

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

如果语法错误

  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)

【问题讨论】:

    标签: java android automated-tests testng appium


    【解决方案1】:

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

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

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

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

    注意 - 确保您的 xpath 正确。

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

    【讨论】:

    • 谢谢伙计。我添加了该功能,它就像帮派克星一样工作。
    • 是的,正确的能力确实可以很好地推动测试。
    【解决方案2】:

    好的,

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

    我不应该使用这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2020-01-20
      • 2018-07-22
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2020-01-10
      相关资源
      最近更新 更多