1

I want to test my website with a DataGridView User.

And I create n thread from n account in DataGridView and run at the same time to test bug.

private void btnStartProgram_Click(object sender, EventArgs e)
{
    Task.Factory.StartNew(() => 
           Parallel.ForEach(dtLink.AsEnumerable(), items => 
           OpenNRowsInData(items["username"].ToString(), items["password"].ToString())));
}
private string link = "http://localhost/laravelapp1/login";
private void OpenNRowsInData(string username, string password)
{
    browser = new Lapsoft_OneDriver(Browsers.Chrome);
    browser.GoToUrl(link);
    while (browser.FindElementByXPath("//a[contains(@class, 'btn-close')]") == null)
        Thread.Sleep(50);
    if (browser.FindElementByXPath("//a[contains(@class, 'btn-close')]") != null)
        browser.FindElementByXPath("//a[contains(@class, 'btn-close')]").Click();
    browser.FindElementById("txtUserName").SendKeys(username);
    browser.FindElementById("txtpassword").SendKeys(password);
    browser.FindElementById("btnlogin").Click();
}

First, the task starts four process Chrome(because I have four rows in DataTable dtLink, but only 2 or 3 Task is going to a link. Another tab will blank page.

Second, only 1 Task can run, other Task is failed at line:

browser.FindElementByXPath("//a[contains(@class, 'btn-close')]").Click();

Because it throws an exception: element not visible.

When I run only 1 Thread, this exception not occurs.

RESOLVED:

Thanks @IvanStoev!!!

@IvanStoev

Looks like you are using one and the same browser field for all parallel tasks. Just make it a local variable, e.g. var browser = ...

Change line:

browser = new Lapsoft_OneDriver(Browsers.Chrome);

to

var browser = new Lapsoft_OneDriver(Browsers.Chrome);.

Ave
  • 4,338
  • 4
  • 40
  • 67
  • 1
    Looks like you are using one and the same `browser` field for all parallel tasks. Just make it a local variable, e.g. `var browser = ...` – Ivan Stoev Jul 20 '16 at 07:32
  • Well I'm not sure you should be mixing `Task`s with `Parallel.ForEach()` http://stackoverflow.com/a/11565317/585968. Besides, don't you need to wait for the `Task` to complete? –  Jul 20 '16 at 07:39
  • @IvanStoev: Please add your answer. I will vote and accept your answers is correct. It working for me. Thank you very much. Can you explain why do this? I usually declare like: `Lapsoft_OneDriver browser` and using it, keyword `var` is different? – Ave Jul 20 '16 at 07:40
  • 1
    It's not different as soon as you declare it in a local scope. `var browser = ..` or `Lapsoft_OneDriver browser = ...` is one the same. What about posting an answer, I don't think my comment deserves too much credit, glad that helped and really appreciate your kindly proposal :) Please feel free to post the resolution as self answer and accept it, so the people know it's closed. – Ivan Stoev Jul 20 '16 at 08:58
  • Thank you very much. I updated my question is resolved and your answer. – Ave Jul 20 '16 at 09:02

1 Answers1

1
//maximize window// Chrome
ChromeOptions options = new ChromeOptions();
options.AddArgument("--start-maximized");
driver = new ChromeDriver(options);

//FIREFOX
driver.Manage().Window.Maximize();

There can be an issue with the windows it is minimized, and therefor it cant find the element, because it is "out" from the screen

  • 1
    I was setting in `Lapsoft_OneDriver(Browsers.Chrome)`. I was resolved my problem by change `browser = ...` to `var browser = ...`. – Ave Jul 20 '16 at 07:54