Cookie handling

In use webdriver tests, many places are using the landing, cookie can be achieved without having to re-enter the user name and password to login.

First look at some of the ways Java Cookie class.

In jsp commonly used processes cookie data:
getDomain (); return the cookie domain name.
GetMaxAge (); return cookie survival time
getName (); Returns the name of the cookie
getPath (); return cookie suitable path
getSecure (); if the browser sends over a secure protocol cookie will return true value, if the browser using the standard protocol just return false
getValue (); return the cookie value
getVersion (); return protocol version setComment (String purpose) cookie that compliance; set a cookie Notes
setPath (String url); setting for path cookie's
setSecure (Boolean flag); whether to set the browser only to send the cookie using a secure protocol, such as using Https or ssl
setValue (String newValue); after the cookie is created to set a new value
setVersion (int v); set the cookie to comply with the protocol version
selenium WebDriver by driver.manage () getCookies () and driver.manage () addCookie (ck); .. get loaded cookie cookie

First, get inside browser.data cookie saved

package com.packt.webdriver.chapter3;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Cookie;

public class Cookies {
/**
* @author Young
*
*/
public static void addCookies() {

    WebDriver driver = DriverFactory.getChromeDriver();
    driver.get("http://www.zhihu.com/#signin");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    WebElement user = driver
            .findElement(By.xpath("//input[@name='email']"));
    user.clear();
    user.sendKeys("[email protected]");
    WebElement password = driver.findElement(By
            .xpath("//input[@name='password']"));
    password.clear();
    password.sendKeys("cookies123");

    WebElement submit = driver.findElement(By
            .xpath("//button[@class='sign-button']"));
    submit.submit();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    File file = new File("broswer.data");
    try {
        // delete file if exists
        file.delete();
        file.createNewFile();
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);
        for (Cookie ck : driver.manage().getCookies()) {
            bw.write(ck.getName() + ";" + ck.getValue() + ";"
                    + ck.getDomain() + ";" + ck.getPath() + ";"
                    + ck.getExpiry() + ";" + ck.isSecure());
            bw.newLine();
        }
        bw.flush();
        bw.close();
        fw.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.out.println("cookie write to file");
    }
}

}
、、、、、、、、、、、、、、、、、、、、、、、、
import java.util.Arrays;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class DriverFactory {

public static WebDriver create() {
    
    // TODO Auto-generated method stub
    String chromdriver="E:\\chromedriver.exe";
    System.setProperty("webdriver.chrome.driver", chromdriver);
    ChromeOptions options = new ChromeOptions();
 
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability("chrome.switches",
            Arrays.asList("--start-maximized"));
    options.addArguments("--test-type", "--start-maximized");
    WebDriver driver=new ChromeDriver(options);
    return driver;
}

}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
package com.packt.webdriver.chapter3;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;

import java.util.Date;
import java.util.StringTokenizer;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.os.WindowsUtils;

public class UseCookieLogin {

/**
 * @author Young
 * @param args
 */

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Cookies.addCookies();
    WindowsUtils.tryToKillByName("chrome.exe");
    WindowsUtils.getProgramFilesPath();
    WebDriver driver=DriverFactory.getChromeDriver();
    driver.get("http://www.zhihu.com/");
    try 
    {
        File file=new File("broswer.data");
        FileReader fr=new FileReader(file);
        BufferedReader br=new BufferedReader(fr);
        String line;
        while((line=br.readLine())!= null)
        {
            StringTokenizer str=new StringTokenizer(line,";");
            while(str.hasMoreTokens())
            {
                String name=str.nextToken();
                String value=str.nextToken();
                String domain=str.nextToken();
                String path=str.nextToken();
                Date expiry=null;
                String dt;
                if(!(dt=str.nextToken()).equals(null))
                {
                    //expiry=new Date(dt);
                    System.out.println();
                }
                boolean isSecure=new Boolean(str.nextToken()).booleanValue();
                Cookie ck=new Cookie(name,value,domain,path,expiry,isSecure);
                driver.manage().addCookie(ck);
            }
        }
        
        
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    driver.get("http://www.zhihu.com/");
}

}

Guess you like

Origin blog.csdn.net/qq_35577329/article/details/93736863