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

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!

Related

EXCEPTION: stale element reference: element is not attached to the page document

My HTML-Page contains (among other stuff) this bit:
<ol id="links">
<li id="links_1">stackoverflow</li>
</ol>
In my code to test the page, I then do:
el←FindElementById'links_1'
(ACTIONS.MoveToElement el).Build.Perform
and this crashes with
EXCEPTION: stale element reference: element is not attached to the page document
(Session info: chrome=81.0.4044.129)
However, this error doesn't seem to be justified, as the element is still alive:
el.Displayed
1
el.Text
stackoverflow
el.Location
{X=56,Y=282}
How can I fix this problem?
(The environment is APL. I've left out a few APL-details here because I feared they might be avoidable "distraction" from the core-issue)
During my research before posting, I saw the question stale element reference: element is not attached to the page document but it doesn't seem to apply:
as shown, I'm doing the FindElementById and access it immediately after finding it. The DOM doesn't change, page is static.
explanations about it no longer being part of the DOM do not apply: it is found and, as I attempted to show, I can access properties such as Displayed or Text.
I also went through the reference, but this explanation did not help.
Also, there is no looping going on and nothing changes the page. It's really straightforward: GoToUrl * Find * MoveToElement.
I removed the chromedriver-tag, as I can repro this with Firefox and geckodriver. However, with geckodriver, I get "EXCEPTION: Web element reference not seen before:" when I do MoveToElement - but I can do el.Click and access its properties in exactly the same way that as with Chrome.
Update: a coworker investigated this a bit deeper (beyond my comfort zone) and found that before throwing this "stale exception", there is (I'm not sure how to word this properly and where exactly it occurred) a 404-exception. I just know 404 as an HTTP Status-code - that's all it means to me. But clearly the browser was not asked to navigate anywhere, so I can't be related to an HTTP404. Does that perhaps ring some bells with anyone more familiar with the internals of WebDriver?
This was a complex and multi-layered problem, but when I finally removed all layers - it worked!
The key-factor which caused this problem:
due to a fault in the way I had written the test caused it to be executed twice - and the 2nd run always exposed the problem.
I had instantiated ACTIONS in the test-framework and was not aware of a critical feature: it continuously builds a chain of actions, any Build.Perform..-steps just added to that. Solution: create separate instances of every run (possibly, since it's fairly lightweight) or call ACTIONS.Reset(requires WebDriver4). I've never had issues with WD4 (although it still is in alpha) - but this gave the ultimate reason to switch!

Instruments fails to execute send_keys intermittently. How should that be handled?

The Problem: I'm using Appium's python client to send_keys to a WebElement.
And Instruments decides it shouldn't tap certain keys sometimes.
This is a pain when I'm trying to do things like login. I need to be able to reliably type or programmatically set values on input fields.
This is (more or less) what I'm doing, and here's the gist of Instruments complaining about it:
el = driver.find_element_by_xpath('//UIATextField[1]')
el.click()
el.send_keys('ABCDEFGHI')
All pretty standard. And it usually works
But usually isn't cutting it. I need something solid.
Possible Solutions?
I think I can make send_keys work if I do some sort of try/retry if I get a WebDriverException back.
But what would be really cool is if I could set the value of the element through a JavaScript execute_script - or better yet - a selenium python binding!
I don't know JavaScript, and I've already tried searching for how to set the value on the object without doing sendKeys but I've come up blank.
Any ideas would be really helpful. Thanks!
Solution below:
# Get the element
el = driver.find_element_by_class_name('UIATextField')
driver.execute_script("au.getElement('%s').setValue('%s')" % (el.id, 'ABCDEF'))
# At this point, the keyboard is opened and the text is instantly entered.

dojo.dnd.move node rendering issue

Dojo has a basic issue, amongst other things... with its dojo.dnd.move class. The issue which you can see here:
http://archive.dojotoolkit.org/nightly/dojotoolkit/dojo/tests/dnd/test_parent_constraints.html
Is that when you click on a node, and start dragging, the node itself jumps. It actually moves its position. This is extremely problematic and I was wondering if anyone has seen this happen before.
I am creating an application that requires the moving of nodes but it needs to be precise and thus can't have the initial jump.
Any help would be greatly appreciated.
This is caused by the following highlighted code in Mover.js:
https://github.com/dojo/dojo/blob/master/dnd/Mover.js#L91-92
The odd thing is, based on the comments, it seems like this code is causing the very problem it aims to prevent.
That said, if your body has padding: 0 applied to its style, this code shouldn't affect you. (You can test it on that test page by running document.body.style.padding = "0" in the console before dragging.)
You might want to enter a ticket on the Dojo bug tracker at http://bugs.dojotoolkit.org (or maybe search and see if one has already been entered for it).

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.

Is there a way to make actions optional in Selenium IDE?

This is a bit of a newbie question, but... is there a way to make actions optional in Selenium IDE? I'll provide a use case.
In the app I'm testing, users see a "hey, you're agreeing to the ToS by logging on"-type modal window at the beginning of each session. They have to click OK to continue, and they don't see the window again until the next session.
Based on what I've seen so far, I need to have one test suite for the first test each day, and a second test suite for all the others. The second suite is exactly the same except that it doesn't have the "click okay to dismiss the initial modal window" step. Alternatively, I could just remember that my first run of the test each day will fail, and that I have to run the test again.
Both of those "solutions" seem unnecessarily awkward. Can I just make the click command optional?
Create a javascript file called user-extensions.js with the below code. Then go into the Selenium IDE Options dialog and select your user-extensions.js file, restart Selenium and you'll be able to choose TryClick which will work the same as Click but suppress any errors.
Selenium.prototype.doTryClick = function(locator) {
try {
return Selenium.prototype.doClick.call(this,locator);
} catch(err) { return null; }
};
Perhaps overdue, but for future searchers.
You could use the if and endIf statements within the IDE.
If you are using cookies to decide whether to hide the ToS dialog, you could check that a certain cookie is set and if so, skip the click.
I haven't used the selenium IDE much, but I think doing the check would be much easier if you are using a programming language. I am not sure how to do it in HTML tests.
If you are using HTML, you could have a look for Selenium IDE Flow Control and see if that can do what you need. I haven't used this myself, but if looks like it supports if statements. You could use verifyCookie to check if the cookie exists.
Hope that helps.
As aj.esler pointed it out Selenium ID Flow control is a good solution that has worked for me.
Here is the Firefox add on
I use the gotoif, here is an example about how you can use it. When skip value is 1 then it will go to the label=jump line and will not execute everything from gotoif like to label=jump .
Another extremely useful flow control add-on for the IDE is SelBlocks
It will give you the ability to use: if/else/for/foreach/while and even a way to read variables from an XML file.
Use http://wiki.openqa.org/display/SEL/flowControl addon.
Make something like this :
1.storeElementPresent | //button[#name="cookie_law_accept"] | cookie_law
2.goToIf | storedVars['cookie_law']!=true | end
3.click | //button[#name="cookie_law_accept"]
4.label | end
Explain:
1.If element is present it will be stored as a "cookie_law" with value "true"
2.If cookie_law is not "true" - go to label "end" - other way go to next step
3.Click to cookie accept button (only when itsenter code herepresent because it its not - you go to "end" label and you skip this command)
4.You go here if there is no cookie law button :)