晚上好, 在一个带有巨大脑屁的泡菜中,可以使用一点点帮助。所以我写了这个C#控制台应用程序,它使用Selenium打开网页,登录,做一些事情,然后提交表单并注销网站。现在我有一个for循环来做100次。现在很少它可能会打嗝并引发异常,因为页面加载速度不够快或其他什么。我认为使用try / catch可能会很好,但是一旦catch捕获异常,我希望它重做它所在的循环数并继续。例如,假如我在迭代66中的100并且它抛出异常导致页面加载不够快或者页面上没有该链接的错误,我需要它来捕获它,记录它,然后重新启动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
}
}
}
}
其中//在66号重新开始测试的代码行,我不知道是我知识何时结束,希望是其他人在哪里。您可以提供的任何指导都会很棒并且值得赞赏。
答案 0 :(得分:1)
遇到异常时减少计数器应该这样做。
for (Int64 i = 1; i < 100; i++) {
try {
//main code here
} catch (Exception ex) {
//logging here
i--;
}
}
答案 1 :(得分:1)
我会试着找出它失败的原因,但是如果那不是一个选项我会把它转换为带有try catch的while循环
int i = 100;
while(i > 0)
{
try
{
//Do your logic here
i--
}
catch
{
//Log failure
}//Don't decrement in case of failure
}
答案 2 :(得分:0)
for (Int64 i = 1; i < 100; ++i)
{
for (;;)
{
try
{
//code here
break;
}
catch (Exception exc)
{
//log error
}
}
}