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

Asked

Viewed 85 times

1

I have a test script in Selenium Webdriver using C# in which I read data from a file .txt external.

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 adjust the path manually directly in the script.

It is possible to set the path C:\Users\...\myData.txt as being a type of variable, I mean, not being permanent in the body of the script?

My situation is as follows: I own about 15 test projects in Visual Studio’s 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 archive .txt described directly in the script for each project, so that each of them reads its own file .txt. If I set a variable to the path, I could read this path from another file .txt, for example, but I believe that doing so is redundant.

Here is 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();
                }
            }
        }

1 answer

0


Do the relative path, even, and add your . txt of each project in the Solution Explorer, in the respective csprojs.

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

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

  • Thank you @Marcelouchimura! Question solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.