我在 Selenium Webdriver 中有一個(gè)使用 C# 的測(cè)試腳本,我在其中從.txt外部文件讀取數(shù)據(jù)。腳本上的路徑是固定的,表示我電腦上的一個(gè)文件夾。但是將來其他人將在其他計(jì)算機(jī)上運(yùn)行此腳本,他們將不得不直接在腳本上手動(dòng)調(diào)整路徑。是否可以將路徑設(shè)置C:\Users\...\myData.txt為一種變量,我的意思是,在腳本正文中不是永久的?這是腳本的一部分:using System;using NUnit.Framework;using OpenQA.Selenium;using OpenQA.Selenium.Chrome;using System.IO;using System.Collections;using System.Text;namespace SeleniumTests{ [TestFixture] public class Principal { IWebDriver driver = null; [SetUp] public void SetUp() { ChromeOptions options = new ChromeOptions(); options.AddArguments("--disable-infobars"); options.AddArguments("start-maximized"); driver = new ChromeDriver(options); } public class DataTXT { public string example1{ get; set; } public string example2{ get; set; } public string example3{ get; set; } public string example4{ get; set; } } public static IEnumerable DataTXT { get { string linha; using (FileStream readFile = new FileStream(@"C:\Users\...\myData.txt", FileMode.Open, FileAccess.Read)) { var reader = new StreamReader(readFile, Encoding.GetEncoding("iso-8859-1")); while ((line = reader.ReadLine()) != null) { var column = line.Split(';'); yield return new DataTXT { example1 = column[0], example2 = column[1], example3 = column[2], example4 = column[3] }; } reader.Close(); readFile.Close(); } } }
3 回答

嗶嗶one
TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
我可能有點(diǎn)誤解你的問題,因?yàn)槲以谶@里有點(diǎn)新。但是,如果您只是想要一個(gè)不是靜態(tài)定義的并且可以由用戶更改的路徑變量;您可以使用配置文件來定義它并改為指向配置。(或 ConfigurationManager MSDN-ConfigManager)

慕妹3146593
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
有多種方法可以做到這一點(diǎn)。
您可以在項(xiàng)目中創(chuàng)建路徑并知道它相對(duì)于 \bin 的位置。
我看到您正在使用 NUnit。NUnit
TestContext.CurrentContext.TestDirectory
將返回測(cè)試 dll 的路徑。如果您將 txt 文件添加到您的解決方案中并通過構(gòu)建后事件將其與 dll 一起復(fù)制,它將始終位于TestDirectory
.
- 3 回答
- 0 關(guān)注
- 244 瀏覽
添加回答
舉報(bào)
0/150
提交
取消