Can I use a variable to set a path in FileStream?

1

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();
                }
            }
        }
    
asked by anonymous 23.06.2018 / 19:13

1 answer

0

Make the relative path, same, and add your .txt from each project in Solution Explorer, in the respective csprojs.

Imagine that the solution is the root and place your .txt in the same relative path used in new FileStream(caminho) .

In Solution Explorer, right-click on the .txt and go to Properties. In Copy To Output Directory, choose Copy if newer.

    
23.06.2018 / 19:31