StaleElementReferenceException in Geb

487 views
Skip to first unread message

Samuel Rossinovic

unread,
Sep 21, 2017, 7:46:58 PM9/21/17
to Geb User Mailing List
I am confused about those exceptions I'm seeing on content elements. 

In Geb's manual, I read: 

cache

Default value: false

The cache option controls whether or not the definition is evaluated each time the content is requested

So, if Geb evaluates content templates on every access, how can stale element exceptions occur?

Thanks

Marcin Erdmann

unread,
Sep 24, 2017, 2:18:55 PM9/24/17
to Geb User Mailing List
Geb indeed evaluates templates on every call but there are multiple situations when results of that evaluation are "cached" in the context of what you're doing. To name three:
1. Some operations are not atomic. What I mean by that is what looks like a single Geb call results in multiple WebDriver calls. Take $(".description").text() - it actually results in two calls, one to locate the element(s) having description as the class and one to obtain the text of that element. If the element is removed from the DOM in-between the two calls you will get a StaleElementException.
2. Geb's modules have a concept of base element. That element is located when an instance of a module is created and "cached" for the lifetime of that instance. So if you are calling any methods on instance of a module and its base element has been removed from the DOM then you will get a StaleElementException.
3. If you assign elements returned from evaluation of content definitions to a variable and then call multiple methods on that variable then you are effectively "caching" that element in that context. If the element is removed from the DOM between being assigned to the variable and the method calls will get a StaleElementException.

--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+unsubscribe@googlegroups.com.
To post to this group, send email to geb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/38944bfb-daaf-4eac-a803-93583ea5e0bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Samuel Rossinovic

unread,
Oct 3, 2017, 7:13:44 PM10/3/17
to Geb User Mailing List
Is there a way to disable caching in case #2?

My experience with geb so-far, is that it is quite prickly with dynamic pages. I may be able to interact with (module) content for the duration of a call, but then if I call the same method a second time, it will fail, citing "content not found". I keep being dumbfounded by cases where I believe my geb code is mirroring what I see in the browser, yet some underlying, invisible issue fails a test.


On Monday, September 25, 2017 at 4:18:55 AM UTC+10, Marcin Erdmann wrote:
Geb indeed evaluates templates on every call but there are multiple situations when results of that evaluation are "cached" in the context of what you're doing. To name three:
1. Some operations are not atomic. What I mean by that is what looks like a single Geb call results in multiple WebDriver calls. Take $(".description").text() - it actually results in two calls, one to locate the element(s) having description as the class and one to obtain the text of that element. If the element is removed from the DOM in-between the two calls you will get a StaleElementException.
2. Geb's modules have a concept of base element. That element is located when an instance of a module is created and "cached" for the lifetime of that instance. So if you are calling any methods on instance of a module and its base element has been removed from the DOM then you will get a StaleElementException.
3. If you assign elements returned from evaluation of content definitions to a variable and then call multiple methods on that variable then you are effectively "caching" that element in that context. If the element is removed from the DOM between being assigned to the variable and the method calls will get a StaleElementException.
On Fri, Sep 22, 2017 at 12:46 AM, Samuel Rossinovic <samuel.r...@gmail.com> wrote:
I am confused about those exceptions I'm seeing on content elements. 

In Geb's manual, I read: 

cache

Default value: false

The cache option controls whether or not the definition is evaluated each time the content is requested

So, if Geb evaluates content templates on every access, how can stale element exceptions occur?

Thanks

--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.

Marcin Erdmann

unread,
Oct 5, 2017, 5:07:28 PM10/5/17
to Geb User Mailing List
No, there is no way to disable "caching" for #2 because it's explicit and actually done by user code and not Geb code.

I do not think that Geb is prickly with dynamic pages. I'm fairly certain that you'd face similar issues using WebDriver.

There are strategies to work around the problems you are facing. I unfortunately wasn't able to come up with anything different than being diligent and understanding which actions in your application and hence Geb pages and modules cause asynchronous events to occur. I then wrap such events in page or module methods which ensure that the asynchronous action completes before the method returns by using a waitFor() call at the end of the method. If an action causes a module base to be removed or replaced in the DOM then that action unfortunately has to be implemented in the class which contains a definition of that module so that the module instance can be recreated without a StaleElementException being thrown. I can produce an example of what I mean code if the explanation is not clear.

Marcin

To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+unsubscribe@googlegroups.com.

To post to this group, send email to geb-...@googlegroups.com.

Samuel Rossinovic

unread,
Oct 8, 2017, 9:26:28 PM10/8/17
to Geb User Mailing List
Recreating instances is indeed what I'm trying to do. What I am finding challenging is recreating module instances that are content templates of a page. Do these get recreated when a page is browsed using Browser.to()?
If Page transition doesn't involve a to() call, but rather, just an at(), will that affect the above?

Marcin Erdmann

unread,
Oct 9, 2017, 1:16:37 PM10/9/17
to Geb User Mailing List
It's extremely easy to recreate instances of modules which are defined as uncached page contents. A new instance is provided every time a template is accessed:

class PageWithModule extends Page {
    static content = {
        aModule { $('#some-id').module(Module) } 
    }
}

when:
to PageWithModule

then:

Module lifecycle is not tied to page navigation in any sense as long as the content template for the module is not marked as cached.

To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+unsubscribe@googlegroups.com.

To post to this group, send email to geb-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages