0

Hi I want to login using cookies in selenium webdriver I first logged in to site using test script 1 and then trying to login using stored cookies using test script 2 It is not working Below is the code :

script 1)

     //save login credentials in cookies
    System.setProperty("webdriver.chrome.driver","D:\\selenium\\chromedriver\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    WebDriver driver = new ChromeDriver(options);   

    driver.get("https://sqa.stackexchange.com/users/login?ssrc=head");


    driver.findElement(By.id("email")).sendKeys("xxx@xxx.com");
    driver.findElement(By.id("password")).sendKeys("xxxx");
    driver.findElement(By.id("submit-button")).click();


    try {
    File f= new File ("D:\\browser.txt");
        FileWriter fs= new FileWriter(f);
        BufferedWriter  bw= new BufferedWriter(fs);

        Set<Cookie> myCookies =driver.manage().getCookies();
        for(Cookie ck:myCookies)
        {

            bw.write(ck.getName()+";"+ck.getValue());
             bw.flush();
        }
        bw.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

Script 2)

    try {

    File f= new File ("D:\\browser.txt");
    FileReader fr= new FileReader(f);

    BufferedReader  br= new BufferedReader(fr);

    String sCurrentLine;

    while (( sCurrentLine = br.readLine()) != null) 
    {
        String[] strArr = sCurrentLine.split(";");


            for (int i=0;i< strArr.length;)
            {

                System.out.println(i);
                //System.out.println(strArr[i]);

                    String name= strArr[i];
                    //System.out.println(name);
                    String value= strArr[i++];
                    i++;

                    Cookie ckNew = new Cookie(name, value);

                    @SuppressWarnings("deprecation")
                    Cookie cookie = new Cookie.Builder(name,value)
                    .domain("https://sqa.stackexchange.com/")
                    .expiresOn(new Date(2017, 31,12))
                    .isHttpOnly(true)
                    .isSecure(true)
                    .build();
                    driver.manage().addCookie(ckNew);
                    //System.out.println(strArr.length);
                    //Set<Cookie> myCookies =driver.manage().getCookies();
                    //for(Cookie ck:myCookies)
                    //System.out.println(ck.getName()+";"+ck.getValue());
            }       
    }           
    } catch (IOException e)
    {

    e.printStackTrace();
    }
driver.get("https://sqa.stackexchange.com/users/login?ssrc=head");
  • We need specifics of your site including how the authentication works and is stored. Sometimes it's just a cookie and other times there are various factors in authentication and you may not have all the pieces, but from the description all we can see is you took cookies and then it's not working. No information with the application specifically and the underneath of how it works. – mutt Apr 3 '17 at 17:23
  • It is just a example code I am just trying to store email and password in cookies – gayatri Apr 4 '17 at 5:51
  • Examples are nice for understanding, but to automate authentication the way you are going we need hard specifics to the application under test to be sure the authentication is correct. – mutt Apr 4 '17 at 14:15
  • I am getting exception as Failed to set the 'cookie' property on 'Document': Cookies are disabled inside 'data:' URLs. when i tried driver.manage().addCookie(ckNew); – gayatri Apr 11 '17 at 5:46
  • so cookies for either the browser or security cookies are locked to outside access. I'm guessing probably the browser cookies are enabled? That might be a protected security cookie allowing only browser access and not third party access... – mutt Apr 11 '17 at 11:51
1

In general handling of cookies is not required for functional testing.

Selenium will run each script in new private window. So the cookies are limited to that script or browser window. Once the session is closed then it is not possible to use cookies for next session

Cookies will be used if you are navigating to different pages from same browser window.

So perform all operations in one script then no need to handle cookies.

  • can you explain "In general handling of cookies is not required for functional testing." ? – Rsf Apr 7 '17 at 9:17
  • In functional test browser is opened, so it will handle when we navigate to other pages. Where as in performance testing the browser won't open so we need to handle cookies on our own – Karthikeya Apr 7 '17 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.