Reputation: 1197
there is an authtoken in the cookies which is used for verifying user but when i try to run selenium C# test case code using Nunit and cmd, the chrome instance launch and there is no cookies so it redirect me to login page. the question is why there is no cookies in the instance that is launched during test and how i resolve this problem. this is my code.
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace AutomationTest
{
[TestFixture]
public class SeleniumTest
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
driver = new ChromeDriver();
baseURL = "http://localhost/";
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
//try
//{
// driver.Quit();
//}
//catch (Exception)
//{
// // Ignore errors if unable to close the browser
//}
//Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void TheSTest()
{
driver.Navigate().GoToUrl(baseURL + "/TrailHead/");
driver.FindElement(By.Id("addNewLeadButton")).Click();
driver.FindElement(By.CssSelector("td.formTitle > input.td-button")).Click();
driver.FindElement(By.Id("township")).Clear();
driver.FindElement(By.Id("township")).SendKeys("2n");
driver.FindElement(By.Id("range")).Clear();
driver.FindElement(By.Id("range")).SendKeys("2e");
driver.FindElement(By.Id("section")).Clear();
driver.FindElement(By.Id("section")).SendKeys("2");
driver.FindElement(By.Id("legal")).Clear();
driver.FindElement(By.Id("legal")).SendKeys("2");
driver.FindElement(By.Id("NRI")).Clear();
driver.FindElement(By.Id("NRI")).SendKeys("2");
driver.FindElement(By.Id("NMA")).Clear();
driver.FindElement(By.Id("NMA")).SendKeys("2");
driver.FindElement(By.Id("tractAskedPrice")).Clear();
driver.FindElement(By.Id("tractAskedPrice")).SendKeys("2");
driver.FindElement(By.CssSelector("div.modalFooter > div.footer-right-button-save")).Click();
driver.FindElement(By.XPath("//div[@onclick='saveAndExit()']")).Click();
// Warning: assertTextPresent may require manual changes
Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.CssSelector("BODY")).Text, "^[\\s\\S]*$"));
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
private bool IsAlertPresent()
{
try
{
driver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException)
{
return false;
}
}
private string CloseAlertAndGetItsText()
{
try
{
IAlert alert = driver.SwitchTo().Alert();
string alertText = alert.Text;
if (acceptNextAlert)
{
alert.Accept();
}
else
{
alert.Dismiss();
}
return alertText;
}
finally
{
acceptNextAlert = true;
}
}
}
}
Upvotes: 0
Views: 433
Reputation: 73
There are a number of strategies you could implement to resolve this:
HttpWebRequest
but shouldn't be too difficult.If the authentication cookie doesn't change, you could manually get the name and value and incorporate this into your tests by adding it to the session:
var cookie = new Cookie("CookieName","CookieValue");
driver.Manage().Cookies.AddCookie(cookie);
Our company has option 2 in place and we are in the process of implementing option 1.
Upvotes: 0
Reputation: 38079
The Chrome WebDriver uses a temporary session to do all of its work. So any cookies you have set as your user will not carry over.
If you want to override this, then you can use the user-data-dir
property.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
All of this came from looking at their docs.
Upvotes: 1