我有一个用于触发长时间运行的数据队列的应用程序。长期运行,我的意思是每个队列大约需要12-16个小时,并且其中任何一个都不能并行执行。每个队列都有单独的步骤,在下一个队列运行之前需要成功。
在初始化ChromeDriver最多1000分钟时,我已经增加了超时时间
webDriver == new ChromeDriver(path,options,TimeSpan.FromMinutes(1000));
1000分钟后,我正在使用WebDriverWait
检查所有步骤是否成功。万一发生故障,我仍然必须等待1000分钟,然后我才能告知开发团队该故障。
是否有更好的方法来解决此问题?它还使我的浏览器保持打开状态1000分钟
答案 0 :(得分:1)
关于您的问题-是否有更好的方法来解决此问题?使用硒,并非如此。与通过UI测试相比,采用其他方法(例如API)会更好。但是,仍然可能,只是不理想。
对于这个问题,我最好的主意是设置某种控制器,该控制器可以管理您的WebDriver实例并跟踪12-16小时的排队时间。由于我没有关于您的项目架构或正在测试的队列的任何特定信息,因此这将是非常通用的实现。
这是一个简单的DriverManager类,用于控制创建和终止WebDriver会话:
public class DriverManager
{
public IWebDriver CreateDriver
{
// code to initialize your WebDriver instance here
}
public void CloseWebDriverSession
{
Driver.Close();
Driver.Quit();
}
}
接下来,这是一个测试用例实现,它利用DriverManager根据需要关闭并重新打开WebDriver。
public class TestBothQueues
{
// this driver instance will keep track of your session throughout the test case
public IWebDriver driver;
[Test]
public void ShouldRunBothQueues
{
// declare instance of DriverManager class
DriverManager manager = new DriverManager();
// start a webdriver instance
driver = manager.CreateDriver();
// run the first queue
RunFirstQueue();
// terminate the WebDriver so we don't have browser open for 12 hours
manager.CloseWebDriverSession();
// wait 12 hours
Thread.Sleep(TimeSpan.FromHours(12));
// start another WebDriver session to start the second queue
driver = manager.CreateDriver();
// run the second queue
RunSecondQueue();
// terminate when we are finished
manager.CloseWebDriverSession();
}
}
一些注意事项:
如果您要启动WebDriver实例以按时间间隔检查队列,也可以将此代码转换为while循环。例如,如果队列需要12到16个小时才能完成,则您可能要等待12个小时,然后每小时检查一次队列,直到可以确认队列已完成。看起来像这样:
// first, wait initial 12 hours
Thread.Sleep(TimeSpan.FromHours(12));
// keep track of whether or not queue is finished
bool isQueueFinished = false;
while (!isQueueFinished);
{
// start webdriver instance to check the queue
IWebDriver driver = manager.CreateDriver();
// check if queue is finished
isQueueFinished = CheckIfQueueFinished(driver);
// if queue is finished, while loop will break
// if queue is not finished, close the WebDriver instance, and start again
if (!isQueueFinished)
{
// close the WebDriver since we won't need it
manager.CloseWebDriverSession();
// wait another hour
Thread.Sleep(TimeSpan.FromHours(1));
}
}
希望这会有所帮助。