0

I am a beginner in coded UI testing, coding isn't my strong suit. I want to ask you guys this with the simples answer possible.

Let's say I have a automated script in C# that

  • opens a browser google.com
  • Searches for a string value in Google

If I wish to replace the static string value with some values in a CSV file How do I do that?

Expectation:

  • I write a program that launches Google.com
  • Fetches a a string from CSV and searches for it
  • Then repeats it again from same file for other values

Below is my code.

public CodedUITest1()
        {
        }

       [TestMethod]
        public void openBrw()
        {

            BrowserWindow browser = BrowserWindow.Launch("www.google.com");
            UITestControl UISearch = new UITestControl(browser);
            UISearch.TechnologyName = "Web";
            UISearch.SearchProperties.Add("ControlType", "Edit");
            UISearch.SearchProperties.Add("Id", "lst-ib");
            Keyboard.SendKeys(UISearch, "Australian Cricket Team");
            this.UIMap.

MY CSV files looks like this with 1 column

column1
How to win matches
Where to find dragons
japan in 1998

Please tell me the easiest way!

Firaun
  • 369
  • 1
  • 5
  • 21
  • possible duplicate of [How to run a test many times with data read from .csv file (data driving)](http://stackoverflow.com/questions/23469100/how-to-run-a-test-many-times-with-data-read-from-csv-file-data-driving) – AdrianHHH Mar 26 '15 at 18:10

2 Answers2

0

Add in a data source to your test method. add in the csv file to your solution. Right click folder /solution, add existing item, select the file, change the properties of the file 'Copy to Output Directory' to 'copy always'

        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]
        [TestMethod]
        public void openBrw()
        {

            BrowserWindow browser = BrowserWindow.Launch("www.google.com");
            UITestControl UISearch = new UITestControl(browser);
            UISearch.TechnologyName = "Web";
            UISearch.SearchProperties.Add("ControlType", "Edit");
            UISearch.SearchProperties.Add("Id", "lst-ib");
//search by column1
            Keyboard.SendKeys(UISearch, TestContext.DataRow["column1"].ToString());            

        }
lloyd
  • 1,683
  • 2
  • 19
  • 23
  • Very very close man! Thanks. Now atleast it ran the test 3 times which shows that it knows there are 3 values in csv file. But sad part is, test still failed as it doesn't type anything in UISearch field. It says, "System.ArgumentException: Column 'column1' does not belong to table ." – Firaun Mar 27 '15 at 09:26
  • It worked. Instead of creating a CSV file within VS; I created file manually in notepad and then fed it to project. Thanks man! – Firaun Mar 27 '15 at 09:46
  • @Firaun see http://stackoverflow.com/questions/24735579/codedui-test-does-not-read-data-from-csv-input-file/24768318#24768318 for column not in data table. – AdrianHHH Mar 27 '15 at 10:36
0

I can't comment so I assume you want them to press a button before searching for the next value. The easiest way for me is to read the csv file using a reader then read a line and when they press a button to read the next line

private String path = @"Your Path Here"
private int i = 1;
private static String BrowserLocation;

public Form1()
{
    InitializeComponent();
}
public void Form1_Load(){ //You can click on the form in the designer to automatically create this method
    BrowseLocaton = ReadCsvLine(path, i);
    openBrsw(BrowseLocation);
    //Instantly browse to first line in the csv file when it loads
}
public openBrsw(String LaunchPath) //I edited the method
{
     BrowserWindow browser = BrowserWindow.Launch(LaunchPath);
     //put your browser properties here
}
public static String ReadCSVLine(String CsvPath, int LineNumber) 
{
    //this retrieves the line number + 1 (LineNumber = 99 retrieves line 100) because of the column header
    return File.ReadLines(CsvPath).ElementAtOrDefault(LineNumber);
    //Method above is same as File.ReadAllLines(CsvPath, Encoding.default).ElementAt(LineNumber)
}
private void NextButton_Click() 
{//Make a button called NextButton and set this method to clicked event in designer(Comment if you need help on this)
    //When they click on this button navigate to next address
    i++;
    BrowseLocaton = ReadCsvLine(path, i);
    openBrsw(BrowseLocation);
}

Note: I didn't put how to make it stop reading for sure, if you need this, comment. If there are any problems, let me know as a comment and I will edit it.

Abob
  • 751
  • 5
  • 27