良好的夜晚, 在一個有點巨大的屁股泡菜,可以使用一點點的幫助。所以我寫了這個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號測試的代碼行,我不知道我的知識是在哪裏結束,希望是別人在哪裏。任何指導你可以給予很大的和讚賞。
提示:把try/catch語句裏面的循環.... – 2013-02-22 01:13:26
只要小心,不要讓自己陷入無限循環,如果它不斷失敗。如果你知道超時拋出的顯式異常,那麼你應該知道這一點,並讓其他嚴重的錯誤通過。 – 2013-02-22 01:29:00