I use WatiN to analyze my website. I have a button that starts the process. I open a browser window and move to where I need to go, then create a new task that calls a method called DoWork .
My problem is that if I call a new method at the end of DoWork to do something else, I get strange results when I try to make the program navigate my site, however, if I don't call this new method from DoWork and just plug in a new method before clicking a button, everything works fine. So my question is, am I incorrectly calling my new method from a background process method, DoWork ?
code:
IE browser = new IE("http://www.mywebsite.com/"); string startYear; string endYear; int NumRows; Task myThread; public Form1() { InitializeComponent(); } private void Start_Click(object sender, EventArgs e) { startYear = txtStartYear.Text; endYear = txtEndYear.Text; //website navigation work removed for brevity browser.Button(Find.ById("ContentPlaceHolder1_btnApplyFilter")).Click(); int numRows = browser.Div(Find.ById("scroller1")).Table(Find.First()).TableRows.Count -1; NumRows = numRows; lblTotalRows.Text = numRows.ToString(); myThread = Task.Factory.StartNew(() => DoWork()); } public void DoWork() { List<string> myList = new List<string>(NumRows); txtStartYear.Text = startYear; txtEndYear.Text = endYear; for (int i = 1; i < NumRows; i++) { TableRow newTable = browser.Div(Find.ById("scroller1")).Table(Find.First()).TableRows[i]; string coll = string.Format("{0},{1},{2},{3},{4}", newTable.TableCells[0].Text, newTable.TableCells[1].Text, newTable.TableCells[2].Text, newTable.TableCells[3].Text, newTable.TableCells[4].Text); myList.Add(coll); label1.Invoke((MethodInvoker)delegate { label1.Text = i.ToString(); }); } //database work removed for brevity. browser.Button(Find.ById("btnFilter")).Click(); newMethod(); } public void newMethod() { int start = int.Parse(startYear); start++; startYear = start.ToString(); int end = int.Parse(endYear); end++; endYear = end.ToString(); browser.SelectList(Find.ById("selStartYear")).SelectByValue(startYear); browser.SelectList(Find.ById("selEndYear")).SelectByValue(endYear); //removed for brevity } }
Repeat if I call newMethod from DoWork , the line browser.SelectList(Find.ById("selStartYear")).SelectByValue(startYear) does not work correctly, but if I remove the call to newMethod from DoWork and just close newMethod until the button works fine. I am wondering if this is related to the DoWork background task?
When I say that it does not behave correctly, I mean that when you select an item from the drop-down list, the page is automatically sent back, however the above line of code selects it, but the page does not send back, which should be possible. If I do not call the method in DoWork , I do not have this problem.