You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否为Coypu的FindCss、FindField等操作设置全局默认超时?

Coypu: Set Default Timeout to Avoid Explicitly Passing options Parameter

Question

I've written the following test code using Coypu:

var options = new Options { Timeout = TimeSpan.FromSeconds(1) }; 
using (var browser = GetFreshBrowserSession()) { 
    browser.Visit(TestParamNames.LoginUri.GetParameterByName() + "candidateportal/en-US/" + TestParamNames.Namespace.GetParameterByName()); 
    Assert.True(browser.FindCss("body > div.header > a.nav-item.job-search.selected > span.header-text", "Job Search", options).Exists()); 
    Assert.True(browser.FindField("keyword", options).Exists()); 
    Assert.True(browser.FindCss("body > div.content-wrapper > div.center.search-pane > form > span > span > input", options).Exists()); 
    Assert.True(browser.FindButton("Search", options).Exists()); 
    Assert.True(browser.FindButton("Reset", options).Exists()); 
    Assert.True(browser.FindCss("div#advanced-search-link","Advanced Search", options).Exists()); 
} 

Right now, I have to pass the options parameter (which includes my timeout setting) to every single action method. Is there a way to set a default timeout once so I don't have to explicitly pass this parameter every time?


Answer

Absolutely! You can set a default timeout in Coypu to avoid passing that options parameter every single time—here are two simple, effective approaches to clean up your code:

1. Configure Global Default Settings

Set up Coypu's global defaults once (typically in your test suite's initialization code, like a [SetUp] method) and every browser session will inherit these values automatically:

// Add this in your test setup (runs once before your tests execute)
SessionConfiguration.Default.Timeout = TimeSpan.FromSeconds(1);
// Optional: You can also adjust the retry interval if needed
SessionConfiguration.Default.RetryInterval = TimeSpan.FromMilliseconds(100);

Once this is configured, you can completely remove the explicit options parameter from all your method calls—Coypu will automatically use the global timeout for every action.

2. Pass Options When Initializing Your Browser Session

If you prefer the timeout to only apply to specific sessions (instead of all your tests), modify your GetFreshBrowserSession() method to initialize the session with your desired options:

private BrowserSession GetFreshBrowserSession()
{
    var sessionOptions = new Options { Timeout = TimeSpan.FromSeconds(1) };
    // All actions on this session will use the options we've defined
    return new BrowserSession(sessionOptions);
}

Then your test code becomes much cleaner, with no need to pass options to every method:

using (var browser = GetFreshBrowserSession()) { 
    browser.Visit(TestParamNames.LoginUri.GetParameterByName() + "candidateportal/en-US/" + TestParamNames.Namespace.GetParameterByName()); 
    Assert.True(browser.FindCss("body > div.header > a.nav-item.job-search.selected > span.header-text", "Job Search").Exists()); 
    Assert.True(browser.FindField("keyword").Exists()); 
    Assert.True(browser.FindCss("body > div.content-wrapper > div.center.search-pane > form > span > span > input").Exists()); 
    Assert.True(browser.FindButton("Search").Exists()); 
    Assert.True(browser.FindButton("Reset").Exists()); 
    Assert.True(browser.FindCss("div#advanced-search-link","Advanced Search").Exists()); 
} 

Choose the approach that fits your needs: global defaults are perfect for consistent settings across all tests, while session-specific options give you flexibility if different tests require different timeouts.


内容的提问来源于stack exchange,提问作者mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:36:16