本文介绍了我怎么能刷新不失饼干我硒ChromeDriver实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在手动测试的情况下,我正在尝试使用C#中硒自动化,它说:登录使用复选框记住我启动,关闭浏览器,打开浏览器,检查用户是否是仍然记录。



手动执行,这当然是全成的。随着在Chrome硒,我总是输在会话饼干



我试了一下,到目前为止:



<$ P $ 。p> 公共静态无效RefreshDriver(TestParams testParams)
{
VAR饼干= testParams.Target.Driver.Manage()Cookies.AllCookies;
VAR URL =新的URI(testParams.Target.Driver.Url);

testParams.Target.Driver.Quit();
testParams.Target = testParams.Target.Clone(); //创建新ChromeDriver()

字符串TEMP = url.GetLeftPart(UriPartial.Authority);
testParams.Target.Driver.Navigate()GoToUrl(临时);
的foreach(在饼干曲奇饼干)
{
testParams.Target.Driver.Manage()Cookies.AddCookie(饼干)。
}
testParams.Target.Driver.Navigate()GoToUrl(URL)。
}

这是我如何创建ChromeDriver:

 私人ChromeDriver _CreateChromeDriver(字符串HTTPPROXY)
{
变种选项=新ChromeOptions();

串的tempDir = @C:\Users\Kimmy\Documents\MyUserDataDir

options.AddArgument(用户数据DIR =+的tempDir);
如果(!string.IsNullOrEmpty(HTTPPROXY))
{
options.Proxy =新代理{HTTPPROXY = HTTPPROXY};
}

返回新ChromeDriver(选件);
}

当我执行我的测试案例和 RefreshDriver(),然后在我的第一个用户登录一次。但只要我开始增加产品的购物篮,我的用户突然不能登录了。



有没有办法告诉Chrome浏览器在整个保持饼干会话,无需手动保存和恢复饼干?


解决方案

我写了使用Java编程语言中类似的计划。
请参考下面的代码。这可能是对你有帮助。
计划有目的的可读性很多评论

 公共类CookieHandling {
公众的webdriver驱动程序。

@Test
公共无效测试()抛出InterruptedException的{
驱动=新FirefoxDriver();
driver.manage()窗口()最大()。

driver.manage()超时()implicitlyWait(10 TimeUnit.SECONDS)。

driver.get(http://gmail.com/);
//登录。因此,新的会话创建
driver.findElement(By.id(电子邮件))的SendKeys(这里的电子邮件ID)。
driver.findElement(By.id(电子邮件))的SendKeys(Keys.ENTER)。
driver.findElement(By.id(的passwd))的SendKeys(邮箱密码在这里);
driver.findElement(By.id(的passwd))的SendKeys(Keys.ENTER)。

//将所有的cookies设置成allCookies参考变种。
组< Cookie和GT; allCookies = driver.manage()的getCookies()。
driver.quit(); //退出Firefox的司机
视频下载(3000);
//打开铬司机
System.setProperty(webdriver.chrome.driver,C:\\Users\\kbmst\\Desktop\\temp\\ \\\chromedriver.exe);
//System.setProperty(\"webdriver.ie.driver\",\"C:\\Users\\kbmst\\Desktop\\temp\\IEDriverServer.exe) ;
驱动=新ChromeDriver();
driver.manage()窗口()最大()。
//这是很进口再次打开相同的Web应用程序设置的cookie。
driver.get(http://www.gmail.com/);
//设置你存储的所有Cookie之前
为(Cookie的C:allCookies){
driver.manage()的addCookie(C);
}
//只需刷新浏览使用()
driver.navigate()刷新()。
}
}


In the manual test case, which I'm trying to automate in Selenium using C#, it says: "Login with checkbox 'remember me' activated, close the browser, open the browser, check if user is still logged in."

Manually executed, this is of course successfull. With Selenium in Chrome, I always lose the cookies across sessions.

What I tried so far:

public static void RefreshDriver(TestParams testParams)
{
    var cookies = testParams.Target.Driver.Manage().Cookies.AllCookies;
    var url = new Uri(testParams.Target.Driver.Url);

    testParams.Target.Driver.Quit();
    testParams.Target = testParams.Target.Clone(); // creates new ChromeDriver()

    string temp = url.GetLeftPart(UriPartial.Authority);
    testParams.Target.Driver.Navigate().GoToUrl(temp);
    foreach (Cookie cookie in cookies)
    {
        testParams.Target.Driver.Manage().Cookies.AddCookie(cookie);
    }
    testParams.Target.Driver.Navigate().GoToUrl(url);
}

This is how I create the ChromeDriver:

private ChromeDriver _CreateChromeDriver(string httpProxy)
{
    var options = new ChromeOptions();

    string tempDir = @"C:\Users\Kimmy\Documents\MyUserDataDir";

    options.AddArgument("user-data-dir=" + tempDir);
    if (!string.IsNullOrEmpty(httpProxy))
    {
        options.Proxy = new Proxy {HttpProxy = httpProxy};
    }

    return new ChromeDriver(options);
}

When I execute my test case and reach the part with RefreshDriver(), then at first my user is logged in again. But as soon as I start adding products to the shopping basket, my user is suddenly not logged in any more.

Is there a way to tell Chrome to keep the cookies across sessions, without manually saving and restoring the cookies?

解决方案

I wrote a similar program using java programming language.Please refer the below code. It may be helpful for you.Program has many comments for readability purpose.

public class CookieHandling {
public WebDriver driver;

    @Test
    public void test() throws InterruptedException{
        driver=new FirefoxDriver();
        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("http://gmail.com/");
        //Login. So new session created
        driver.findElement(By.id("Email")).sendKeys("email id here");
        driver.findElement(By.id("Email")).sendKeys(Keys.ENTER);
        driver.findElement(By.id("Passwd")).sendKeys("mail password here");
        driver.findElement(By.id("Passwd")).sendKeys(Keys.ENTER);

        //Store all set of cookies into 'allCookies' reference var.
        Set<Cookie> allCookies=driver.manage().getCookies();
        driver.quit();//Quitting the firefox driver
        Thread.sleep(3000);
        //Opening the chrome driver
        System.setProperty("webdriver.chrome.driver","C:\\Users\\kbmst\\Desktop\\temp\\chromedriver.exe");
        //System.setProperty("webdriver.ie.driver","C:\\Users\\kbmst\\Desktop\\temp\\IEDriverServer.exe");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
        //It is very import to open again the same web application to set the cookies.
        driver.get("http://www.gmail.com/");
        //Set all cookies you stored previously
        for(Cookie c:allCookies){
            driver.manage().addCookie(c);
        }
        //Just refresh using navigate()
        driver.navigate().refresh();
    }
}

这篇关于我怎么能刷新不失饼干我硒ChromeDriver实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:29