【问题标题】:How to throw exception if the element not found on the screen如果在屏幕上找不到元素,如何抛出异常
【发布时间】:2019-08-12 20:33:38
【问题描述】:

如果元素不在页面上,我有一个关于引发异常的问题。 我有一些代码,一个测试,我检查一个元素是否在页面上。我通过尝试和捕捉来做到这一点。 在“try”中我放置了断言,在“catch”中我想打印该元素未找到。

Now the button is appear on the page , so i going to the xpath of the element and change it , supposedly signifying the possibility that it does not exist.

@Test
    public void menuOpensOnClick() {
        topMenu.openMenu();
        try {
            Assert.assertTrue(topMenu.getDisconnectButton().isDisplayed());
        } catch (NoSuchElementException e) {
            System.out.println("disconnectButton is not displayed");
        }
    }

Instead of getting the exception with the string "disconnectButton is not displayed"
i Got error :
rg.openqa.selenium.NoSuchElementException: Can't locate an element by this strategy: By.chained({By.xpath: //android.view.ViewGroup[@bounds='[551,842][907,1979]']})

    at io.appium.java_client.pagefactory.AppiumElementLocator.findElement(AppiumElementLocator.java:126)
    at io.appium.java_client.pagefactory.interceptors.InterceptorOfASingleElement.intercept(InterceptorOfASingleElement.java:59)
    at io.appium.java_client.android.AndroidElement$$EnhancerByCGLIB$$d2f22ea4.isDisplayed(<generated>)
    at Tests.HomePage_test.menuOpensOnClick(HomePage_test.java:21)
    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:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction@13e344d (tried for 1 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:304)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at io.appium.java_client.pagefactory.AppiumElementLocator.waitFor(AppiumElementLocator.java:99)
    at io.appium.java_client.pagefactory.AppiumElementLocator.findElement(AppiumElementLocator.java:119)
    ... 27 more

【问题讨论】:

    标签: junit automation appium


    【解决方案1】:

    不是真的。捕捉“异常”太笼统了。

    因此,如果您收到 NullPointerException,您仍然会失败并显示“菜单未打开”消息。

    您的问题在于导入。

    我确定你正在捕捉 java.util.NoSuchElementException,但 org.openqa.selenium.NoSuchElementException 被抛出。

    因此,更改您的导入,它将起作用。

    如果你需要在你的类中使用这两个异常,指定包:

    catch (org.openqa.selenium.NoSuchElementException e)
    

    【讨论】:

    • 它与 appium 一起工作?我没有在我的项目中使用 selenium,我正在测试本机应用程序。
    • Appium 使用 Selenium。我从您的跟踪中获取了org.openqa.selenium.NoSuchElementException
    【解决方案2】:
    Ok i find the solution.
    
    Just catching "Exception" instead of "NoSuchElementException"
    Now it printing me the message that i want.
    
    Not sure, but thinking it is nice solution.
    
    @Test
        public void menuOpensOnClick() {
            topMenu.openMenu();
            try {
                Assert.assertTrue(topMenu.getDisconnectButton().isDisplayed());
            } catch (Exception e) {
                failMessage("The menu is not opened\n" + e.getMessage());
            }
        }
    
    
    Aug 12, 2019 2:27:39 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
    INFO: Detected dialect: W3C
    The menu is not opened
    

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多