ShadowsFall88
ShadowsFall88

Reputation: 23

Pass item from listbox to selenium webdriver

I want to be able to pass items from listbox to a webdriver. Basically i have a list of URLs being added to the listbox via a text file and then i want to pass each line of the listbox to selenium webdriver so i can run a test. from what i can gather i need to make the each item in the listbox a string somehow and then pass that string to the webdriver. i am not sure how to go about this. any help would be much appreciated.

this is the code for the openFileDialog in the form.cs to load the text file into the Listbox

private void UrlFilePickerBtn_Click(object sender, EventArgs e)
    {
        var filePath = string.Empty;

        openFileDialog.InitialDirectory = Application.StartupPath;
        openFileDialog.Filter = "txt files (*.txt)|*.txt|All Files (*.*)|*.*";
        openFileDialog.FilterIndex = 2;
        openFileDialog.RestoreDirectory = true;

       if(openFileDialog.ShowDialog() == DialogResult.OK)
        {
            filePath = openFileDialog.FileName;

            var fileStream = openFileDialog.OpenFile();
            StreamReader reader = new StreamReader(fileStream);
            {
                string line;
                while ((line = reader.ReadLine()) != null) {
                    LSlistBox.Items.Add(line);
                }
            }
        }
    }

and this is in a seperate class where i want to be able to call the listbox item to a string i guess and the string will replace the current URL

public void OpenBrowsers()
    {
        Console.WriteLine("Normal Button Starting Browsers");
        chromeDriver = DriverClass.GetDriver("Chrome");
        foxDriver = DriverClass.GetDriver("Firefox");
        edgeDriver = DriverClass.GetDriver("Edge");

        chromeDriver.Navigate().GoToUrl("http://www.google.co.uk");

            
    }

hope that makes sense

Upvotes: 1

Views: 183

Answers (1)

rahul rai
rahul rai

Reputation: 2326

I believe you have already got a way of storing your lines in listbox. You can use below iteration to call each URL (considering one line consist of one URL).

 foreach (string url in LSlistBox.Items)
{
    //check if URL is valid
    Uri uriResult;
    bool result = Uri.TryCreate(url, UriKind.Absolute, out uriResult)&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
   //if valid URL call your OpenBrowsers method
   if (result ){
      OpenBrowsers(url)
         }
}

Then modify your OpenBrowsers method to accept parameter.

public void OpenBrowsers(string URL)
    {
        Console.WriteLine("Normal Button Starting Browsers");
        chromeDriver = DriverClass.GetDriver("Chrome");
        foxDriver = DriverClass.GetDriver("Firefox");
        edgeDriver = DriverClass.GetDriver("Edge");

        chromeDriver.Navigate().GoToUrl(URL);

            
    }

Upvotes: 1

Related Questions