2015-05-20 41 views
0

我在Selenium Web驅動程序中編寫測試用例,我想將結果輸出到日誌文件。測試用例應該在文本文件中按行編號。編號已經實現,但附加測試用例是我無法實現的。問題是for循環,我想追加通過引用傳遞的變量組合。這些變量將成爲測試用例。如果需要更多解釋,請告訴我。將「由參考變量調用」追加到文本文件中

問題是,連接定義的變量只能爲所有值附加一個值。

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

namespace ConsoleApplication5 
{ 
    class Class1 
    { 
     public static int counter; 
     public static String[] test = {"1", "2" }; 
     public static String[] test1 = { "a", "b" }; 

     public static void Main(String[] args) 
     { 
      new Class1().testing2(); 
     } 

     public String testing(String ab, String cd) 
     { 
      //Count the number of times the method is called 
      ++counter; 
      //Write it to a text file 
      File.WriteAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt", counter.ToString()); 
      //Read from that text file 
      String Readfiles = File.ReadAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt"); 
      //Convert to integer 
      Int32 myInt = Int32.Parse(Readfiles); 
      //Store in array variable 
      String[] start = new String[myInt]; 
      //iterate through 
      for (int i = 0; i < myInt; ++i) 
      { 
       **start[i] = (i + 1).ToString() +ab +cd;** 
      } 
      //Write into another text file on new lines 
      File.WriteAllLines(@"C:\Users\ken4ward\Desktop\Tidy\Writing.txt", start); 

      String gb = ab; String th = cd; String you = ab + cd; 
      return you; 
     } 

     public void testing2() 
     { 
      foreach(var item in Class1.test) 
      { 
       foreach (var item1 in Class1.test1) 
       { 
        String test = testing(item, item1); 
        Console.WriteLine(test); 
        Console.ReadLine(); 
       } 
      } 
     } 
    } 
} 

當我跑了它這是在文本文件中的輸出:

1 2b 
2 2b 
3 2b 
4 2b 

什麼,我想在文本文件輸出爲:

1 1a 
2 1b 
3 2a 
4 2b 
+1

你的問題不清楚。你能展示什麼方法輸出,你真的想要它輸出? –

+0

謝謝。我編輯它添加我期待的輸出。 – kehinde

+0

這行代表什麼意思?** start [i] =(i + 1).ToString()+ ab + cd; **'?你的意思是'開始[i] =(i + 1).ToString()+ ab + cd;'? – J3soon

回答

1

添加一個全局變量

public static String[] lines = { }; 

更改testing()對此:

public String testing(String ab, String cd) 
    { 
     //Count the number of times the method is called 
     ++counter; 
     //Write it to a text file 
     File.WriteAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt", counter.ToString()); 
     //Read from that text file 
     String Readfiles = File.ReadAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt"); 
     //Convert to integer 
     Int32 myInt = Int32.Parse(Readfiles); 
     //Store in array variable 
     String start = myInt.ToString() + ab + cd; 
     //Store the new data in the array 
     Array.Resize(ref lines, lines.Length + 1); 
     lines[lines.GetUpperBound(0)] = start; 
     //Write into another text file on new lines 
     File.WriteAllLines(@"C:\Users\ken4ward\Desktop\Tidy\Writing.txt", lines); 
     //String gb = ab; String th = cd; 
     String you = ab + cd; 

     return you; 
    } 

由於WriteAllText表示覆蓋文件。

for (int i = 0; i < myInt; ++i) 
{ 
    start[i] = (i + 1).ToString() +ab +cd; 
} 

(ab + cd)以上的代碼等於2b,這意味着只更改的行號,和2b保持不變。

+0

更好的方法是追加輸出文件。但它會更復雜一點。 – J3soon

+0

我是否要創建一個變量「行」?這會引發錯誤,它在當前上下文中不存在。欣賞。如果是,什麼數據類型? – kehinde

+0

添加一個全局變量'lines'。看到我的答案的第一行。 – J3soon