MarketSquare / robotframework-browser

Robot Framework Browser library powered by Playwright.
Apache License 2.0
522 stars 105 forks source link

Keyword "Get page source": Add selector for partial source code #3460

Open simonmeggle opened 7 months ago

simonmeggle commented 7 months ago

I want to propose to add an optional selector to the keyword Get Page Source. The current implementation only supports to get the complete page source which can be a huge amount of data.


Long use case description: The idea for a selector came when I was trying to find a selector for Get Text keyword.

My current way is to F12 into the browser devtools, pick the element and then inspect where I could start to locate. As long as the selector is pure XPath/CSS, I develop the selector in the console wia $x() or $$().

The conveniance feature of Browserlib to concatenate Xpath and CSS with >>, as well as frame selectors (>>>) comes at the cost to built them via the Debug Console of VS code, because the browser console does not understand this syntax. That requires to switch between VS Code and the Browser Devtools multiple times (or to work with tiled windows...)

It would relly help to do something like this:

Get Page Source  p.availability
<p class="availability narrow">
  <span class="key">Durchschnittliche Erreichbarkeit des Dienstes in den letzten 7 Tagen: </span>
  <span class="value bold">100,00 %</span>
</p>
Get Text  p.availability span.value 
100,00 %

Here, I used Get Page Source to get a HTML snippet as output right into the debug console of VS Code. From there I can inspect the interesting HTML source code easily and go on to refine the selector - without switching back to the browser.

aaltat commented 7 months ago

So are you looking what this Playwright API returns https://playwright.dev/docs/api/class-locator in innerHTML ?

simonmeggle commented 7 months ago

So are you looking what this Playwright API returns https://playwright.dev/docs/api/class-locator in innerHTML ?

Yes, exactly.

simonmeggle commented 7 months ago

And lastly, when you want to assert a certain content in the page source, you normally know where it should roughly be. No need to search the complete DOM or even worse, dump it in case of a failure. This is where a selector also would help.

aaltat commented 7 months ago

@allcontributors please add @simonmeggle for ideas

allcontributors[bot] commented 7 months ago

@aaltat

@simonmeggle already contributed before to ideas

Snooz82 commented 1 week ago

@simonmeggle

With Get Property you can just do that.

Get Property    <selector>    innerHTML

This just includes the content of an element.

Get Property    <selector>    outerHTML

This includes the element itself.

I think Get Page Source is really the wrong keyword for that. It literally says in the name Page source. not element.

So maybe we could add a new keyword for innerHTML and outerHTML so that it is more obvious for users. On the other hand, where do we stop?

simonmeggle commented 1 week ago

Ah, nice. Did not know this, thanks!