lapwinglabs / x-ray-phantom

phantom driver for x-ray.
112 stars 30 forks source link

add phantom 'evaluate' method #5

Closed bdentino closed 9 years ago

bdentino commented 9 years ago

super useful for debugging

matthewmueller commented 9 years ago

does this overwrite the internal one? i was also thinking about adding .screenshot(path) to map the walking.

bdentino commented 9 years ago

Nope. You can chain multiple evaluates with nightmare. In fact, that allows you hack together some conditional nightmare sequences:

  xray(some_url)
      .ua(user_agent)
      .use(phantom({ cookiesFile: cookies_file }))
      .evaluate(function() {
        if (document.body.innerText.indexOf("Sign up") >= 0) { // We're not logged in, so redirect to login
          window.location.assign('https://someurl.com/login')
          return false;
        }
        return true;
      }, function(authenticated) {
        console.log('logged_in:', authenticated)
      })
      .wait() // wait for login page to load (if we're already logged in this won't do anything)
      .evaluate(function(user, pass) {
        if (document.getElementById('signin')) { //only do this if signin form is present
          var userInput = document.getElementById('email')
          var passInput = document.getElementById('password')
          var signinButton = document.getElementById('signin')
          userInput.value = user
          passInput.value = pass
          signinButton.click()
          return 'signin'
        }
        return 'no signin'
      },function(action){ console.log(action) }, user, password)
      .wait()
      // now we're authenticated...if we already were, this still works

As long as you use all of your evaluates before 'select', you should be fine.