Deserialize cookies stored WebDriver
OK going round in circles, I need some help!
How do I deserialize this so I can re-load the cookies that I have stored
try
{
var cookies = driver.Manage().Cookies.AllCookies;
SiteCookieList siteCookies = new SiteCookieList();
siteCookies.Cookies = new List<SiteCookie>();
foreach (var cookie in cookies)
{
SiteCookie sc = new SiteCookie
{
Domain = cookie.Domain,
Name = cookie.Name,
Path = cookie.Path,
Expiry = cookie.Expiry,
Secure = cookie.Secure,
Value = cookie.Value
};
siteCookies.Cookies.Insert(0, sc);
}
lc.SharingCookie = SerializeAnObject(siteCookies);
rep.UpdateLoginCredential(lc);
}
catch (Exception)
{
throw;
}
My other classes
[Serializable]
public class SiteCookie
{
[XmlAttribute]
public string Domain { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public DateTime? Expiry { get; set; }
public bool Secure { get; set; }
public string Value { get; set; }
}
[Serializable]
public class SiteCookieList
{
public List<SiteCookie> Cookies { get; set; }
}
Im trying
public static object DeSerializeAnObject(string Xml)
{
using (StringReader strreader = new StringReader(Xml))
{
using (XmlReader xmlreader = XmlReader.Create(strreader))
{
object result = new
XmlSerializer(typeof(SiteCookieList)).Deserialize(xmlreader);
return result as SiteCookieList;
}
}
}
public static string SerializeAnObject(object AnObject)
{
XmlSerializer Xml_Serializer = new XmlSerializer(AnObject.GetType());
StringWriter Writer = new StringWriter();
Xml_Serializer.Serialize(Writer, AnObject);
return Writer.ToString();
}
private static void loadStoredCookies(IWebDriver driver, string XML)
{
try
{
dynamic siteCookies = DeSerializeAnObject(XML);
foreach (var cookie in siteCookies)
{
driver.Manage().Cookies.AddCookie(cookie);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Its wrong, late at 5am here so I need to have fresh eyes on this!
No comments:
Post a Comment