1

I'm looking for a way to find a DialogElement from a child context. Finding it from the "root" context is working fine, but I'm building a Page component for testing and when I try to find the DialogElement using the $(DialogElement.class) approach it tries to find it in the context of the current Element, but Dialog seems to be added to the main/root context, hence it always fails. Any suggestion how to do it?

UPDATE

I found a solution that works from anywhere in the test:

DialogElement dialogElement = new ElementQuery<>(DialogElement.class).context(getDriver()).first()

UPDATE 2

Managed to improve it:

DialogElement dialogElement = $(DialogElement.class).context(getDriver()).first()

If anyone knows a more elegant one feel free to post it.

Balazs
  • 185
  • 1
  • 3
  • 10
  • Why do you put finding the Dialog to the page object? Simply use $(DialogElement) in your tests. We added a method to get the current dialog to the base class of our UI tests – Simon Martinelli Jun 23 '21 at 07:29
  • Because it logically belongs to that page object and I don't want to break the otherwise encapsulated test sequence just to interact with the Dialog. – Balazs Jun 23 '21 at 07:47
  • The dialog is always on top so you will no have a chance to do that. – Simon Martinelli Jun 23 '21 at 08:31
  • I don't see why it shouldn't be possible though – Balazs Jun 23 '21 at 09:06
  • You wrote: "it tries to find it in the context of the current Element" That's why – Simon Martinelli Jun 23 '21 at 09:29
  • I understand why it is not working. I'm looking for a solution and I'm saying that this should be possible technically. – Balazs Jun 23 '21 at 09:33
  • Selenium (which TestBench is based on) doesn't know anything about logical relationships, it only cares about what the actual DOM tree says. `$(DialogElement.class).first()` should do the trick if you only have one Dialog, otherwise you can use e.g. `.id` to get a specific one. – Anna Koskinen Jun 23 '21 at 11:18
  • As I explained in my question this only works if you call it from the test case directly (which uses the root context) and doesn't work from within a "page object" as it uses the page object's context and dialog is being added at the top level. – Balazs Jun 23 '21 at 11:25

1 Answers1

0

Try DialogElement dialogElement = $(DialogElement.class).onPage().first()

onPage() defines that the query should start the search from the root of the page, in practice from the tag.