2016-05-26 1 views
0

私はログインページをテストするためにC#でいくつかのSeleniumテストを作成しました。各テストでは、同じExcelスプレッドシートから取得した異なる資格情報のペアを使用します。 Excelのスプレッドシートを読み込んで、特定の行にデータを配置して使用できるようにするために、Web上に大きなコードがあります。私が取り組んでいる問題は、テストは単独で実行され、一連のテストでは実行されないことです。後者の場合、最初のものだけが成功し、その後のすべてのテストは失敗し、「テキストはnullにはなれません」例外が発生します。私はそれが各テストの間に適切に処分されないExcelとは何かになる可能性があると疑っています。どのように私は適切にExcelのものをきれいにすることができます上の任意のアイデア?ここExcelオブジェクトを破棄して後続のセレンテストが失敗すると思われる問題はありますか?

私はExcelのオブジェクトをインスタンス化するコードである:Iブックを呼び出すのはここ

string path1 =  Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,  @"SeleniumData\LoginBook.xlsx"); 
ExcelLib loginBook = new ExcelLib(path1); 

である:ここ

rmMain_PageObject pageRMMain = new  rmLogin_PageObject().Login(loginBook.ReadData(1, "UserName"),  (loginBook.ReadData(1, "Password"))); 

メソッド自体である:

 using Excel; 
using System; 
using System.Collections.Generic; 
using System.Data; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace AutoTestProject1 
{ 
    public class ExcelLib 
    { 
     private DataTable ExcelToDataTable(string fileName) 
     { 
      //open file and returns as Stream 
      FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read); 
      //Createopenxmlreader via ExcelReaderFactory 
      IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //.xlsx 
      //Set the First Row as Column Name 
      excelReader.IsFirstRowAsColumnNames = true; 
      //Return as DataSet 
      DataSet result = excelReader.AsDataSet(); 
      //Get all the Tables 
      DataTableCollection table = result.Tables; 
      //Store it in DataTable 
      DataTable resultTable = table["Sheet1"]; 

      //return 
      return resultTable; 
     } 

     public ExcelLib(string fileName) 
     { 
      PopulateInCollection(fileName); 
     } 
     public class Datacollection 
     { 
      public int rowNumber { get; set; } 
      public int colNumber { get; set; }        //test 
      public string colName { get; set; } 
      public string colValue { get; set; } 
     } 

     static List<Datacollection> dataCol = new List<Datacollection>(); 

     private void PopulateInCollection(string fileName) 
     { 
      DataTable table = ExcelToDataTable(fileName); 

      //Iterate through the rows and columns of the Table 
      for (int row = 1; row <= table.Rows.Count; row++) 
      { 
       //for (int col = 0; col <= table.Columns.Count; col++) 
       for (int col = 0; col < table.Columns.Count; col++) 
       { 
        Datacollection dtTable = new Datacollection() 
        { 
         rowNumber = row, 
         colNumber = col,          //test 
         colName = table.Columns[col].ColumnName, 
         colValue = table.Rows[row - 1][col].ToString() 
        }; 
        //Add all the details for each row 
        dataCol.Add(dtTable); 
       } 
      } 
     } 

答えて

-1
var excelObject = new ExcelObject //what ever excel assy you are using 
try{ 
} 
catch(Exception e){ 
} 
finally{ 
    excelObject.Dispose(); 
} 
+1

あなたの回答を説明してくださいコードだけではなく – Leva7

関連する問題