1
@DataProvider(name = "Authentication1")
    public static Object[][] credentials1()
    {

        return new Object[][] { 
                                { "12234", " 6576","gfdgfdg"," dfg","gfd "},
                                {"&^*&^","&*^*&^","soni.rutvigmail.com","ghgfh","ghfghgfh"},
                                {"Rutvi","Soni","rutvi@gmailcom","123456","123456"},
                                {"Rutvi","Soni","rutvi@gmailcom","123456","123456"},
                                {"Rutvi","Soni","rutvi@gmail..com","123456","123456"},
                                {"Rutvi","Soni","soni.rutvi@123.123.123","123456","123456"},
                                {"Rutvi","Soni",".rutvi@gmail.com","123456","123456"},
                                {"Rutvi","Soni","soni.rutvi%gmail.com","123456","123456"},
                                {"Rutvi","Soni","soni#rutvi@gmail.com","123456","123456"},
                            };

    }
    @Test(dataProvider = "Authentication1" )
    public void invaliddata(String fname, String lname,String email,String pass,String cpass)
    {
        driver.get("http://canvaswish.7bits.in/canvaswish/register");

        WebElement firstname = driver.findElement(By.id("FirstName"));
        firstname.clear();
        firstname.sendKeys(fname);

        WebElement lastname = driver.findElement(By.id("LastName"));
        lastname.clear();
        lastname.sendKeys(lname);

        WebElement mail = driver.findElement(By.id("Email"));
        mail.clear();
        mail.sendKeys(email);

        WebElement password = driver.findElement(By.id("Password"));
        password .clear();
        password .sendKeys(pass);

        WebElement conpassword = driver.findElement(By.id("ConfirmPassword"));
        conpassword .clear();
        conpassword.sendKeys(cpass);

        WebElement register = driver.findElement(By.id("register-button"));
        register.click();

        String geturl=driver.getCurrentUrl();
        System.out.println("geturl->"+geturl);

        if(geturl.equals(expectedurl))
        {
            Assert.assertTrue(true);
            testresultdata.put("3", new Object[] { 2d,fname,lname, email, pass,cpass, expectedurl, geturl, "Pass" });

        }
        else
        {
            testresultdata.put("3", new Object[] { 2d,fname,lname, email, pass,cpass, expectedurl, geturl, "Fail" });
            Assert.assertTrue(false);

        }

    }

@AfterClass
    public void setupAfterclass() throws IOException
    {
        Set<String> keyset = testresultdata.keySet();
        int rownum = 0;

        for (String key : keyset)
        {
            Row row = sheet.createRow(rownum++);
            Object[] objArr = testresultdata.get(key);
            int cellnum = 0;


            for (Object obj : objArr)
            {
                Cell cell = row.createCell(cellnum++);

                if (obj instanceof Date)
                    cell.setCellValue((Date) obj);
                else if (obj instanceof Boolean)
                    cell.setCellValue((Boolean) obj);
                else if (obj instanceof String)
                {
                        if(obj.equals("Pass"))
                        {
                            font.setColor(HSSFColor.GREEN.index);
                            style.setFont(font);
                            cell.setCellStyle(style);
                            cell.setCellValue((String) obj);   
                        }
                        else if(obj.equals("Fail"))
                        {
                            font.setColor(HSSFColor.RED.index);
                            style.setFont(font);
                            cell.setCellStyle(style);
                            cell.setCellValue((String) obj);
                        }

                        {

                            style.setFont(font);  
                            cell.setCellValue((String) obj);
                        }
                }
                else if (obj instanceof Double)
                    cell.setCellValue((Double) obj);
            }
        }
        try
        {
            FileOutputStream out = new FileOutputStream(new File("E:\\Register.xls"));
            workbook1.write(out);
            out.close();
        //  System.out.println("Excel written successfully..");
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }   

I want to display multiple test cases in excel sheet but now problem is that last data is onle display in sheet

0
testresultdata.put("3", new Object[] { });

Here you put your test result data into the same "3" key every time.

From the Java HashMap documentation:

put(K key, V value) Associates the specified value with the specified key in this map.

It will overwrite your the data in the key and act is if there was only one test, the last one.

Instead you need to update the "3" key for each run. For example by adding a Test-ID field to your Authentication1 dataprovider.

Example, add a extra field in the data, pass it to the test method, pass it to the put method:

@DataProvider(name = "Authentication1")
public static Object[][] credentials1()
{

    return new Object[][] { 
                            {"TestID1", "12234", " 6576","gfdgfdg"," dfg","gfd "},
                            {"TestID2", "&^*&^","&*^*&^","soni.rutvigmail.com","ghgfh","ghfghgfh"}
}

@Test(dataProvider = "Authentication1" )
public void invaliddata(String testId, String fname, String lname,String email,String pass,String cpass)
{
   testresultdata.put(testID, new Object[] { });
}
| improve this answer | |
  • i have doubt in how to pass key value. – Rutvi Soni Feb 27 '17 at 10:24
  • Added an example, maybe ask developers close by? This seems like a basic programming question. I would prefer to ask in your local office first before asking on the internet. :) – Niels van Reijmersdal Feb 27 '17 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.