I have been trying to hit a Web controller directly to upload csv files using Nunit, Specflow and Restsharp but the tests are failing in groups but passes when individually. Another thing which we noticed is these are working fine locally as in running the app as localhost but not in other env. All other test groups are passing in that env. I have seen couple of posts which suggested not to use static properties to store values but i have tried that as well. we also added delay between threads but thats not working either
please can anyone help on this one or anyone who have faced this? The error I am getting is timeout. if you accces it from the browser then there are no issues
I am using context injection to pass values from one another in step def file. Here is what the code looks like
public (IRestResponse response,string RVtoken) GetRVtoken()
{
var client = new RestClient(ConfigurationManager.AppSettings["Web_URL"] +"Account/SignIn");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var html = new HtmlDocument();
html.LoadHtml(response.Content.ToString());
var RVtoken = html.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[1]/form[1]/input[1]/@value[1]").Attributes["value"].Value;
return (response, RVtoken);
}
public (CookieContainer cookieContainer, Uri target) GetAspxAuthToken(string email,string password, IRestResponse SignInResponse, string RVtoken)
{
string ReturnUrl = ConfigurationManager.AppSettings["Web_URL"] + "Account/SignIn?ReturnUrl=%2F";
var client = new RestClient(ReturnUrl);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(ReturnUrl);
httpRequest.CookieContainer = cookieContainer;
httpRequest.Method = Method.POST.ToString();
string postData = string.Format("Email={0}&Password={1}&__RequestVerificationToken={2}&RememberMe=false", email, password, RVtoken);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = byteArray.Length;
Uri target = new Uri(ReturnUrl);
httpRequest.CookieContainer.Add(new Cookie(SignInResponse.Cookies[0].Name, SignInResponse.Cookies[0].Value) { Domain = target.Host });
httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
Stream dataStream = httpRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse myresponse = (HttpWebResponse)httpRequest.GetResponse();
return (cookieContainer,target);
}
public IRestResponse Uploadflex(string flexType,string filepath,IRestResponse SignInResponse)
{
RestClient newclient = newclient = new RestClient(ConfigurationManager.AppSettings["Web_URL"] + "audiences/" + flexType + "/upload?utc=1580117555355");
var newrequests = new RestRequest(Method.POST);
newrequests.AddFile("targetAreaFile",filepath);
newrequests.AddCookie(SignInResponse.Cookies[0].Name, SignInResponse.Cookies[0].Value);
newrequests.AddCookie(User.cookieContainer.GetCookies(User.target)[".aspxauth"].Name.ToString(), User.cookieContainer.GetCookies(User.target)[".aspxauth"].Value.ToString());
newrequests.AddParameter("name", "targetAreaFile");
newrequests.AddParameter("Content-Type", "application/vnd.ms-excel");
newrequests.AddParameter("Content-Disposition", "form-data");
var response = newclient.Execute(newrequests);
return response;
}
ok, so the issue was I was creating differnet sessions for these tests but somehow cookies were not clearing so I put all the tests under same session and it works magically.