Selenium:cookie功能不起作用

时间:2016-04-07 14:25:49

标签: selenium

我是硒的新手。我正在尝试测试一个应用程序。应用程序有两个页面login.jsp,restricted.jsp。您只能在登录后访问restricted.jsp(尝试访问restricted.jsp而不登录将重定向到login.jsp页面)。我的硒应用如下所示。

一个。首先登录该应用

湾成功登录后,将所有cookie存储到" session.properties"文件。

℃。下一次,我正在加载来自" session.properties"的所有cookie。驱动程序并尝试访问" restricted.jsp"页。但我正在重定向到login.jsp,而不是restricted.jsp。

以下是我的Java代码。

public class App {

 private static void loginApp(WebDriver driver) {
    driver.get("http://localhost:8080/selenium_app/login");

    WebElement userName = driver.findElement(By.name("userName"));
    WebElement password = driver.findElement(By.name("password"));

    userName.sendKeys("admin");
    password.sendKeys("admin");

    userName.submit();
}

private static void storeSessionProps(WebDriver driver) throws IOException {
    File f = new File("session.properties");
    f.delete();
    f.createNewFile();

    FileWriter fos = new FileWriter(f);
    BufferedWriter bos = new BufferedWriter(fos);

    /* Get all the cookies and store them to session.properties file */
    Set<Cookie> cookies = driver.manage().getCookies();
    for (Cookie cookie : cookies) {
        bos.write(cookie.getName() + "=" + cookie.getValue());
        bos.newLine();
    }

    bos.flush();
    bos.close();
    fos.close();
}

private static void loadPropertiesToDriver(WebDriver driver)
        throws IOException {
    Properties properties = new Properties();
    FileInputStream fin = new FileInputStream("session.properties");

    properties.load(fin);

    Set<Object> props = properties.keySet();

    for (Object prop : props) {
        Cookie ck = new Cookie((String) prop,
                properties.getProperty((String) prop));
        driver.manage().addCookie(ck);
        System.out.println(ck);
    }
}

public static void main(String[] args) throws InterruptedException,
        IOException {
    WebDriver driver = new FirefoxDriver();

    // loginApp(driver);
    // storeSessionProps(driver);

    loadPropertiesToDriver(driver);
    driver.get("http://localhost:8080/selenium_app/restricted");
    Thread.sleep(5000);

    driver.quit();
}

}

当我取消注释loginApp(driver);, storeSessionProps(driver);一切都很好,我能够访问restricted.jsp页面,但是当我通过评论那些并加载cookie来运行应用程序时,我将重定向到login.jsp页面。对此有何帮助??

1 个答案:

答案 0 :(得分:1)

您需要存储Cookie中的所有数据,而不仅仅是名称/值。此外,在创建cookie之前,您需要加载一个包含与cookie域相匹配的域的页面。

这是一个快速存储和恢复cookie的示例:

Path cookiesFile = Paths.get("C:\\Temp\\cookies.txt");

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;

// load the domain
driver.get("https://www.google.com");

if(cookiesFile.toFile().exists()) {

    // load the cookies into the browser for the current domain
    String cookies = new String(Files.readAllBytes(cookiesFile), Charsets.UTF_8);
    js.executeScript(cookies);

    // reload the page with the injected cookies
    driver.get("https://www.google.com");
}

// save the cookies to a file for the current domain
try(PrintWriter file = new PrintWriter(cookiesFile.toFile(), "UTF-8")){
    for(Cookie c : driver.manage().getCookies()) {
        file.println("document.cookie='" + c.toString() + "';");
    }
}