我在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
答案 0 :(得分: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
保持不变。