- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 480
 
Implement Console Events and Page.close tests #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
        
          
                lib/PuppeteerSharp/ConsoleMessage.cs
              
                Outdated
          
        
      | 
               | 
          ||
| public string Text() | ||
| { | ||
| return string.Join(" ", Args); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use arrow function
| public class PageConsoleResponse | ||
| { | ||
| [JsonProperty("type")] | ||
| public string Type { get; set; } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newtonsoft.Json knows how to deserialize enums (StringEnumConverter) so we can use ConsoleType here and remove the conversion in the
ConsoleMessage class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you can also improve Dialog go for it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't think of it back then...
I think I thought that it wouldn't work when there are multiple words (DialogType.BeforeUnload), I just tested it and it works.
[JsonConverter(typeof(StringEnumConverter), true)]
public enum DialogType
{
    Alert,
    Prompt,
    Confirm,
    BeforeUnload
};
JsonConvert.DeserializeObject<DialogType>("beforeunload");
// output: BeforeUnloadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It only works when deserializing since:
JsonConvert.SerializeObject(DialogType.BeforeUnload)
// output: "\"beforeUnload\"" and not beforeunload
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A possible solution is to use [EnumMember(Value = "beforeunload")] for enum values where the default cameCase doesn't work, does this make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the changes, but didn't include "[JsonConverter(typeof(StringEnumConverter), true)]", do you confirm it's necessary ? It works find without it to deserialize, do we need to serialize these values too ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't really need it, don't add it.
| 
           I think everything is OK now, do you confirm?  | 
    
        
          
                lib/PuppeteerSharp/Page.cs
              
                Outdated
          
        
      | private void OnConsoleAPI(MessageEventArgs e) | ||
| private async Task OnConsoleAPI(PageConsoleResponse message) | ||
| { | ||
| // TODO Do we need to translate this part ? | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need these comments? ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I have my answer :)
| } | ||
| 
               | 
          ||
| var handles = message.Args | ||
| .Select(_ => (JSHandle)_frameManager.CreateJsHandle(message.ExecutionContextId, _)) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to cast here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I don't really understand why, but it doesn't recognize the return type of CreateJsHandle :
Unable to cast object of type 'System.Collections.Generic.List<dynamic>' to type 'System.Collections.Generic.IList<PuppeteerSharp.JSHandle>'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's because message.Args is dynamic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's normal, it's the original data, not yet transformed into a JSHandle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
too bad.
ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First time I see _ in use, fancy!
What would be the benefit of using _ instead of defaulting that argument to null in the method?
| @@ -0,0 +1,14 @@ | |||
| namespace PuppeteerSharp | |||
| { | |||
| public enum ConsoleType | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I this should have more options - https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/puppeteer/index.d.ts#L133
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks
…res/page.events.console
…to features/page.events.console
| 
           Argh, I think I did a mistake :/  | 
    
| 
           if it's too hard to revert you can edit the description of this pull-request and add another   | 
    
| Assert.Equal("5", (await message.Args[1].JsonValue()).ToString()); | ||
| 
               | 
          ||
| // TODO Json Serialization is not exactly the same as in Puppeteer | ||
| Assert.Equal("{\r\n \"foo\": \"bar\"\r\n}", (await message.Args[2].JsonValue()).ToString()); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we should get a json, object, and in line 30 we should get a 5 as an int.
I think that ValueFromRemoteObject should try to parse that as a JSON value. I should succeed parsing strings, numbers, and objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, I'll change it to use remoteObject.type (https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-RemoteObject) with a switch to handle all the cases, what do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sounds good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe make JsonValue return Task<T> instead of Task<object>?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this a little bit, the doc says:
Remote object value in case of primitive values or JSON values (if it was requested).
So if we fall there it's because it's serializable, you can just use JSON.Parse in one line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can make it generic, but let's leave that conversion to JSON.NET.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't find a way to make it work for the third assert (with {foo: "bar"}) without using generics, and for the second (with "5"), there are no integers in JS, only numbers, so I have to force it to float. You'll see all this in my next commit.
        
          
                lib/PuppeteerSharp/FrameManager.cs
              
                Outdated
          
        
      | }); | ||
| private void OnExecutionContextCreated(ContextPayload contextPayload) | ||
| { | ||
| 
               | 
          
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this empty line
| } | ||
| 
               | 
          ||
| var handles = message.Args | ||
| .Select(_ => (JSHandle)_frameManager.CreateJsHandle(message.ExecutionContextId, _)) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First time I see _ in use, fancy!
What would be the benefit of using _ instead of defaulting that argument to null in the method?
| 
           The "_" is not null, it corresponds to the items from the Args array. I use underscore for Queryables and Enumerables by habit :)  | 
    
| 
           How about the one on CreateJsHandle @PhenX? #LearningMode  | 
    
| 
           Hum, I'm not sure to understand, do we agree that this :     var handles = message.Args
        .Select(_ => (JSHandle)_frameManager.CreateJsHandle(message.ExecutionContextId, _))is the same as this ?     var handles = message.Args
        .Select(item => (JSHandle)_frameManager.CreateJsHandle(message.ExecutionContextId, item))or even this ?     var handles = new List<JSHandle>();
    foreach (var arg in message.Args)
    {
        handles.Add(_frameManager.CreateJsHandle(message.ExecutionContextId, arg));
    } | 
    
| 
           I think that once one of the argument is of type   | 
    
| 
           @Meir017 yes, I think that's exactly this, I see it with the remoteObject object, I need to convert it explicitly to string in a string variable to get something other than dynamic.  | 
    
…ow real type instead of dynamic value
| 
           How are we going with this guy @PhenX ?  | 
    
| 
           What do you think about my latest changes on jsonvalue?  | 
    
| Assert.Equal("{\r\n \"foo\": \"bar\"\r\n}", (await message.Args[2].JsonValue()).ToString()); | ||
| Assert.Equal("hello", await message.Args[0].JsonValue()); | ||
| Assert.Equal(5, await message.Args[1].JsonValue<float>()); | ||
| Assert.Equal(obj,await message.Args[2].JsonValue<Dictionary<string, object>>()); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about testing this?
Assert.Equal("bar", await message.Args[2].JsonValue<dynamic>().foo);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it could be in addition to the current test
| switch (objectType) | ||
| { | ||
| case "object": | ||
| return JsonConvert.DeserializeObject<T>(objectValue); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think JsonConvert will be able to serialize a "number" a "boolean" and a "bigint", what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's pretty sure it can
| 
           Looking good!  | 
    
| 
           I'll try and do that tomorrow. When do you think you'll release 0.4?  | 
    
| 
           I want to release it during the weekend, we are almost there  | 
    
| var newPage = await Browser.NewPageAsync(); | ||
| var neverResolves = newPage.EvaluateFunctionAsync("() => new Promise(r => {})"); | ||
| 
               | 
          ||
| await newPage.CloseAsync(); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I noticed on Puppeteer is that they are not awaiting the close
https://github.com/GoogleChrome/puppeteer/blob/v1.0.0/test/test.js#L325
        
          
                lib/PuppeteerSharp/ConsoleMessage.cs
              
                Outdated
          
        
      | public ConsoleType Type { get; } | ||
| public IList<JSHandle> Args { get; } | ||
| 
               | 
          ||
| public ConsoleMessage(ConsoleType type, IList<JSHandle> args) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConsoleMessage has a text property which is different from the args
https://github.com/GoogleChrome/puppeteer/blob/v1.0.0/lib/Page.js#L447
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, at the beginning it was present, I didn't see the purpose ... I replaced it by this in ConsoleMessage which does the same
    public string Text() => string.Join(" ", Args);If really needed I'll change it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, we have to honor Puppeteer
…res/page.events.console
           | 
    
| 
           Looks good to me, what do you think @Meir017 ?  | 
    
| 
           @PhenX you have officially completed version 0.4 :)  | 
    
| 
           :D  | 
    
closes #13
closes #5