【发布时间】:2015-05-20 09:15:55
【问题描述】:
我正在 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
【问题讨论】:
-
您的问题不清楚。您能否展示该方法的输出以及您真正希望它输出的内容?
-
谢谢。我已经编辑它添加了我期望的输出。
-
这条线是什么意思
**start[i] = (i + 1).ToString() +ab +cd;**?你是说start[i] = (i + 1).ToString() +ab +cd;吗? -
这正是问题所在。这意味着将通过引用传递的变量附加到每个编号的行。我想要的是,对于每次迭代,将调用的变量的串联添加到每一行。
标签: c# arrays file for-loop foreach