クッキーの取り扱い

使用webdriverをテストでは、多くの場所が着陸を使用している、クッキーは、ユーザー名とログインするためのパスワードを再入力することなく実現することができます。

最初の方法のJava Cookieクラスのいくつかを見てみましょう。

JSPで一般的に使用されるプロセスのCookieデータ:
getDomain();クッキードメイン名を返す。
GetMaxAge();クッキーの生存期間を返す
のgetName()は、クッキーの名前を返します
(ある、getPath)を、クッキー、適切なパスを返す
getSecureを();場合ブラウザは、セキュアなプロトコルクッキーの上に送り、標準プロトコルを使用してブラウザがちょうどfalseを返す場合、真の値を返します
のgetValue()は、クッキーの値を返す
GETVERSIONを();その遵守リターンプロトコルバージョンのsetComment(文字列目的)クッキー、クッキーを設定しますノート
SETPATH(文字列のURL);パスクッキーのために設定
setSecure(ブール値のフラグ);などHTTPSやSSL使用して、セキュアなプロトコルを使用してクッキーを送信するだけで、ブラウザを設定するかどうか
のsetValue(文字列newValueにする);クッキーが新しい値を設定するために作成された後
setVersionを(INT V);プロトコルバージョンに準拠するようにクッキーを設定し
、driver.manage()getCookies()とdriver.manage()addCookie(CK)によってセレンwebdriverを ... クッキークッキーをロードします

まず、保存されたbrowser.dataクッキーの中に入ります

パッケージcom.packt.webdriver.chapter3。

輸入java.io.BufferedWriter。
インポートのjava.io.File;
輸入java.io.FileWriter;

輸入java.util.concurrent.TimeUnit。

輸入org.openqa.selenium.By ;
輸入org.openqa.selenium.WebDriver;
輸入org.openqa.selenium.WebElement。
輸入org.openqa.selenium.Cookie。

パブリッククラスクッキー{
/ **
* @authorヤング
*
* /
パブリック静的な無効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");
    }
}

}
,,,,,,,,,,,,,,,,,,,,,,,,
インポートjava.util.Arrays。

輸入org.openqa.selenium.WebDriver;
輸入org.openqa.selenium.chrome.ChromeDriver;
輸入org.openqa.selenium.chrome.ChromeOptions。
輸入org.openqa.selenium.remote.DesiredCapabilities。

パブリッククラス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;
}

}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
パッケージcom.packt.webdriver.chapter3。

輸入java.io.BufferedReader;

インポートのjava.io.File;
輸入java.io.FileReader。

輸入java.util.Date;
輸入java.util.StringTokenizer。

輸入org.openqa.selenium.Cookie。
輸入org.openqa.selenium.WebDriver;
輸入org.openqa.selenium.os.WindowsUtils。

パブリッククラス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/");
}

}

おすすめ

転載: blog.csdn.net/qq_35577329/article/details/93736863