在Selenium中,如何使用现有的Cookie打开Firefox浏览器 [英] In Selenium, How can I open Firefox browser but with the existing cookie

查看:874
本文介绍了在Selenium中,如何使用现有的Cookie打开Firefox浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个硒测试,该测试具有以下步骤:

I am trying to create a selenium test which is having below step:

  1. 登录到Google页面.
  2. 通过输入有效凭据成功登录
  3. 页面将在此处显示收件箱
  4. 直接点击浏览器右上角的关闭"按钮来关闭浏览器.
  5. 重复步骤1

在此测试用例中,我希望Google步骤第5步后不再要求提供凭据,而是直接移至收件箱页面.如何使用Selenium Webdriver做到这一点?

In this test case I am expecting that after 5th step google page do not ask for credentials again and moves to inbox page directly. How can do this using selenium webdriver?

推荐答案

这应该为您提供所需的东西.使用driver.manage().getCookies();

This should give you what you need. Copy the cookies from the first driver instance into the new driver instance using driver.manage().getCookies();

FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
//Passing valid credentials
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("testuser@gmail.com");
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("password");
driver.findElement(By.xpath("//*[@id='signIn']")).click();
Thread.sleep(20000);
Set<Cookie> cookies = driver.manage().getCookies();
driver.close();
//Starting new browser 
driver = new FirefoxDriver();
for(Cookie cookie : cookies)
{
    driver.manage().addCookie(cookie);
}
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
Thread.sleep(20000);
driver.quit();

这篇关于在Selenium中,如何使用现有的Cookie打开Firefox浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆