I have a test script in Selenium Webdriver using C # in which I read data from an external .txt
file.
The path to the file is fixed in the script, indicating a folder on my computer. But in the future other people will run this script on other computers, and will have to manually adjust the path directly in the script.
Is it possible to set the C:\Users\...\myData.txt
path to be a variable type, I mean, not being permanent in the body of the script?
My situation is this: I have about 15 test projects in the Visual Studio Solution Explorer. For each project I have to use different data in the tests. So that's why today I have a path to the .txt
file described directly in the script for each project, so that each of them reads its own .txt
file. If I set a variable to the path, I could read this path from another .txt
file, for example, but I believe doing that is redundant.
Here's part of the script:
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();
}
}
}