Schreiben Test-Ergebnisse in Excel mithilfe von Selen

Habe ich getan, viel Forschung auf diese Frage und habe versucht, viele verschiedene Methoden, aber keiner von Ihnen das tun, was ich möchte, oder die Erklärungen umzusetzen in meinem eigenen code sind wirklich vage.

Ich brauche um den export der Testergebnisse (TestID, Erwartete Ergebnis, bestanden oder nicht bestanden) in einem excel-sheet. Ich bin derzeit mit TestNG und Apache POI.

Ich weiß, wie ich zum schreiben in eine excel-Tabelle, aber ich bin absolut verloren, wie zu schreiben oder nicht, ob etwas erfolgreich war oder nicht. Ich bin derzeit mit einigen code, der nicht exakt arbeiten - manchmal schreibt er, manchmal nicht. Ich brauche möglichst einfache, einfache Möglichkeit, dies zu tun, mit einer guten Erklärung.

Zeige ich Ihnen meine aktuellen @BeforeClass, @AfterClass, und zwei @Test Blöcke.

@BeforeClass:

@BeforeClass(alwaysRun = true)
    public void setupBeforeSuite(ITestContext context) throws IOException {
        //create a new work book
        workbook = new HSSFWorkbook();
        //create a new work sheet
        sheet = workbook.createSheet("Test Result");
        testresultdata = new LinkedHashMap < String, Object[] > ();
        //add test result excel file column header
        //write the header in the first row
        testresultdata.put("1", new Object[] {
            "Test Step Id", "Action", "Expected Result", "Actual Result"
        });

    }

@AfterClass:

@AfterClass
      public void setupAfterSuite(ITestContext context) {
        //write excel file and file name is TestResult.xls 
        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)
                    cell.setCellValue((String)obj);
                else if(obj instanceof Double)
                    cell.setCellValue((Double)obj);
            }
        }
        try {
            FileOutputStream out =new FileOutputStream(new File("C:/Users/PathToFile/LoginCombinations.xls"));
            workbook.write(out);
            out.close();
            System.out.println("Excel written successfully..");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

@Test Blöcke:

@Test(priority=0)
    public void successfulLogin() throws InterruptedException {

        Properties prop = new Properties();
        InputStream config = null;
        InputStream signinpage;

        try {
            //First we iterate over and read the config file
            config = new FileInputStream("C:/Users/PathToFile/src/ConfigFiles/config");
            prop.load(config);
            signinpage = new FileInputStream("C:/Users/PathToFile/src/ObjectRepositories/signinpage");
            prop.load(signinpage);

            //Next we initiate the driver, and navigate to the Web Application
            driver = new FirefoxDriver();
            driver.get(prop.getProperty("url"));
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            //Now we run the first step, "enterValidCredentials"
            //In this test, this is actually the only step.
            LoginPage.enterValidCredentials.run(driver);

            //Assert that we landed on the Product Select page.
            //assertEquals(driver.findElement(By.xpath(prop.getProperty("tempproductselect"))).getText(), "SELECT PRODUCT");
            try{
                assertEquals(driver.findElement(By.xpath(prop.getProperty("tempproductselect"))).getText(), "SELECT PRODUCT");
                  //add pass entry to the excel sheet
                  testresultdata.put("2", new Object[] {1d, "User can login with a valid username and password", "Login successful","Pass"});
                  }

                  catch(Exception e)
                  {
                    //add fail entry to the excel sheet
                    testresultdata.put("2", new Object[] {1d, "User can login with a valid username and password", "Login successful","Fail"});
                  }

            //Write the test result to the sheet.

            driver.close();
            Alert alert = driver.switchTo().alert();
            alert.accept();

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (config != null) {
                try {
                    config.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

@Test(priority=1)
    public void invalidCredentialsOne() throws InterruptedException {

        Properties prop = new Properties();
        InputStream config = null;
        InputStream signinpage;

        try {
            //First we iterate over and read the config file
            config = new FileInputStream("C:/Users/PathToFile/src/ConfigFiles/config");
            prop.load(config);
            signinpage = new FileInputStream("C:/Users/PathToFile/src/ObjectRepositories/signinpage");
            prop.load(signinpage);

            //Next we initiate the driver, and navigate to the Web Application
            WebDriver driver;
            driver = new FirefoxDriver();
            driver.get(prop.getProperty("url"));
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            //Now we run the first step, "invalidCredentialsOne"
            //In this test, this is actually the only step.
            LoginPage.invalidCredentialsOne.run(driver);
            Thread.sleep(5000);

            try{
                assertEquals(driver.findElement(By.xpath(prop.getProperty("failedlogin"))).getText(), "LOG IN");
              //add pass entry to the excel sheet
                testresultdata.put("3", new Object[] {2d, "User should not be able to login with an invalid password", "Login failed","Pass"});
              }

              catch(Exception e)
              {
                //add fail entry to the excel sheet
                testresultdata.put("3", new Object[] {2d, "User should not be able to login with an invalid password", "Login failed","Fail"});
              }           

            //Write the test result to the sheet.

            //After the test, we close the driver.
            driver.close();
            Alert alert = driver.switchTo().alert();
            alert.accept();

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (config != null) {
                try {
                    config.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

Zweiten test invalidCredentialsOne, der nie schreibt zu excel, ob ich es bestehen oder nicht.

Java ist auch neu für mich, so verzeihen Sie jede Formatierung/lingo/was auch immer Fehler, die ich dort haben. Ich bin sehr offen für Anregung, ich bin versucht zu verbessern.

  • Was macht Sie benutzen .xls-Erweiterung ? Betrachten Sie das speichern als CSV: "Test-Id", "Action", "Erwartete Resultat", "Ergebnis" . Wenn Sie in excel öffnen, es wird getrennt auf die Spalten automatisch. Für das schreiben in die Datei verwende ich: BufferedWriter outputWriter; outputWriter = new BufferedWriter(new FileWriter("C:/zz/zz.txt")); outputWriter.write("Parameter");
  • Manager ' s Vorliebe, wirklich. CSV könnte auf jeden Fall funktionieren, ich müsste lernen, etwas mehr über BufferedWriter (wenn Sie irgendwelchen haben berechtigterweise gute tutorials zum schreiben von test-Resultate in CSV, schicken Ihnen auf diesem Weg).
  • Np, Kumpel, ich hatte dasselbe Problem Letzte Woche. alvinalexander.com/java/edu/qanda/pjqa00009.shtml dies ist der Artikel, die ich durchgeführt
  • Wenn jemand noch ausführlich erläutert Beispiele, wie konnte ich die Struktur meiner BeforeClass, AfterClass und zwei Test Blöcke in Textform wirksam CSV-Dateien würde ich auf jeden Fall akzeptieren, dass als Antwort. Der schwierigste Teil für mich nicht den code zu verstehen, es ist die Umsetzung, es Stück für Stück in meinen Rahmen.
  • Hi @jagdpanzer brauche ich Eure Hilfe. Wie pro Ihre Klasse vor, wie können wir hinzufügen, testresultdata Werte in excel-sheet. ich bin neu auf Selen. bitte helfen Sie mir.
InformationsquelleAutor jagdpanzer | 2015-10-21
Schreibe einen Kommentar