Skip to content
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

Screenshot function causes undefined httpResponse #940

Closed
dnyg opened this issue Jan 1, 2017 · 4 comments
Closed

Screenshot function causes undefined httpResponse #940

dnyg opened this issue Jan 1, 2017 · 4 comments

Comments

@dnyg
Copy link

dnyg commented Jan 1, 2017

The following code works flawlessly without screenshot(), but for some reason the httpResponse is undefined when the screenshot() function is present.

var Nightmare = require('nightmare');

Nightmare().goto("https://google.com")
.screenshot("abc.png")
.end().then(function(httpResponse){ 
  console.log(httpResponse.code); 
})

Nightmare version is 2.9.0

Node version 6.9.1

@TheTechy
Copy link

TheTechy commented Jan 1, 2017

Thanks to Daniel for raising the bug.
I originally raised a question on StackOverflow which he answered by emitting the screenshot method.

I have updated node to 7.3.0 just in case and the issue remains. I have also removed and re0installed nightmare a couple of times.

The screenshot is rendered and then nightmare seem to stall

I have run a debug and the output is below:

Here's the code:

`var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })

Nightmare().goto("https://google.com")
.screenshot("abc.png")
.end().then(function(httpResponse){
console.log(httpResponse.code);
})
.catch(function(error){
console.log('ERROR: ' + error)
})
`

DEBUG=* node nightmaretest.js nightmare queuing process start +0ms nightmare queuing process start +3ms nightmare queueing action "goto" for https://google.com +2ms nightmare queueing action "screenshot" +0ms nightmare running +0ms nightmare:log Navigating: "https://google.com", headers: [none], timeout: 30000 +439ms nightmare:log did-start-loading +59ms [{}] nightmare:log did-get-redirect-request +301ms [{},"https://google.com/","https://www.google.co.uk/?gfe_rd=cr&ei=ZglpWPnWN6P38AehmLPoCQ",true,302,"GET","",{"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["private"],"content-length":["262"],"content-type":["text/html; charset=UTF-8"],"date":["Sun, 01 Jan 2017 13:51:34 GMT"],"location":["https://www.google.co.uk/?gfe_rd=cr&ei=ZglpWPnWN6P38AehmLPoCQ"],"status":["302"]}] nightmare:log did-get-response-details +161ms [{},false,"https://www.google.co.uk/?gfe_rd=cr&ei=ZglpWPnWN6P38AehmLPoCQ","https://google.com/",200,"GET","",{"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["private, max-age=0"],"content-encoding":["gzip"],"content-type":["text/html; charset=UTF-8"],"date":["Sun, 01 Jan 2017 13:51:35 GMT"],"expires":["-1"],"p3p":["CP=\"This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info.\""],"server":["gws"],"set-cookie":["NID=93=jh20qDlSaE_B2kQ68P5xXm4MGswjyFWb7_HRJivnfvIcWA4MLk1cdEF2zpALsTrHmQLimdih2TFhtqIDA4NGMzn9kBZTSR33sJYMjsKs_GRLy0t8SPOeJfzZ9DwM_p24; expires=Mon, 03-Jul-2017 13:51:35 GMT; path=/; domain=.google.co.uk; HttpOnly","CONSENT=WP.25b8c3; expires=Fri, 01-Jan-2038 00:00:00 GMT; path=/; domain=.google.co.uk"],"status":["200"],"x-frame-options":["SAMEORIGIN"],"x-xss-protection":["1; mode=block"]},"mainFrame"] nightmare:log did-get-response-details +128ms [{},false,"https://www.google.co.uk/logos/doodles/2017/new-years-day-2017-5644902961512448-res.png","https://www.google.co.uk/logos/doodles/2017/new-years-day-2017-5644902961512448-res.png",200,"GET","https://www.google.co.uk/",{"age":["49895"],"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["public, max-age=31536000"],"content-length":["2982"],"content-type":["image/png"],"date":["Sun, 01 Jan 2017 00:00:00 GMT"],"expires":["Mon, 01 Jan 2018 00:00:00 GMT"],"last-modified":["Thu, 22 Dec 2016 16:09:59 GMT"],"server":["sffe"],"status":["200"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log did-get-response-details +0ms [{},false,"https://www.google.co.uk/logos/doodles/2017/new-years-day-2017-5644902961512448-hp.gif","https://www.google.co.uk/logos/doodles/2017/new-years-day-2017-5644902961512448-hp.gif",200,"GET","https://www.google.co.uk/",{"age":["49895"],"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["public, max-age=31536000"],"content-length":["292678"],"content-type":["image/gif"],"date":["Sun, 01 Jan 2017 00:00:00 GMT"],"expires":["Mon, 01 Jan 2018 00:00:00 GMT"],"last-modified":["Thu, 22 Dec 2016 16:08:55 GMT"],"server":["sffe"],"status":["200"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log did-get-response-details +19ms [{},false,"https://consent.google.com/status?continue=https://www.google.co.uk&pc=s&timestamp=1483278695","https://consent.google.com/status?continue=https://www.google.co.uk&pc=s&timestamp=1483278695",204,"GET","https://www.google.co.uk/",{"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["no-cache, no-store, max-age=0, must-revalidate"],"content-length":["0"],"content-security-policy":["script-src 'nonce-OP99O3k3cs17E3WnibN/cBUKQKE' 'self' 'unsafe-eval' https://apis.google.com https://ssl.gstatic.com https://www.google.com https://www.gstatic.com;report-uri /_/ConsentHttp/cspreport"],"content-type":["text/html; charset=utf-8"],"date":["Sun, 01 Jan 2017 13:51:35 GMT"],"expires":["Mon, 01 Jan 1990 00:00:00 GMT"],"p3p":["CP=\"This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info.\""],"pragma":["no-cache"],"server":["ESF"],"set-cookie":["NID=93=twdvPiiYKzn4X3eXW3OAsSJLMyFYRCwGGc-_j7Qcxg0uY4msqPGNSspiMY8MC7fgzwpCo8swub1F0tICMzJe1gaqkrjNxGTfaAY44p9P0P9-uUNGLAQeCx88yrZYPc50;Domain=.google.com;Path=/;Expires=Mon, 03-Jul-2017 13:51:35 GMT;HttpOnly"],"status":["204"],"x-content-type-options":["nosniff"],"x-frame-options":["SAMEORIGIN"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log did-get-response-details +58ms [{},false,"https://consent.google.com/status?continue=https://www.google.co.uk&pc=s&timestamp=1483278695","https://consent.google.com/status?continue=https://www.google.co.uk&pc=s&timestamp=1483278695",204,"GET","https://www.google.co.uk/",{"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["no-cache, no-store, max-age=0, must-revalidate"],"content-length":["0"],"content-security-policy":["script-src 'nonce-tKCihCyNfIOByWphPLBRJTh3d4o' 'self' 'unsafe-eval' https://apis.google.com https://ssl.gstatic.com https://www.google.com https://www.gstatic.com;report-uri /_/ConsentHttp/cspreport"],"content-type":["text/html; charset=utf-8"],"date":["Sun, 01 Jan 2017 13:51:35 GMT"],"expires":["Mon, 01 Jan 1990 00:00:00 GMT"],"pragma":["no-cache"],"server":["ESF"],"status":["204"],"x-content-type-options":["nosniff"],"x-frame-options":["SAMEORIGIN"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log dom-ready +0ms [{}] nightmare:log did-get-response-details +19ms [{},false,"https://ssl.gstatic.com/gb/images/i2_2ec824b0.png","https://ssl.gstatic.com/gb/images/i2_2ec824b0.png",200,"GET","https://www.google.co.uk/",{"age":["52211"],"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["public, max-age=31536000"],"content-length":["24211"],"content-type":["image/png"],"date":["Sat, 31 Dec 2016 23:21:24 GMT"],"expires":["Sun, 31 Dec 2017 23:21:24 GMT"],"last-modified":["Mon, 12 Dec 2016 14:45:00 GMT"],"server":["sffe"],"status":["200"],"vary":["Origin"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log did-get-response-details +5ms [{},false,"https://www.google.co.uk/xjs/_/js/k=xjs.s.en.E-k8HBn4MQ0.O/m=sx,c,sb,cdos,cr,elog,hsm,jsa,r,qsm,j,p,d,csi/am=AARKHi9YAPH3EBC3iY4gNWBgUA/rt=j/d=1/t=zcms/rs=ACT90oG2tg8KG9nbmKr7z7wuMQcXuZD4Nw","https://www.google.co.uk/xjs/_/js/k=xjs.s.en.E-k8HBn4MQ0.O/m=sx,c,sb,cdos,cr,elog,hsm,jsa,r,qsm,j,p,d,csi/am=AARKHi9YAPH3EBC3iY4gNWBgUA/rt=j/d=1/t=zcms/rs=ACT90oG2tg8KG9nbmKr7z7wuMQcXuZD4Nw",200,"GET","https://www.google.co.uk/",{"age":["247456"],"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["public, max-age=31536000"],"content-encoding":["gzip"],"content-length":["143255"],"content-type":["text/javascript; charset=UTF-8"],"date":["Thu, 29 Dec 2016 17:07:19 GMT"],"expires":["Fri, 29 Dec 2017 17:07:19 GMT"],"last-modified":["Wed, 28 Dec 2016 13:03:14 GMT"],"server":["sffe"],"status":["200"],"vary":["Accept-Encoding"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"script"] nightmare:log did-get-response-details +203ms [{},false,"https://www.google.co.uk/xjs/_/js/k=xjs.s.en.E-k8HBn4MQ0.O/m=sy42,sy53,em3,em1,em2,sy55,em0,sy328,aa,abd,sy82,sy81,sy80,sy83,em16,async,erh,sy85,foot,fpe,ipv6,sy152,sy185,lu,m,sf,sy49,sy131,sy178,sy271,sy226,sy286,sy225,sy283,sy44,sy179,sy270,sy287,sy290,sy284,sy281,spch,vm,sy341,sy343,sy60,sy342,sy344,sy345,sy544,udlg,sy43,sy105,sy46,sy51,sy104,sy162,sy45,sy47,sy164,em8,em9,sy41,sy52,sy106,sy161,sy163,sy165,cbin,sy400,dgm,cbhb/am=AARKHi9YAPH3EBC3iY4gNWBgUA/rt=j/d=0/t=zcms/rs=ACT90oG2tg8KG9nbmKr7z7wuMQcXuZD4Nw","https://www.google.co.uk/xjs/_/js/k=xjs.s.en.E-k8HBn4MQ0.O/m=sy42,sy53,em3,em1,em2,sy55,em0,sy328,aa,abd,sy82,sy81,sy80,sy83,em16,async,erh,sy85,foot,fpe,ipv6,sy152,sy185,lu,m,sf,sy49,sy131,sy178,sy271,sy226,sy286,sy225,sy283,sy44,sy179,sy270,sy287,sy290,sy284,sy281,spch,vm,sy341,sy343,sy60,sy342,sy344,sy345,sy544,udlg,sy43,sy105,sy46,sy51,sy104,sy162,sy45,sy47,sy164,em8,em9,sy41,sy52,sy106,sy161,sy163,sy165,cbin,sy400,dgm,cbhb/am=AARKHi9YAPH3EBC3iY4gNWBgUA/rt=j/d=0/t=zcms/rs=ACT90oG2tg8KG9nbmKr7z7wuMQcXuZD4Nw",200,"GET","https://www.google.co.uk/",{"age":["69113"],"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["public, max-age=31536000"],"content-encoding":["gzip"],"content-length":["39347"],"content-type":["text/javascript; charset=UTF-8"],"date":["Sat, 31 Dec 2016 18:39:42 GMT"],"expires":["Sun, 31 Dec 2017 18:39:42 GMT"],"last-modified":["Sat, 24 Dec 2016 22:01:56 GMT"],"server":["sffe"],"status":["200"],"vary":["Accept-Encoding"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"script"] nightmare:log did-get-response-details +46ms [{},false,"https://www.gstatic.com/og/_/js/k=og.og2.en_US.oDYjzNdSXG0.O/rt=j/m=def/exm=in,fot/d=1/ed=1/rs=AA2YrTt3OYTznFuWnDoc_JnRaLQNCghyMg","https://www.gstatic.com/og/_/js/k=og.og2.en_US.oDYjzNdSXG0.O/rt=j/m=def/exm=in,fot/d=1/ed=1/rs=AA2YrTt3OYTznFuWnDoc_JnRaLQNCghyMg",200,"GET","https://www.google.co.uk/",{"age":["395248"],"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["public, max-age=31536000"],"content-encoding":["gzip"],"content-length":["47789"],"content-type":["text/javascript; charset=UTF-8"],"date":["Wed, 28 Dec 2016 00:04:07 GMT"],"expires":["Thu, 28 Dec 2017 00:04:07 GMT"],"last-modified":["Mon, 12 Dec 2016 03:57:39 GMT"],"server":["sffe"],"status":["200"],"vary":["Accept-Encoding, Origin"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"script"] nightmare:log did-get-response-details +10ms [{},false,"https://www.google.com/textinputassistant/tia.png","https://www.google.com/textinputassistant/tia.png",200,"GET","https://www.google.co.uk/",{"age":["250477"],"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["public, max-age=31536000"],"content-length":["258"],"content-type":["image/png"],"date":["Thu, 29 Dec 2016 16:16:58 GMT"],"expires":["Fri, 29 Dec 2017 16:16:58 GMT"],"last-modified":["Thu, 08 Dec 2016 15:30:00 GMT"],"server":["sffe"],"status":["200"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log did-get-response-details +4s [{},false,"https://www.google.co.uk/gen_204?atyp=i&ct=&cad=&vet=10ahUKEwjPw-Ddi6HRAhXCMhoKHcQyDoYQsmQIEA..s&ei=ZwlpWI90wuVoxOW4sAg&zx=1483278695561","https://www.google.co.uk/gen_204?atyp=i&ct=&cad=&vet=10ahUKEwjPw-Ddi6HRAhXCMhoKHcQyDoYQsmQIEA..s&ei=ZwlpWI90wuVoxOW4sAg&zx=1483278695561",204,"GET","https://www.google.co.uk/",{"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"content-length":["0"],"content-type":["text/html; charset=UTF-8"],"date":["Sun, 01 Jan 2017 13:51:35 GMT"],"server":["gws"],"status":["204"],"x-frame-options":["SAMEORIGIN"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log did-get-response-details +93ms [{},false,"https://apis.google.com/_/scs/abc-static/_/js/k=gapi.gapi.en.FgPLF5SwqIU.O/m=gapi_iframes,googleapis_client,plusone/rt=j/sv=1/d=1/ed=1/rs=AHpOoo-9R8fkhlRsCMrG4wpDzgf1RI7BzQ/cb=gapi.loaded_0","https://apis.google.com/_/scs/abc-static/_/js/k=gapi.gapi.en.FgPLF5SwqIU.O/m=gapi_iframes,googleapis_client,plusone/rt=j/sv=1/d=1/ed=1/rs=AHpOoo-9R8fkhlRsCMrG4wpDzgf1RI7BzQ/cb=gapi.loaded_0",200,"GET","https://www.google.co.uk/",{"age":["504093"],"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["public, max-age=31536000"],"content-encoding":["gzip"],"content-length":["43225"],"content-type":["text/javascript; charset=UTF-8"],"date":["Mon, 26 Dec 2016 17:50:07 GMT"],"expires":["Tue, 26 Dec 2017 17:50:07 GMT"],"last-modified":["Fri, 02 Dec 2016 02:44:02 GMT"],"server":["sffe"],"status":["200"],"vary":["Accept-Encoding"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"script"] nightmare:log did-frame-finish-load +72ms [{},true] nightmare:log did-finish-load +0ms [{}] nightmare:log page-favicon-updated +1ms [{},["https://www.google.co.uk/images/branding/product/ico/googleg_lodp.ico"]] nightmare:log did-stop-loading +1ms [{}] nightmare:actions .screenshot() +1ms nightmare:log subscribing to browser window frames +1ms nightmare:log altering page to force rendering +0ms nightmare:log did-get-response-details +58ms [{},false,"https://www.google.co.uk/images/nav_logo242.png","https://www.google.co.uk/images/nav_logo242.png",200,"GET","https://www.google.co.uk/",{"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"cache-control":["private, max-age=31536000"],"content-length":["16786"],"content-type":["image/png"],"date":["Sun, 01 Jan 2017 13:51:40 GMT"],"expires":["Sun, 01 Jan 2017 13:51:40 GMT"],"last-modified":["Wed, 14 Dec 2016 20:30:00 GMT"],"server":["sffe"],"status":["200"],"x-content-type-options":["nosniff"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log did-get-response-details +0ms [{},false,"https://www.google.co.uk/gen_204?v=3&s=webhp&atyp=csi&ei=ZwlpWI90wuVoxOW4sAg&imc=4&imn=4&imp=2&adh=&xjs=init.47.21.sb.30.spch.6.p.4.foot.3.jsa.1&ima=2&rt=xjsls.51,prt.90,iml.105,dcl.93,xjses.219,jraids.249,jraide.254,xjsee.301,xjs.301,ol.4937,aft.104,wsrt.622,cst.57,dnst.23,rqst.126,rspt.49,sslt.34,rqstt.442,unt.362,cstt.385,dit.712","https://www.google.co.uk/gen_204?v=3&s=webhp&atyp=csi&ei=ZwlpWI90wuVoxOW4sAg&imc=4&imn=4&imp=2&adh=&xjs=init.47.21.sb.30.spch.6.p.4.foot.3.jsa.1&ima=2&rt=xjsls.51,prt.90,iml.105,dcl.93,xjses.219,jraids.249,jraide.254,xjsee.301,xjs.301,ol.4937,aft.104,wsrt.622,cst.57,dnst.23,rqst.126,rspt.49,sslt.34,rqstt.442,unt.362,cstt.385,dit.712",204,"GET","https://www.google.co.uk/",{"alt-svc":["quic=\":443\"; ma=2592000; v=\"35,34\""],"content-length":["0"],"content-type":["text/html; charset=UTF-8"],"date":["Sun, 01 Jan 2017 13:51:40 GMT"],"server":["gws"],"status":["204"],"x-frame-options":["SAMEORIGIN"],"x-xss-protection":["1; mode=block"]},"image"] nightmare:log unsubscribing from browser window frames +34ms nightmare:actions .screenshot() captured with length 361068 +467ms nightmare electron child process exited with code 0: success! +51ms ERROR: TypeError: Cannot read property 'code' of undefined nightmare electron child process not started yet, skipping kill. +1ms

@rosshinkley
Copy link
Contributor

@dnyg httpResponse is not defined because the parameters passed to .then() will be from the last action executed in the chain (with the exception of .end()) - in your case, .screenshot() (which doesn't return anything). If you need the HTTP response, you can break up your chain with another .then() to perform logic. Something like:

var Nightmare = require('nightmare');
var nightmare = Nightmare();
nightmare.goto("https://google.com")
.then(function(httpResponse) {
  if(httpResponse.code == 200) {
    return nightmare.screenshot('abc.png');
  } 
  else {
    //error condition?
    throw new Error('http response was not ok');
  }
})
.then(function(){ return nightmare.end(); })
.catch(function(){ return nightmare.end(); })
.then(function(){ 
  console.log('done');
})

@TheTechy your code suffers from the same issue - the additional problem here is you're trying to access code off of httpResponse which will cause an exception. From your trace: ERROR: TypeError: Cannot read property 'code' of undefined

@TheTechy
Copy link

TheTechy commented Jan 2, 2017

Hey Ross,

Thanks for your answer. The code posted by Daniel originally was mine :)
The update here works a treat and I get the then-able nature of Nightmare now.

Thanks again for your help & please close the issue.

@rosshinkley
Copy link
Contributor

@TheTechy Ah, cool. :) No problem, happy to help.

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

No branches or pull requests

3 participants