SeleniumLibrary icon indicating copy to clipboard operation
SeleniumLibrary copied to clipboard

Feature Request - Add a 'Get Logs' keyword to retrieve the browser console logs

Open sergi-comeche-housfy opened this issue 3 years ago • 13 comments

Similar to what 'get_log()' can do on python Selenium, I think we are missing a direct way to obtain console and even network logs which can be useful to verify some browser data.

I've been trying different options to obtain this info but couldn't find a Robot Framework direct way to do it. In Python, using driver.get_log("chrome"), for instance, is a simple way to retrieve this data and proceed with further verifications.

Thanks in advance for the support!

sergi-comeche-housfy avatar May 12 '22 15:05 sergi-comeche-housfy

It's on the list of Selenium 4 ToDo's but was middle of the pack in our community wants survey ..

Here is the results, ranked in order of preference, of the new Selenium 4 feature requests community survey

1. Relative Locators
2. Element Attributes and Properties
3. Simulate a mobile device viewport size
4. Basic Authentications
5. Collect Performance Metrics
6. Listen to/Retrieve events from console.log
7. Emulate GeoLocation
8. Retrieve JS Exceptions
9. Create and Switch to a new (blank) tab or window.
10. Print a page as a PDF
11. Timeout getters
12. Add/Expand Microsoft Edge Option using Chromium capabilities

in addition, some write in items

- A couple request for full page screenshots
- A proxy that runs multiple screenshots for compatibility
- Manipulate traffic

One other item that no one mentioned was Shadowdom support. I thought of this too far into the survey to add it. No one raised it in the open section but it is something mentioned occasionally that Selenium has added/is adding.

I need to circle back to this list and set some time aside for development. I will add that pull requests or pair programming to add new features is welcomed!

emanlove avatar May 12 '22 15:05 emanlove

I literally added this in 5 seconds on my local, I mean, It's pretty basic. It was just surprising how something that useful when testing browser is not considered in the main library, specially when it's 1 line of code. But I appreciate that you shared your to-do list, that gives us all a future sight of what you are focusing your efforts.

Thank you for your quick response.

sergi-comeche-housfy avatar May 12 '22 15:05 sergi-comeche-housfy

Can you share a snippet of your code? I would be curious how you are using it. There are a few articles I know of which highlight and give examples of this functionality, like the this Selenium Doc. Although as I think about that, that example is listening to the logs which I am not sure is the same as grabbing or downloading the logs.

This opens up to me the design consideration for adding this looking at the overall design. Some questions or design considerations (in no particular order other than when they come to my mind) ..

  • What would the keyword name be? (Does it conflict with any other keyword(s) in other libraries?)
  • Can we both listen to and grab the log?
  • Can one clear out the log? Can one start listening and then stop listening (i.e. save part of the log)?
  • How do we configure the saving/logging? Do the specify a filename or do we also provide a default name like the screenshots?
  • My understanding is that the bidi functionality for selenium is currently implemented by selenium but the WC spec is not complete. What design consideration do we need to consider because of this?
  • This appears to be Chrome only. How do we handle browser specific keywords/functionality?
  • ( .. anything else ..?)

Other examples or references: "BiDirectional APIs in Selenium 4" from SauceLabs "Selenium 4 With Python: All You Need To Know" by Himanshu Sheth "Selenium 4 - What's new and how you can use it!" with Davis Burns

emanlove avatar May 12 '22 16:05 emanlove

Just thought of another question which I feel it is important to ask ..

  • How is either BroswerLibrary handling logs? Or other robot web testing solutions .. QWeb, RoboCorp/RPAFramework?

To me building up some consistency and, more so, familiarity is important to building a solution that is end user friendly.

emanlove avatar May 12 '22 17:05 emanlove

@sergi-comeche-housfy I should also note that the order list was the voted upon community survey response, coming from about 20-30 submissions. I suspect for various reasons development might be a little different. For example I thought creating new tabs windows would be both easy to implement and popular request. Turns out that it was not in a small response survey. And relative locators, although top rated, has some large design questions and implementation and testing requirements. So I don't want to say "item 6 will only be done after item 5 which will only be done after ... ". This was just the preferences of those who choose to respond to the survey.

emanlove avatar May 12 '22 21:05 emanlove

As for now, I only added a piece of code in "browsermanagement.py" to be able to obtain the browser logs, which solved my problem. So I didn't really consider all the questions you raised and I don't even have an answer for them, it's beyond my knowledge.

@keyword
def get_logs(self, log_type: str = "browser") -> str:
    """Returns the browser selected logs."""
    return self.driver.get_log(log_type)

As you can see in the image, I'm just trying to provide a simple keyword for this functionality and only for Chrome. I guess there is much more work to do before setting it on release with every possible issue taken into consideration, but I needed it and had to do a quick patch for myself. image

Thanks for your time and I'll keep checking on this. Take care.

sergi-comeche-housfy avatar May 13 '22 07:05 sergi-comeche-housfy

I am re-reading / re-thinking / re-understanding this. My original mindset was on (DevTool) console logs which my understanding were not available expect through Chrome using the Chrome DevTools Protocol (CDP) and then this was a little bit of a hack. This doc image showing the get_log function is on the generic remote driver which works across all browsers.

But the official listed (and thus supported) options I see listed don't include using chrome. Looking to see any reference about this "hidden" option ..

In addition I see that there was an initial attempt to put this get_log functionality into the code [Read through #574]. [Side discussion] This is an absolutely interesting historical record. First it highlights those larger questions about keyword design, architecture, and functionality. I'll add when someone raises these up try not let this either discourage you or slow you down. Even as I wrote above my concerns sometimes functionality can be broken down into smaller parts and added in more quickly. But we are also balancing it with the larger issues and usage. You might be able to see that in #574. On a personal note I also see my role along with Tatu's and Pekka's in the continuing leadership and maintainership of the library .. ok coming back from memory lane :) ..

emanlove avatar May 14 '22 11:05 emanlove

And @sergi-comeche-housfy as I read through #574 more closely I see that Tatu notes that Jani has put in the Get Log keyword into the SeleniumTestability library.

emanlove avatar May 14 '22 11:05 emanlove