Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Empty Array #45

Open
racmd opened this issue Feb 25, 2020 · 24 comments
Open

Empty Array #45

racmd opened this issue Feb 25, 2020 · 24 comments

Comments

@racmd
Copy link

racmd commented Feb 25, 2020

I'm currently following one of the examples to try get the response data
`
module.exports = {
'LogIn': function (browser) {

    browser

        .url(browser.globals.baseUrl, function () {

            browser.pause(1000)
            .assert.urlContains(browser.globals.loginUrl)
        })
        .waitForElementVisible('#email-input')
        .setValue(selector.LOGIN.EMAIL, browser.globals.user_credentials.login)
        .setValue(selector.LOGIN.PASSWORD, atob(browser.globals.user_credentials.password))
        .click(selector.LOGIN.SIGNIN)
        .listenXHR()
        .waitForElementVisible(selector.BUTTONS.UPLOAD)
        .getXHR('', 1000, function (xhrs) {
            console.log(xhrs)
        })
}

}`

However when i tried to run the above code, it returns an empty array

@Pieras2
Copy link

Pieras2 commented Feb 25, 2020

I had also problem with this xhr npm module.
What helped me was to change in its source file.
Find in node_modules its code with listener onLoadend. Make a copy of it and change the trigger to onLoadstart. It helped me to see some data but unfortunately it did not found all the hxrs I needed. I posted some example of it on github under nightwatch-xhr

@Pieras2
Copy link

Pieras2 commented Feb 25, 2020

Screenshot_20200225-234217_Samsung Internet

@racmd
Copy link
Author

racmd commented Feb 25, 2020

`

this.onloadend = function () {
if (this.readyState === XMLHttpRequest.DONE) {
var xhr = getXhr(this.id);
if (xhr) {
xhr.httpResponseCode = this.status;
xhr.responseData = this.response;
xhr.status = this.status === 200 ? 'success' : 'error';
xhr.responseHeaders = this.getAllResponseHeaders();
}
}
};
`

this one ?
where do i paste it ?

@Pieras2
Copy link

Pieras2 commented Feb 25, 2020

Please find file client.js in node_modules where this package is installed. If you use visual studio code the keyboard shortcut ctrl+shift+h does the search among all filez.

@racmd
Copy link
Author

racmd commented Feb 25, 2020

yes i found the client.js in node_modules and also found the function you want me to copy.
next step is just basically paste it on top of this.onloadend = function ()
but different function name ? this.onloadstart = function() <--- pasted from this.onloadend function

@Pieras2
Copy link

Pieras2 commented Feb 25, 2020 via email

@racmd
Copy link
Author

racmd commented Feb 25, 2020

doesn't seem to change anything in my case :(

@Pieras2
Copy link

Pieras2 commented Feb 25, 2020

Tommorow I will try to install it and provide some example of how it's working. Did you check in browser inspector that the request you want to get are really XHRs or not else?

@racmd
Copy link
Author

racmd commented Feb 25, 2020

i shall try that
thank you so much for your help

@Pieras2
Copy link

Pieras2 commented Feb 25, 2020

Aaaa one more thing. I don't remember if it was mentioned before somewhere but it matters if the click() is prior get() or after. I think click() is supposed to be before and next line is with get

@Pieras2
Copy link

Pieras2 commented Feb 26, 2020

@racmd I installed pure installation of nightwatch and nightwatch-xhr and managed to get some XHRs (however installation of nightwatch, then configuration is a disaster - documentation sorry sucks).

I made such a test.js and it works


module.exports = {
    'LogIn': function (browser) {
        browser
            .url("https://www.webpagetest.org/")
            .click('a[title="About"]')
            .listenXHR()
            .getXHR('', 12000, function (xhrs) {
                console.log('>>>>' + JSON.stringify(xhrs))
            })
    }
}

It looks that it won't work if you replace lines click() and listenXHS() - click is a trigger then listenXHR applies. It is important to give bigger delay in get - no idea why but it requires wait until everything finishes. With this example you don't have to modify anything in nightwatch-xhr source files.

Please let me know if this works for you - copy paste of test

@JalilArfaoui
Copy link
Collaborator

Hi everyone,

Just to let you know that although I did work on this repo and added some features, I did not have time recently to handle open issues :-/

But that doesn’t mean project is dead, and I guess PR are still welcomed if you spotted any issue with current code !

@Pieras2
Copy link

Pieras2 commented Feb 26, 2020

@JalilArfaoui honestly speaking it's difficult to achieve positive results.

I am not programmer, experienced tester which begins with programming but I don't go really deep in tools. I find problems with using nightwatch-xhr however it is really important in e2e testing to monitor background traffic.

I'm not able to investigate and provide fixes but at least provide some examples when it doesn't work. I can help to find scenarios when it doesn't work to help make the tool better - providing you have time to analyze. Maybe I won't give dozens of scenarios but I could make some clean project in visual studio code where I would put my code examples and share it here < even if the commit would be readme update, then it's already a success to know how to do and what not to do (in testing code).

@racmd
Copy link
Author

racmd commented Feb 26, 2020

@Pieras2
Hi I tried using your example, it does work using the example url you given.
however for some unknown it doesn't work on the url I'm currently using to login into

@Pieras2
Copy link

Pieras2 commented Feb 26, 2020

@racmd, ok so we now know that the code and usage is correct.
What I know ...

  1. this doesn't work for all the request I had in my project (no idea about reason etc)
  2. in original shape of nightwatch-xhr I guess it has problem with requests that are sent twice and e.g. first is cancelled (not fully sure, in my project I noticed some requests were doubled or 1st is always get for options, then the request I expect) ... no idea to me why this module doesn't do the job.

Now you can try this ...

  1. enter node_modules/nightwatch-xhr/es5/client.js
  2. between line 34 and 35 paste this block

 this.onloadstart = function () {
                if (this.readyState === XMLHttpRequest.DONE) {
                    var xhr = getXhr(this.id);
                    if (xhr) {
                        xhr.httpResponseCode = this.status;
                        xhr.responseData = this.response;
                        xhr.status = this.status === 200 ? 'success' : 'error';
                        xhr.responseHeaders = this.getAllResponseHeaders();
                    }
                }
            };

Save this file and try again.
I read on this page https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange about events. Now it is the moment when we have to get some knowledge or find some smart programmer who knows HTTP etc. to improve nightwatch-xhr or at least could provide some tricks where change and what in source files

@Pieras2
Copy link

Pieras2 commented Feb 26, 2020

I would be happy if more people shared their feedback and experience with XHR monitoring.

@racmd
Copy link
Author

racmd commented Feb 26, 2020

Interesting
it works on some website and doesn't work on others
I tried doing it with Amazon and Airbnb, it works
but then trying it on google and my website it doesn't work (empty array)

@racmd
Copy link
Author

racmd commented Feb 27, 2020

@Pieras2
So i tried doing your fixes unfortunately it still doesn't work (empty array) on the website I'm currently testing.

@Pieras2
Copy link

Pieras2 commented Feb 27, 2020 via email

@christophedebatz
Copy link

Same problem than @racmd
Empty array when testing with getXHR and unable to trigger the click event when I use the waitForXHR command (unable to locate element... but my element is clickable because without nightxatch-xhr, the click is working well). By the way, the query I want to listen to is triggered (I see it on the Network tab of developers tool). 👍

@Pieras2
Copy link

Pieras2 commented Apr 17, 2020

I open popcorn ;) All I mean is that this XHR plugin doesn't work ok. Needs further development. I wish it was working as described - and as you guys also confirm, it's far from perfect :(

@christophedebatz
Copy link

christophedebatz commented Apr 17, 2020

@Pieras2 the code to spoof the network with Javascript is very simple in fact. Just have a look to this code and you will understand what the plugin is really doing: https://gist.github.com/icodejs/3183154 This plugin was a way to go straight for me. But the time I passed on it begins to be more than I wanted. So, I will try rewrite my own nightwatch command to do the same. Have a good day.
Good luck for the rest ;-)

@Pieras2
Copy link

Pieras2 commented Apr 17, 2020

I will try. Thank you.

@Bugazelle
Copy link

Hello @racmd @Pieras2 @christophedebatz ,

These days, I also trying to capture the network traffics, and has the same Empty Array issue.

Luckily, I have figured out a walkaround to capture the XHR request.
@christophedebatz : thank you to point out the https://gist.github.com/icodejs/3183154, it is the key!!

For the demo, please refer to: https://github.com/Bugazelle/nightwatch-capture-network-traffic

Hope could be the help to all of you guys.

Cheers

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants