How can I refresh my Selenium ChromeDriver instanc

2019-06-04 08:26发布

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?

1条回答
贪生不怕死
2楼-- · 2019-06-04 09:02

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();
    }
}
查看更多
登录 后发表回答