2013-02-22 43 views
0

良好的夜晚, 在一個有點巨大的屁股泡菜,可以使用一點點的幫助。所以我寫了這個C#控制檯應用程序,它使用Selenium打開一個網頁,登錄,做一些事情,然後提交一個表單並註銷網站。現在我有一個for循環做100次。現在很少有它可能會打嗝並引發異常,導致頁面加載速度不夠快或什麼。我認爲使用try/catch可能會很好,但是一旦catch捕捉到異常,但是我希望它重做它所在並繼續的那個循環數。因此,例如,如果我在100的迭代66上,並且它引發了一個異常,導致頁面加載速度不夠快,或者頁面上出現了該鏈接的錯誤,我需要它來捕獲它,記錄它,然後重新啓動數字66再次。下面是我的一些原始代碼和我已經得到的另一部分。添加嘗試並抓住現有程序

using System; 
using System.Collections.Generic; 
using System.Text; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium.Support.UI; 
using System.Threading; 
using System.IO; 

namespace SeleniumTest 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
      for (Int64 i = 1; i < 100; i++) 
      { 
       DateTime time; 
       time = DateTime.Now; 
       StreamWriter tw = new StreamWriter(@"C:\folder\file.txt", true); 
       IWebDriver driver = new FirefoxDriver(); 
       tw.WriteLine("Staring test," + time); 
       driver.Navigate().GoToUrl("http://site.com"); 
       driver.FindElement(By.Name("username")).Clear(); 
       driver.FindElement(By.Name("username")).SendKeys("username"); 
       driver.FindElement(By.Name("password")).Clear(); 
       driver.FindElement(By.Name("password")).SendKeys("password"); 
       driver.FindElement(By.CssSelector("input.ui-standard-button")).Click(); 
       driver.FindElement(By.LinkText("page")).Click(); 
       driver.FindElement(By.LinkText("page")).Click(); 
       Thread.Sleep(5000); 
       //Do awesome stuff 
       DateTime time1; 
       time1 = DateTime.Now; 
       driver.FindElement(By.CssSelector("div.Parameters")).Click(); 
       driver.FindElement(By.Name("submit")).Click(); 
       driver.FindElement(By.LinkText("Logoff")).Click(); 
       driver.Quit(); 
       tw.WriteLine("Stopping Test Successfully," + time1); 
       tw.Flush(); 
       tw.Close(); 
       Thread.Sleep(10000); 
      } 
     } 
    } 
} 


using System; 
using System.Collections.Generic; 
using System.Text; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium.Support.UI; 
using System.Threading; 
using System.IO; 

namespace SeleniumTest 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     try 
      { 
      for (Int64 i = 1; i < 100; i++) 
      { 
       DateTime time; 
       time = DateTime.Now; 
       StreamWriter tw = new StreamWriter(@"C:\folder\file.txt", true); 
       IWebDriver driver = new FirefoxDriver(); 
       tw.WriteLine("Staring test," + time); 
       driver.Navigate().GoToUrl("http://site.com"); 
       driver.FindElement(By.Name("username")).Clear(); 
       driver.FindElement(By.Name("username")).SendKeys("username"); 
       driver.FindElement(By.Name("password")).Clear(); 
       driver.FindElement(By.Name("password")).SendKeys("password"); 
       driver.FindElement(By.CssSelector("input.ui-standard-button")).Click(); 
       driver.FindElement(By.LinkText("page")).Click(); 
       driver.FindElement(By.LinkText("page")).Click(); 
       Thread.Sleep(5000); 
       //Do awesome stuff 
       DateTime time1; 
       time1 = DateTime.Now; 
       driver.FindElement(By.CssSelector("div.Parameters")).Click(); 
       driver.FindElement(By.Name("submit")).Click(); 
       driver.FindElement(By.LinkText("Logoff")).Click(); 
       driver.Quit(); 
       tw.WriteLine("Stopping Test Successfully," + time1); 
       tw.Flush(); 
       tw.Close(); 
       Thread.Sleep(10000); 
      } 
     } 
     catch(Exception e) 
     { 
      StreamWriter tw = new StreamWriter(@"C:\folder\file.txt", true); 
      tw.WriteLine("Problem happened. Restarting test. Exception is :" + e); 
      //Line of code to restart test at number 66 which I don't know 
     } 
    } 
    } 
} 

Where //重啓66號測試的代碼行,我不知道我的知識是在哪裏結束,希望是別人在哪裏。任何指導你可以給予很大的和讚賞。

+0

提示:把try/catch語句裏面的循環.... – 2013-02-22 01:13:26

+0

只要小心,不要讓自己陷入無限循環,如果它不斷失敗。如果你知道超時拋出的顯式異常,那麼你應該知道這一點,並讓其他嚴重的錯誤通過。 – 2013-02-22 01:29:00

回答

1

遇到異常時減少計數器應該這樣做。

for (Int64 i = 1; i < 100; i++) { 
    try { 
     //main code here 
    } catch (Exception ex) { 
     //logging here 
     i--; 
    } 
} 
+0

這次我和其他一些人一起進行了測試,實際上我可以看到最好的效果,但是想要感謝大家的想法和控制。非常感謝你粘* *拳泵* ....現在我有另一個問題是Firefox實際上並沒有關閉(呃我沒有告訴它關閉在我的抓住)。你會推薦我在嘗試之前打開Firefox,如果發生錯誤然後關閉?或者你會推薦別的嗎? – jladd 2013-02-22 01:39:05

1

我會嘗試找出爲什麼它的失敗,但如果這不是一種選擇,我會把它轉換成while循環與嘗試捕捉

int i = 100; 
    while(i > 0) 
    { 
     try 
    { 
    //Do your logic here 
     i-- 
    } 
     catch 
    { 
     //Log failure 
     }//Don't decrement in case of failure 
    } 
+0

我只是認爲while循環更清晰/可讀,如果它不是確定性循環將執行多少次 – TGH 2013-02-22 01:26:03

+0

我一直看到它失敗的原因是我的ISP決定有一個lat尖峯,並且它無法加載頁面由於那個時間。我會試試這個 – jladd 2013-02-22 01:29:09

0
for (Int64 i = 1; i < 100; ++i) 
{ 
    for (;;) 
    { 
    try 
    { 
     //code here 
     break; 
    } 
    catch (Exception exc) 
    { 
     //log error 
    } 
    } 
}