I have to test a web app in c# selenium and all functions need login before test. Is there any way i can skip login steps in my tests? as they are repeating and wasting time...I have read about saving login details to cookie, but not sure how and where to add cookie and how to call them in test methods. Also that if i use cookies, i will not be able to run them parallel by adding [Parallelizable] thing in it
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
}
}
}'''
You can use user-data-dir inside of chromeoptions to save profile data, them you can check if you are logged in at the init of every test.
Example:
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
}
After the first execution the cookies will be saved in the profile folder and after the second execution you will be logged, them you can call every test without login in every one