如果在屏幕上找不到元素,如何引发异常

时间:2019-08-12 08:16:04

标签: junit automation appium

如果元素不在页面上,我有一个引发异常的问题。     我有一些代码,一个测试,用于检查页面上是否有元素。我做到这一点。     在“尝试”中放入断言,然后在“捕获”中打印未找到该元素。

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

2 个答案:

答案 0 :(得分:1)

不是。捕获“异常”过于笼统。

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

您的问题出在进口上。

我确定您正在捕获java.util.NoSuchElementException,但是抛出了org.openqa.selenium.NoSuchElementException

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

如果您需要在课程中同时使用这两个异常,请指定包:

catch (org.openqa.selenium.NoSuchElementException e)

答案 1 :(得分:0)

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