December 15, 2010

Write results to excel sheet

After successfully executing scripts, every one want to write results to excel sheet..here is the way to write results to excel sheet....

Below is the sample script to write results to excel sheet...


package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;

public class Importexport1 {
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testImportexport1() throws Exception {

// Read data from excel sheet
FileInputStream fi = new FileInputStream(
"F:\\Framework\\testdata\\Login1_Credentials.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
// Write the input data into another excel file
FileOutputStream fo = new FileOutputStream(
"F:\\Framework\\Results\\LoginResult1.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);
WritableSheet ws = wwb.createSheet("loginresult1", 0);

selenium.open("http://www.gmail.com");
selenium.windowMaximize();

System.out.println("s.getRows() = " + s.getRows());

for (int i = 0; i < s.getRows(); i++) {
System.out.println("s.getColumns = " + s.getColumns());
for (int j = 0; j < s.getColumns(); j++) {
a[i][j] = s.getCell(j, i).getContents();
Label l = new Label(j, i, a[i][j]);
Label l1 = new Label(2, 0, "Result");
ws.addCell(l);
ws.addCell(l1);
}
}

for (int i = 1; i < s.getRows(); i++) {
selenium.type("Email", s.getCell(0, i).getContents());
selenium.type("Passwd", s.getCell(1, i).getContents());
selenium.click("signIn");
selenium.waitForPageToLoad("30000");

boolean aa = selenium.isTextPresent("The username or password you entered is incorrect. [?]");
System.out.println("the value of aa is::" + aa);
if (aa)

{

Label l3 = new Label(2, i, "fail");

ws.addCell(l3);
System.out.println("Login Failure");
Thread.sleep(10000);

} else {

Label l2 = new Label(2, i, "pass");

ws.addCell(l2);
selenium.click("link=Sign out");
Thread.sleep(10000);
}
}
wwb.write();
wwb.close();
}

@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();

}
}




Your input data should be like this....















Your out put excel should be like this











Hope this post is helpful....

60 comments:

  1. Thanks this was helpful
    PS: if the code is with proper comments it will be of more help.

    ReplyDelete
  2. Thanks this code is very useful.

    ReplyDelete
  3. Thanks....It is helpful for my data driven approach

    ReplyDelete
  4. Hi Naga,

    I am trying to perform a similar thing & have been running into issues..

    My input to the script: “N” number of urls through an excel sheet.

    Desired output: I need to get some string from page source info for all those urls & write the same into Excel.

    Actual output: I am getting data written in excel sheet for the last test/url I have run… For instance if we have 100urls in the excel, in my output sheet excel, I see results from 100th url. Not the 1 to 99 urls…


    My problem: when I run this test for 100 different urls. I get the results for the last url I had run the test for… How do I grab the values for other 99 urls & how do I store it in the excel sheet.

    ReplyDelete
    Replies
    1. did you find the solution for your problem ?

      Delete
    2. Hi any solution to this problem

      Delete
  5. wwb.write();
    wwb.close();
    keep these lines at the end of the loop

    ReplyDelete
  6. I am trying to run scripts through Selenium IDE-HTML commands. Is there any way to extract the results to excel with HTML commands?

    ReplyDelete
  7. superb...it helped me lot...

    can u pls let me know...how can i write the results also in same excel where we getting inputs.

    ReplyDelete
  8. im using Web driver how can i use that code?
    thanks

    ReplyDelete
  9. Hi All,
    I'm using selenium IDE to record & replay some testcase.
    During this, i stored some values in variables.Now i've to load/write these variable's value into new file.How can i do it ? is it possible ?

    ReplyDelete
  10. This copy of Office is not genuine :P
    Thanks for the code anyways \m/

    ReplyDelete
  11. Above is find with one test but if we have multiple tests in single junit then how to handle?

    ReplyDelete
  12. It is appropriate time to make a few plans for the longer term and it is time to be happy.
    I've read this publish and if I may just I wish to recommend you few fascinating things or advice. Maybe you can write next articles relating to this article. I desire to read even more issues about it! chcesz wiedzieć więcej - kliknij, https://www.barmen.nu/mediawiki/index.php/Bruker:JulietNho

    ReplyDelete
  13. Great blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would really make my blog stand out.
    Please let me know where you got your theme. With thanks

    Take a look at my web page: Crack Passwords like a Boss With GPU-Based Cluster

    ReplyDelete
  14. When getting a discount rowing machine, you desire to look at it out thoroughly.


    Feel free to visit my weblog dumbbells for sale

    ReplyDelete
  15. Good day very nice web site!! Guy .. Excellent .. Superb .
    . I will bookmark your blog and take the feeds additionally�I'm happy to find a lot of useful information here in the publish, we'd like work out more techniques on this regard, thank you for sharing.


    my weblog ... premium minecraft

    ReplyDelete
  16. Will you be pondering if it is best to use a Bowflex as portion
    of your house gymnasium gear?

    Visit my homepage; please click the next site

    ReplyDelete
  17. Informative article, exactly what I needed.

    Here is my web page ... earn while earn unemployed

    ReplyDelete
  18. The folded footprint is fifty-two inches lengthy by thirty-eight inches broad.


    Feel free to surf to my web page :: Check This Out

    ReplyDelete
  19. These sites will assist you to decide which treadmill or another exercise devices
    you want to get.

    Have a look at my web blog: dumbbell sets

    ReplyDelete
  20. Link exchange is nothing else but it is only placing the other person's weblog link on your page at proper place and other person will also do same for you.

    My site - dragon on dragonvale

    ReplyDelete
  21. Do not think that you just want a gym membership to receive into shape.


    my website: http://www.getfitnstrong.com/bowflex-dumbbells/bowflex-selecttech-552-adjustable-dumbbells/

    ReplyDelete
  22. The process is created of a tough polymer and integrates the most up-to-date personal computer application, to deliver an incredibly eye-catching
    and helpful bit of work out equipment.

    my web page Read Full Report

    ReplyDelete
  23. You are able to conduct much more than 140 wellbeing
    club superior workouts.

    Check out my webpage: Suggested Webpage

    ReplyDelete
  24. I have found that the lack of financial worry certainly sales opportunities to success.


    my web site :: Personal Site

    ReplyDelete
  25. I do agree with all of the concepts you have introduced for your
    post. They are really convincing and can certainly
    work. Nonetheless, the posts are too quick for beginners.
    May you please lengthen them a bit from next time? Thanks for the post.


    Here is my site; psn cards

    ReplyDelete
  26. I have been surfing on-line greater than 3 hours lately, but
    I by no means found any fascinating article like yours. It's beautiful worth sufficient for me. In my opinion, if all site owners and bloggers made good content material as you probably did, the net will likely be a lot more useful than ever before. "Perfection of moral virtue does not wholly take away the passions, but regulates them." by Saint Thomas Aquinas.

    My webpage ... PSN Code Generator

    ReplyDelete
  27. Arguably the three most acknowledged home gyms are
    classified as the Bodylastics resistance bands procedure,
    and the Bowflex as well as the Overall Gym cable
    pulley units.

    Here is my website - http://www.getfitnstrong.com/bowflex-dumbbells/bowflex-selecttech-dumbbells-ultimate-home-exercising/

    ReplyDelete
  28. Thanks for finally writing about > "Write results to excel sheet" < Liked it!

    Feel free to surf to my page virtapay

    ReplyDelete
  29. This paragraph provides clear idea for the new viewers of blogging, that
    in fact how to do blogging and site-building.

    Here is my website - virta pay

    ReplyDelete
  30. Simply desire to say your article is as astonishing.

    The clearness in your post is just great and i could assume
    you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up to date
    with forthcoming post. Thanks a million and please keep up
    the gratifying work.

    My blog: Minecraft Beta

    ReplyDelete
  31. I visited several websites however the audio quality for audio songs current at this website is genuinely excellent.


    Take a look at my page ... hack twitter account

    ReplyDelete
  32. It truly is the ideal way for professional runners to take care
    of and increase their physical issue.

    My web-site ... adjustable weights

    ReplyDelete
  33. This is really interesting, You are a very skilled blogger.
    I've joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your website in my social networks!

    Here is my site ... virtapay exchange

    ReplyDelete
  34. Hi i am kavin, its my first time to commenting anywhere, when i read
    this paragraph i thought i could also create comment due to this sensible post.


    Feel free to visit my web-site - Hot young girls

    ReplyDelete
  35. I am regular visitor, how are you everybody? This post posted at this site is in
    fact fastidious.

    my page :: free sex games

    ReplyDelete
  36. hey there and thank you for your info – I have certainly picked up anything new
    from right here. I did however expertise several technical points using this site, as I
    experienced to reload the web site many times previous to I could get it to load properly.
    I had been wondering if your hosting is OK? Not that I am complaining, but sluggish loading instances times will
    sometimes affect your placement in google and can damage your quality
    score if advertising and marketing with Adwords. Well I'm adding this RSS to my email and can look out for much more of your respective intriguing content. Make sure you update this again very soon.

    Check out my blog: download miscrits

    ReplyDelete
  37. Somebody necessarily lend a hand to make significantly posts I'd state. This is the very first time I frequented your website page and to this point? I surprised with the research you made to create this particular post extraordinary. Wonderful job!

    Check out my webpage: pc sex games

    ReplyDelete
  38. These foodstuff groups also are prosperous in fiber that can help your belly digest the foodstuff within your intestines faster.



    Here is my web-site: simply click the following page

    ReplyDelete
  39. Many well being and conditioning experts concur that manual machines are essentially superior for taking off more lbs all-around your hips
    than motorized equipment.

    Also visit my site; http://www.getfitnstrong.com

    ReplyDelete
  40. Using weights is the way you are likely to make muscle.



    Check out my page; View Results

    ReplyDelete
  41. Nonetheless, every person would not need to have the high power
    for their workout.

    Also visit my webpage just click the up coming article

    ReplyDelete
  42. You should take part in a contest for one of the finest websites on the internet.
    I'm going to highly recommend this site!

    my site - virtapay to lr

    ReplyDelete
  43. The biggest advantage of dumbbell shrugs in excess of most other shrug exercise routines
    is definitely the arm positioning.

    Feel free to surf to my web page ... bowflex selecttech 552 dumbbells sale

    ReplyDelete
  44. The strength rod technology with the Bowflex Activity residence gymnasium is really preferred by numerous exercise enthusiasts as opposed to
    free weights or stacks.

    my page :: http://www.getfitnstrong.com/bowflex-dumbbells/bowflex-selecttech-dumbbells-ultimate-home-exercising/

    ReplyDelete
  45. This could maintain adequate fuel and energy to elevate the heavy weights and build muscle mass.


    Also visit my web page; Recommended Internet site

    ReplyDelete
  46. Becoming a work-out person, I've constantly tried out several machines, and so forth to stay in shape.

    my weblog: relevant resource site

    ReplyDelete
  47. The solar heating energy venture amalgamating high-performing
    solar-thermal systems with fossil fuels, we only need to build more solar heating energy.
    20 of the food we throw away the opportunity to become stewards of our environment and learn about sustainable energy
    strategies here in Wisconsin. We're starting to see stocks tighten up in certain small markets already, and some final things around those.

    Feel free to visit my web site; Photovoltaic Uk Association

    ReplyDelete
  48. According to RWDSU, gig bag guitar Center Holdings, is
    now carrying a heavy debt load of $1. She even laughed at one of those guys who knows I put in enough time
    learning an instrument and get on stage.

    Here is my page :: www.phantomwithdrawals.com

    ReplyDelete
  49. The exercise sessions you offer you you should not must be really extreme.


    my page - adjustable dumbbells

    ReplyDelete
  50. Thanks to my father who stated to me concerning this
    weblog, this webpage is in fact amazing.

    my weblog How To Hack Twitter

    ReplyDelete
  51. My brother suggested I might like this website. He was totally right.

    This post truly made my day. You cann't imagine just how much time I had spent for this information! Thanks!

    Feel free to surf to my web page :: miscrits hack

    ReplyDelete
  52. Do you have any video of that? I'd care to find out some additional information.

    my web-site - http://julio.Estudiosuma.cl/

    ReplyDelete
  53. Having said that, your products will never retain with
    the exact stage for your extended period of time.

    Feel free to surf to my webpage bowflex 552

    ReplyDelete
  54. Way cool! Some extremely valid points! I appreciate you writing this post plus the rest of the site is really good.



    Also visit my page ... Adfoc.Us

    ReplyDelete
  55. Cool blog! Is your theme custom made or did you download
    it from somewhere? A theme like yours with a few simple
    adjustements would really make my blog jump out. Please let me know where you got your theme.
    Thank you

    Here is my site :: übersetzung online kostenlos ()

    ReplyDelete
  56. PSN Cards 50$ free

    Feel free to visit my blog post: Free PSN Cards

    ReplyDelete