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);
.