2 回答

TA貢獻1803條經驗 獲得超6個贊
您可以在 chromeoptions 中使用 user-data-dir 來保存配置文件數據,您可以在每次測試的初始化時檢查您是否已登錄。
例子:
public void Setup ( )
{
string ProfileDirect=Directory.GetCurrentDirectory()+"\\MyProfile";
if ( !Directory.Exists ( ProfileDirect ) )
{
//create data folder if not exist
Directory.CreateDirectory ( ProfileDirect );
}
// Create new option with data folder
var options=new ChromeOptions();
options.AddArgument ( @"user-data-dir="+ProfileDirect );
// Instance new Driver , with our current profile data.
Driver=new ChromeDriver(options);
if ( !IsLoggedIn ( ) )
{
Login ( );
}
}
public bool IsLoggedIn ( )
{
// Check if button logout is visible
return Driver.FindElement(By.XPath ( "//a[contains(@href,'logout')]" ))!=null;
}
public void Login ( )
{
//Some code to login
}
第一次執行后,cookie 將保存在配置文件文件夾中,第二次執行后,您將被記錄,您可以調用每個測試,而無需在每個測試中登錄

TA貢獻1829條經驗 獲得超7個贊
將 driver.Url = "http:/yoururlhere
添加到 [SetUp]
,因為它在每次測試之前執行一次
https://nunit.org/docs/2.2.10/fixtureSetup.html
- 2 回答
- 0 關注
- 180 瀏覽
添加回答
舉報