Selenide terminating testcase if element not found

676 views
Skip to first unread message

santoshsarma jayanthi

unread,
Aug 7, 2015, 1:47:48 AM8/7/15
to selenide
Hello,


Selenide terminating program if any given element not found by throwing error.

Here is one such example.

Exception in thread "main" Element not found {div#div1>ul>li>span}


Expected: visible


Screenshot: /tests/1438924106477.1.png


Timeout: 10 s.


Caused by: NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"div#div1>ul>li>span"}


        at com.codeborne.selenide.impl.AbstractSelenideElement.createElementNotFoundError(AbstractSelenideElement.java:648)


        at com.codeborne.selenide.impl.WaitingSelenideElement.createElementNotFoundError(WaitingSelenideElement.java:74)


        at com.codeborne.selenide.impl.AbstractSelenideElement.checkCondition(AbstractSelenideElement.java:491)


        at com.codeborne.selenide.impl.AbstractSelenideElement.should(AbstractSelenideElement.java:462)


        at com.codeborne.selenide.impl.AbstractSelenideElement.invokeShould(AbstractSelenideElement.java:294)


        at com.codeborne.selenide.impl.AbstractSelenideElement.dispatchSelenideMethod(AbstractSelenideElement.java:180)


        at com.codeborne.selenide.impl.AbstractSelenideElement.dispatchAndRetry(AbstractSelenideElement.java:90)


        at com.codeborne.selenide.impl.AbstractSelenideElement.invoke(AbstractSelenideElement.java:65)


        at com.codeborne.selenide.impl.WaitingSelenideElement.invoke(WaitingSelenideElement.java:18)


        at com.sun.proxy.$Proxy3.shouldBe(Unknown Source)


        at com.codeborne.selenide.impl.AbstractSelenideElement.dragAndDropTo(AbstractSelenideElement.java:391)


        at com.codeborne.selenide.impl.AbstractSelenideElement.dispatchSelenideMethod(AbstractSelenideElement.java:275)


        at com.codeborne.selenide.impl.AbstractSelenideElement.dispatchAndRetry(AbstractSelenideElement.java:90)


        at com.codeborne.selenide.impl.AbstractSelenideElement.invoke(AbstractSelenideElement.java:65)


        at com.codeborne.selenide.impl.WaitingSelenideElement.invoke(WaitingSelenideElement.java:18)



I don't want to terminate the case but, want to catch exception and write it to some file for further verification. How can I achieve that ? 

P.S : It is not JUnit testcase. I've written it in simple java program.


Thanks & Regards,
SantoshSarma J V

Andrei Solntsev

unread,
Aug 7, 2015, 6:25:53 AM8/7/15
to selenide
Hi SantoshSarma!
I cannot imagine why do you need it. Why not just write automated test? Then you don't need to manually save and analyze anything.

If you still need it, just use try/catch clause:

try {
try {
  $("#from").dragAndDropTo("#to")
}
catch (AssertionError e) {
   // ... save and analyze ...
}

пятница, 7 августа 2015 г., 8:47:48 UTC+3 пользователь santoshsarma jayanthi написал:

Travis

unread,
Aug 7, 2015, 1:32:20 PM8/7/15
to selenide
I'm running into the same problem in some situations.  For example, when switching frames, I first tried this:

try {
   switchTo
().frame($(By.xpath("//frame[@name='theFrame']")));
}
catch (NoSuchFrameException ex) {
   System.out.println("Frame doesn't exist.  Continuing anyways.");
}


This is at a point in the test where theFrame might not exist on the page yet.  The test is checking to see if the element exists, but first it has to switch to the right frame.  When the frame doesn't exist, an error like the one from santoshsarma jayanthi is displayed, and java exits completely.  It never reaches the catch block.

However, if instead I use this:

try {
   switchTo
().frame("theFrame");
}
catch (NoSuchFrameException ex) {
   System.out.println("Frame doesn't exist.  Continuing anyways.");
}

the NoSuchFrameException is caught, and program execution continues.

It's easy enough to work around this by using the second method (which I think is the better method anyways), but it seems like the NoSuchFrameException should be caught in both situations.

Travis

Travis

unread,
Aug 7, 2015, 1:42:43 PM8/7/15
to selenide
I should also add that I'm using selenium 2.46.0 and selenide 2.21.

Andrei Solntsev

unread,
Aug 7, 2015, 2:02:32 PM8/7/15
to Travis, selenide

Isn't just a timing issue? I guess method switchTo() works equally in both cases, but frame appeared with different speed.

--
You received this message because you are subscribed to the Google Groups "selenide" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenide+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Travis

unread,
Aug 7, 2015, 3:53:17 PM8/7/15
to selenide, travis...@gmail.com
It doesn't seem like a timing issue.  I'm running it in a debugger (Eclipse), so when it breaks on this line it has plenty of time to fully load the page.  I've also tried it without the debugger, with the same result.  The stack trace I'm getting is this:

Exception in thread "main" Element not found {By.xpath: //frame[@name='theFrame']}
Expected: exist
Timeout: 4 s.
Caused by: NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//frame[@name='theFrame']"}
 at com
.codeborne.selenide.impl.AbstractSelenideElement.createElementNotFoundError(AbstractSelenideElement.java:662)
 at com
.codeborne.selenide.impl.WaitingSelenideElement.createElementNotFoundError(WaitingSelenideElement.java:74)
 at com
.codeborne.selenide.impl.AbstractSelenideElement.dispatchAndRetry(AbstractSelenideElement.java:112)
 at com
.codeborne.selenide.impl.AbstractSelenideElement.invoke(AbstractSelenideElement.java:67)
 at com
.codeborne.selenide.impl.WaitingSelenideElement.invoke(WaitingSelenideElement.java:18)
 at com
.sun.proxy.$Proxy0.getWrappedElement(Unknown Source)
 at org
.openqa.selenium.remote.internal.WebElementToJsonConverter.apply(WebElementToJsonConverter.java:49)
 at org
.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.frame(RemoteWebDriver.java:894)
 at com
.codeborne.selenide.SelenideTargetLocator.frame(SelenideTargetLocator.java:32)
 
...
 
...
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//frame[@name='theFrame']"}
Command duration or timeout: 30 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'


*** Element info: {Using=xpath, value=//frame[@name='theFrame']}
Session ID: ...
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=39.0.3}]
 at sun
.reflect.GeneratedConstructorAccessor10.newInstance(Unknown Source)
 at sun
.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java
.lang.reflect.Constructor.newInstance(Unknown Source)
 at org
.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
 at org
.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
 at org
.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
 at org
.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:358)
 at org
.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:455)
 at org
.openqa.selenium.By$ByXPath.findElement(By.java:358)
 at org
.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350)
 at com
.codeborne.selenide.impl.WebElementSelector.findElement(WebElementSelector.java:28)
 at com
.codeborne.selenide.impl.WaitingSelenideElement.getActualDelegate(WaitingSelenideElement.java:55)
 at com
.codeborne.selenide.impl.AbstractSelenideElement.dispatchSelenideMethod(AbstractSelenideElement.java:281)
 at com
.codeborne.selenide.impl.AbstractSelenideElement.dispatchAndRetry(AbstractSelenideElement.java:92)
 
... 15 more
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//frame[@name='theFrame']"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: '...', ip: '...', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Driver info: driver.version: unknown
 at
<anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/.../anonymouswebdriver-profile/extensions/fxdr...@googlecode.com/components/driver-component.js:10299)
 at
<anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/.../anonymouswebdriver-profile/extensions/fxdr...@googlecode.com/components/driver-component.js:10308)
 at
<anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/.../anonymouswebdriver-profile/extensions/fxdr...@googlecode.com/components/command-processor.js:12282)
 at
<anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/.../anonymouswebdriver-profile/extensions/fxdr...@googlecode.com/components/command-processor.js:12287)
 at
<anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/.../anonymouswebdriver-profile/extensions/fxdr...@googlecode.com/components/command-processor.js:12229)

Andrei Solntsev

unread,
Sep 1, 2015, 4:24:41 PM9/1/15
to Travis, selenide
Hi Santoshsarma and Travis,
I have registered an issue on github: https://github.com/codeborne/selenide/issues/212
It seems to be similar to another issue: https://github.com/codeborne/selenide/issues/206

Please confirm that this is exactly your problem, and you have no other problems at the moment.

Andrei Solntsev

Travis

unread,
Sep 9, 2015, 1:39:03 PM9/9/15
to selenide, travis...@gmail.com
Looks good to me.  Thanks for keeping at it!

cea...@gmail.com

unread,
Sep 30, 2015, 12:21:07 PM9/30/15
to selenide
I'm also running into this issue, and I'm trying to iterate over a list of paragraphs that sometimes contains links, I don't care on the order of the links, just that they are there. This should just throw the underlying exception and not try to do something with it, because there will be situations where I want to do negative testing.
Reply all
Reply to author
Forward
0 new messages