How to avoid long waits while waiting for asserted throw? - junit5

I'm testing a menu item for non-existence (which is relevant as I want to check whether a condition is correctly met which hides the item). Therefore, I call SWTBotRootMenu#contextMenu(String text) throws WidgetNotFoundException in org.junit.jupiter.api.Assertions.assertThrows.
However, having a number of these tests makes running the tests really slow, as the WidgetNotFoundException is thrown only after a 5s timeout it seems.
Is there a way to set the timeout to a shorter value? Or avoid the wait altogether in a different way?

If you just want to check menu for missing item you can get content SWTBotRootMenu#contextMenu() of whole menu and perform basic check if tested item is present or not.
SWTBotRootMenu context = menu.contextMenu();
assertFalse(context.menuItems().contains(text));

Related

When invoking js.exec in Geb/Spock, the exec method is flagged as 'null'

I am creating a suite of tests (using Geb/Spock) for a web site. In one of them, the element I want to access is on the top of the page, so, to make sure that is visible, I want to scroll to the top of the page.
The command I am using is:
browser.js.exec('window.scrollTo(0, 0);')
or variations of it like
js.exec('window.scrollTo(0, 0);')
or other alternative like:
js.exec('window.scrollBy(0, -250);')
None of them makes the page scroll up, and when executing I get the following error (it is the only error, no other feedback). The error message using the other options listed above is identical (other than the command itself):
Condition not satisfied:
browser.js.exec('window.scrollTo(0, 0);')
| | |
| | null
| geb.js.JavascriptInterface#4019094f
geb.Browser#3dcac33e
at UserCreatesCompany.Go to Home Page and click on the log to
GitHub button as user User1(UserCreatesCompany.groovy:170)
I can not interpret the message that 'exec' is null. What exactly it means?
To make things more interesting, at the end of this script I am running the following cleanup procedure
js.exec('window.scrollTo(0, document.body.scrollHeight);')
DeleteButton.click()
$("button",'data-automation-id':"button-modal-yes").click()
}
and that works well: the page scrolls down. So, does not seem a problem about some missing library.
Any suggestion of what I may be doing wrong?
The version of the different components I am using is:
groovyVersion = '2.5.4'
gebVersion = '2.3'
seleniumVersion = '3.141.59'
chromeDriverVersion = '2.45'
First of all, you should not need to ever manually scroll the page to make elements visible - Selenium WebDriver which is underpinning Geb will do that for you automatically as soon as you start interacting (clicking, setting value, etc) with content.
Secondly, the failure you are getting is a failed assertion coming from a statement in an automatically asserted (then: or expect:) Spock block. It feels to me that you don't understand a concept which is core to Spock and therefore you should read about it in the manual first. It should make the failure you're getting clearer.
Thanks for the answer. Clearly: I was not fully aware of the different constrains the different blocks impose on what is executable or not. The manual is pretty clear once you have stumbled!
I am intrigued by your first assertion pointing that Selenium WebDriver will move to the element as soon as I interact with it. That was my understanding but it was not working. I made sure the element in question had a unique identifier, but still, it was not able to found it if the element had to be found by scrolling up. On the other hand it worked smoothly when locating the element WebDriver scrolled the page down.
Thanks again for the explanation. I have learn something new today!

Selenium - watch for an error condition AND run 'happy path' test code

My app displays an error dialog whenever a JavaScript error occurs. This is always a bad sign, so I want to set up my tests so that, if the error dialog appears, it causes the test to fail there and then.
So I'd like to do something like (very much pseudocode!);
// start a new 'guard' thread;
start {
found = this.driver.wait(untilVisible(By.css('.myErrorDialog')), VERY_LONG_TIMEOUT);
if (found) {
// the error dialog appeared! That's bad!
throw();
}
}
// now run the test
login();
clickButton();
testBannerContains();
But I'm having trouble and I think it has to do with the way Selenium schedules actions.
What I've found is that for a single driver, I can only schedule one thing at a time, so the guard I set up early in the test blocks the body of the test from starting.
Is there a better way to handle conditions like 'this should never happen', or a way to create two independent threads in the same test?
So the problem with the code you have is that it immediately runs it and waits for a VERY_LONG_TIMEOUT amount of time for that error dialog to appear. Since it never does, it continues to wait. You have already discovered that is not what you want... ;)
I haven't done anything like this but I think you want a JS event handler that watches for the event that is triggered when the error dialog appears. See the link below for some guidance there.
Can my WebDriver script catch a event from the webpage?
One option would be to watch for that event to fire and then store true (or whatever) in some JS variable. Before leaving a page, check to see if the variable is set to true and if so, fail the test. You can set and get JS variables using JavascriptExecutor. Some google searches should get you all you need to use it.

Identifying objects in IBM RFT

While executing my script in RFT, my script got failed due to the slight position change of a button. (This button's position slightly changes according to the option selected for previous combo box due to the label appearing near the button)
As there are 2 positions for this button in window, one of my script fails while other passes.
Please suggest how to identify this same object in 2 different places in RFT?
If you're alright with not using pre-mapped values and instead work with objects directly in code (which I've personally found to be extremely useful... it's allowed me to do great and wondrous things with RFT :), the following ought to work fine:
private void clickObject(String uniqueIdentifier) {
// Find object
RootTestObject root = RootTestObject.getRootTestObject();
TestObject[] matchingObjs = root.find(atProperty(".id", uniqueIdentifier));
if (matchingObjs.length > 0) {
// Click the object
((GuiTestObject) matchingObjs[0]).click();
}
// Clean-up
unregister(matchingObjs);
}
Feel free to replace ".id" with whatever property is best suited for the situation... since I work primarily with a web application, the ".id" property has worked splendidly for me.
Because the method finds the object anew each time, it'll grab the object's position wherever it's at at the time the method's called. The clean-up will also prevent any weird, horrible, and otherwise unfortunate UnregisteredObjectExceptions from cropping up.
Without looking at your pages I cannot be sure, but I think the buttons are actually two different buttons. Maybe they are generated by javascript, or they are just un-hidden after the option you select in the combobox.
If they are two different buttons (record them both and look at the recognition properties) you can either replace some properties with a regular expression or check wich button is visible/exists and then click it:
if (btn_button1.exists()) {
btn_button1.click();
} else if (btn_button2.exists()) {
btn_button1.click();
}
Here's a more complete tutorial on Object Recognition.
You can increase the tolerance of Rational Performance Tester AssureScript in the properties tab or you could set the description but hide the value. You can also make a custom code that updates the object map to prepare for this change in a java IF structure

wxWidgets: is it possible to nest two different wxTimers?

I am writing an application that stays in the traybar and do some checks every some minutes.
When it performs this checks, I would like the traybar icon to be animated.
That is why I have a first wxTimer triggering checks. In its OnTimer call I tried to manage the second wxTimer to handle the animation.
The issue is that timers work in the mainloop, so the icon is not updated when the second timer updates the icon index.
Is there a way to overcome this problem?
Thank you!
Your description of the problem is unfortunately not clear at all but if you mean that you don't get timer events until you reenter the event loop, this is indeed true and, moreover, almost tautological -- you need to return to the event loop to get any events.
This is the reason why your event handlers should always execute quickly and return control to the main loop. And if they take too long, the usual solution is to use a background thread for the real work and just schedule it in your event handler, but not wait until it is done.
Basing on Ryan G's comment
It is possible to incorporate wx.Yield() into the main loop. This is usually used to temporarily release the global lock to allow the widgets to update.
It is also possible to create a separate thread to update the animation independently from the main thread.
Using wx.Yield() should be easier to implement.

Selenium: intermittent "element not found" issues

Every now and then my selenium tests randomly fail with an "element X not found" error message. I do a simple select by ID, eg.
click('sideBar_queryButton')
When I use the dom inspector, the element is there, so I wonder why selenium doesn't find it sometimes. When I run the same test again, it works or it breaks again, there seems to be no reliable way of reproducing it. Even tests which are there for ages seem to randomly break and then magically work again. Inserting a few sleep statements sometimes helps but not reliably. So I wonder if I'm using it incorrectly. Has anyone else had these problems with selenium and if so, how did you fix them?
Edit: I found it to be much more reliable to put some test markers in my pages and wait for them to appear. If you use asynchronous operations which might create race conditions in your tests, inserting a test marker into your html after you finish the operation worked pretty well for me. E.g.
$('<div>').addClass("testMarker").append("OpXYZFinished").appendTo($('#content'));
That way, you can do a simple "waitForTextPresent" to see if things worked out and its much more reliable than guessing the browser's loading state. The testmarker class needs to be formatted in a way that it is not visible to the user (e.g. font color == background color).
Thanks for all your comments. After some deeper digging on the net and
in our tests I finally found combining these statements instead of a
simple waitForPageToLoad to be the cure for our issues:
waitForPageToLoad('')
// wait until all ajax activity has ceased. That check's jQuery's $.active
waitForCondition('selenium.browserbot.getUserWindow().$.active == 0', 5000)
// wait a second for all JS to properly initialize
pause(1000)
There is still a pause in there which is somewhat ugly, but it does
the trick.