您好。我正在尝试运行以下代码和测试用例,例如
随后,执行代码以验证会话是否仍然存在。
public void test()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://site/login");
this.login();
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://site/homepage");
this.CheckSessionExist(); /// reutrn false as the session is lost
}
我的问题是:在初始化另一个驱动程序实例后,我不应该能够检索会话吗?如果这不是它的工作原理,那么我该如何复制这个测试用例?
答案 0 :(得分:1)
建议:
public void test()
{
IWebDriver driver = new ChromeDriver(); // This opens a window
driver.Navigate().GoToRul("https://www.google.com"); // Navigate to a dummy url, this is to keep the chrome driver session alive later
IWebElement body = driver.FindElement(By.CssSelector("body"));
body.SendKeys(Keys.CONTROL + 't'); // This opens a new window
String winHandleBefore = driver.CurrentWindowHandle();
//Switch to new window opened
foreach(String winHandle in driver.WindowHandles{
if(winHandle !- winHandleBefore)
driver.SwitchTo().Window(winHandle);
}
driver.Navigate().GoToUrl("http://site/login");
this.login();
driver.Close(); // This closes the current window, but should keep the ChromeDriver session alive
driver.SwitchTo().Window(winHandleBefore);
IWebElement body = driver.FindElement(By.CssSelector("body"));
body.SendKeys(Keys.CONTROL + 't'); // This opens a new window
String winHandleBefore = driver.CurrentWindowHandle();
//Switch to new window opened
foreach(String winHandle in driver.WindowHandles{
if(winHandle !- winHandleBefore)
driver.SwitchTo().Window(winHandle);
}
driver.Navigate().GoToUrl("http://site/homepage");
this.CheckSessionExist(); /// reutrn false as the session is lost
}
这里的想法是你
ChromeDriver
会话保持活动状态可能过于复杂,可能存在更好的解决方案。
答案 1 :(得分:0)
在关闭浏览器之前提取cookie,打开新浏览器,导航到同一个域并删除cookie