Monday, 18 March 2013

Headless Browser Testing using PhantomJS - GhostDriver | WebDriver


PhantomJS is a Headless Webkit with JavaScript API. It has fast & native support for various Web Standards: DOM handling, CSS selector, JSON, canvas and SVG.  GhostDriver is a Webdriver wire protocol in simple JS for PhantomJS.  PhantomJS is used for Headless Testing of Web Applications that comes with in-built GhostDriver.

Involves,
1| General command-line based testing.
2| As a part of a Continuous Integration System.

PhantomJS is not a Test framework, it is used only to LAUNCH the tests via a suitable Test Runner.

Framework used: WebDriver
Test Runner: GhostDriver

CI systems: Make sure PhantomJS is installed properly on the slave/build agent and it is ready to go. Headless Browser is a Web Browser without a GUI (Graphical User Interface). It access Web Pages but doesn't show them to any human being. Headless Browser should be able to parse JavaScript.



Configure PhantomJS

1. Download phantomjs.exe
2. Extract the phantomjs-1.8.x-windows.zip folder and locate phantomjs.exe file to C:/ folder
3. Add the following imports to your code:

import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

4. Replace the object, "driver" specifying "FirefoxDriver" with "PhantomJSDriver".

Replace the code,
WebDriver driver = new FirefoxDriver

with
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);

5. Run Test.

Note|
PhantomJSDriver-1.0.x.jar can also be downloaded and configured in Eclipse manually.



PhantomJS | Screen Capture

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://phantomjs.exe");
caps.setCapability("takesScreenshot", true);
driver = new PhantomJSDriver(caps);  
baseUrl = "http://www.xyz.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void test01() throws Exception {
driver.get(baseUrl + "/");   
long iStart = System.currentTimeMillis(); // start timing
<your script>
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\sample.jpeg"),true);    
System.out.println("Single Page Time:" + (System.currentTimeMillis() - iStart)); // end timing    
}

68 comments:

  1. Hey Prashanth,

    I've been looking for this solution everywhere. Thank you so much. Since it's designed to hide all of the actions from the user, is there anyway to make sure this works besides using print statements in the code? Again, thank you.

    ReplyDelete
    Replies
    1. I guess PhantomJS supports screenshots as well.

      Delete
    2. Hi Max,

      Phantomjs supports Screen capture as well. Please find the example I have given above.

      Cheers
      _
      Prashanth Sams

      Delete
  2. Thanks a lot for great tutorial. Highly appreciated.

    ReplyDelete
  3. Can you give me a real time example where we use this in Automation? Like some example application

    ReplyDelete
  4. Thanks a lot .... best tut about phantom on the net ..

    ReplyDelete
  5. Nice article, prashanth!!
    Althought I'm new to selenium and phantoms.. i'm trying something with nodejs , but not getting what i expect..

    can you please help me on this..

    http://stackoverflow.com/q/24556738/2958224

    Much appreciated..!!

    ReplyDelete
  6. Hi Prashanth,

    This is very nice and helpful article.

    Now, can you please tell me, how can i enable cookie using selenium and phantoms?

    ReplyDelete
  7. here's how i do cookies in my GebConfig.groovy....

    DesiredCapabilities dCaps = new DesiredCapabilities()
    dCaps.setJavascriptEnabled(true);
    dCaps.setCapability("takesScreenshot", false);
    dCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
    '/Users/en032339/Desktop/phantomjs-1.9.7-windows/phantomjs.exe')
    dCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
    ['--cookies-file=phantomjs_cookies.txt', '--load-images=no'] as String[])
    WebDriver driver = new PhantomJSDriver(dCaps)

    ReplyDelete
  8. HI can you please tell me how to handle download s in headless testing.
    eg: we have a link to download excel file .In selenium we are using autoit script to handle the download windows but here i have no guess can anyone please help me on this

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. How to use PhantomJS with maven?

    ReplyDelete
  11. Thanks for the great information in your blog Selenium Training in Chennai

    ReplyDelete
  12. thank you so much..
    very useful documnet..
    so many thanks

    ReplyDelete
  13. very good Article. Is there such kind of write-up for PhantomJS, Selenium on Linux OS?

    ReplyDelete
  14. can we use phantomjs in grid, please help me with that

    ReplyDelete
  15. Hello,

    I found this site just now. Your code has been a good help for me but there are two things:

    1. FileUtils.copyFile doesn't work for me. I saw this on several sites but I have to use FileCopyUtils.copyFile() otherwise I get an compilation error that this method does not exist.

    2. Open a site and make a screenshot isn't enough for me. For a test I have to click buttons, insert values in text fields,..
    But whatever I do I always get a NoSuchElementException. I tried

    - driver.findElementBy.id/xpath/tagName/cssSelector
    - actions.moveToElement(driver.findElementBy.id/xpath/tagName/cssSelector)
    - both with 30 seconds timeout

    I'm sick of it. Maybe you have an idea how to find a webelement with PhantomJSDriver?

    Thanks and a nice day
    Achim

    ReplyDelete
  16. I really enjoy the blog.Much thanks again. Really Great selenium Online course Bangalore

    ReplyDelete
  17. It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
    It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.

    ReplyDelete
  18. All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  19. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  20. Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!
    Data Science course in kalyan nagar
    Data Science course in OMR
    Data Science course in chennai
    Data science course in velachery
    Data science course in jaya nagar
    Data Science interview questions and answers
    Data science course in bangalore

    ReplyDelete
  21. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  22. I have express a few of the articles on your website now, and I really like your style of Python training in pune blogging. I added it to my favorite’s blog site list and will be checking back soon…

    ReplyDelete
  23. Attend The Analytics Courses in Bangalore From ExcelR. Practical Analytics Courses in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Analytics Courses in Bangalore.
    ExcelR Analytics Courses in Bangalore

    ReplyDelete
  24. Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.python training in bangalore

    ReplyDelete
  25. Its really helpful for the users of this site. I am also searching about these type of sites now a days. So your site really helps me for searching the new and great stuff.vmware training in bangalore

    ReplyDelete
  26. Very useful and information content has been shared out here, Thanks for sharing it.aws training in bangalore

    ReplyDelete
  27. I gathered a lot of information through this article.Every example is easy to undestandable and explaining the logic easily.mulesoft training in bangalore

    ReplyDelete
  28. These provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post.salesforce developer training in bangalore

    ReplyDelete
  29. Your articles really impressed for me,because of all information so nice.salesforce admin training in bangalore

    ReplyDelete
  30. Linking is very useful thing.you have really helped lots of people who visit blog and provide them use full information.devops training in bangalore

    ReplyDelete
  31. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving.servicenow training in bangalore

    ReplyDelete
  32. Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.informatica training in bangalore

    ReplyDelete
  33. I know that it takes a lot of effort and hard work to write such an informative content like this.cloud computing training in bangalore

    ReplyDelete
  34. Such a great word which you use in your article and article is amazing knowledge. thank you for sharing it.

    Looking for Training Institute in Bangalore , India. Softgen Infotech is the best one to offers 85+ computer training courses including IT software course in Bangalore, India. Also it provides placement assistance service in Bangalore for IT.
    Best Software Training Institute in Bangalore

    ReplyDelete
  35. Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...

    Best Training Institute in Bangalore BTM. My Class Training Bangalore training center for certified course, learning on Software Training Course by expert faculties, also provides job placement for fresher, experience job seekers.
    Training Institute in Bangalore

    ReplyDelete
  36. Post is very useful. Thank you, this useful information.

    Start your journey with Training Institute in Bangaloreand get hands-on Experience with 100% Placement assistance from Expert Trainers with 8+ Years of experience @eTechno Soft Solutions Located in BTM Layout Bangalore.
    SAP Training in Bangalore

    ReplyDelete
  37. Wow its a very good post. The information provided by you is really very good and helpful for me. Keep sharing good information.

    Best Training Institute in Bangalore BTM. My Class Training Bangalore training center for certified course, learning on Software Training Course by expert faculties, also provides job placement for fresher, experience job seekers.
    Software Training Institute in Bangalore

    ReplyDelete
  38. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.

    ReplyDelete
  39. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.sap hana training in bangalore

    ReplyDelete
  40. Awesome,Thank you so much for sharing such an awesome blog. sap hr training in bangalore

    ReplyDelete
  41. Thanks for sharing this blog. This very important and informative blog.sap mm training in bangalore

    ReplyDelete
  42. Very interesting, good job and thanks for sharing such a good blog. Thanks a lot…

    Learn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Softgen Infotech provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
    Big Data Analytics Training in Bangalore
    Tableau Training in Bangalore
    Data Science Training in Bangalore
    Workday Training in Bangalore

    ReplyDelete
  43. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    servicenow online training
    top servicenow online training
    best servicenow online training

    ReplyDelete
  44. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

    ReplyDelete
  45. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    Data Science Training in Electronic City

    ReplyDelete
  46. Great blog created by you. I read your blog, its best and useful information. Super blogging and keep it updating
    Hadoop Training in Hyderabad

    ReplyDelete
  47. This is the information that ive been looking for. Great insights & you have explained it really well. Thank you & looking forward for more of such valuable updates.
    Artificial Intelligence Training In Hyderabad

    Artificial Intelligence Course In Hyderabad

    ReplyDelete
  48. Thanks you for sharing this informative and useful article.Really interesting and awesome article.
    Data Science Training in Hyderabad

    ReplyDelete
  49. Wow!! Really a nice Article about Selenium. Thank you so much for your efforts. Definitely, it will be helpful for others. I would like to follow your blog. Share more like this. Thanks Again.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  50. I had to read this three times because I wanted to be sure on some of your points. I agree on almost everything here, and I am impressed with how well you wrote this article.
    Best Data Science training in Mumbai

    Data Science training in Mumbai

    ReplyDelete
  51. thanks for sharing nice information. its Very use full and informative and keep sharing.
    more : https://www.analyticspath.com/artificial-intelligence-training-in-hyderabad

    ReplyDelete
  52. Get real time project based and job oriented Salesforce training India course materials for Salesforce Certification with securing a practice org, database terminology, admin and user interface navigation and custom fields creation, reports & analytics, security, customization, automation and web to lead forms.  

    ReplyDelete
  53. thanks for sharing nice information.....
    more : https://www.kellytechno.com/Hyderabad/Course/AI-Training-In-Hyderabad

    ReplyDelete
  54. Such an excellent and interesting blog, do post like this more with more information, this was very useful. Salesforce Training Canada   

    ReplyDelete
  55. I am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog. Have a look at my services.  
    This is really the best Top 20 Salesforce CRM Admin Development Interview Questions highly helpful. I have found these Scenario based Salesforce developers interview questions and answers very helpful to attempt job interviews. Wow, i got this scenario based Salesforce interview questions highly helpful.

    ReplyDelete