亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

selenium C#:如何在測試方法中跳過登錄

selenium C#:如何在測試方法中跳過登錄

C#
嚕嚕噠 2023-12-17 10:22:36
我必須在 C# Selenium 中測試一個 Web 應用程序,并且所有功能都需要在測試之前登錄。有什么方法可以在測試中跳過登錄步驟嗎?因為它們在重復并浪費時間...我已經閱讀了有關將登錄詳細信息保存到 cookie 的信息,但不確定如何以及在何處添加 cookie 以及如何在測試方法中調用它們。另外,如果我使用 cookie,我將無法通過在其中添加 [Parallelizable] 來并行運行它們namespace ParallelGrid {[TestFixture][Parallelizable]public class ParallelGrid1{    public static IWebDriver driver;      [SetUp]       public void Setup()       {          ChromeOptions options = new ChromeOptions();          driver = new ChromeDriver();       }    [Test]    public void Test1()    {        driver.Navigate().GoToUrl(" ");        //enter username        //enter password        //press submit       //go to home screen       //perform test 1    }    [Test]              public void Test2()        {                 driver.Navigate().GoToUrl(" ");        //enter username        //enter password        //press submit        //go to home screen        //perform test 2        }       [Test]              public void Test3()        {                 driver.Navigate().GoToUrl(" ");        //enter username        //enter password        //press submit        //go to home screen        //perform test 3        }    }}'''
查看完整描述

2 回答

?
慕碼人8056858

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 將保存在配置文件文件夾中,第二次執行后,您將被記錄,您可以調用每個測試,而無需在每個測試中登錄


查看完整回答
反對 回復 2023-12-17
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

將 driver.Url = "http:/yoururlhere 添加到 [SetUp],因為它在每次測試之前執行一次

https://nunit.org/docs/2.2.10/fixtureSetup.html


查看完整回答
反對 回復 2023-12-17
  • 2 回答
  • 0 關注
  • 180 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號