java - Selenium 测试运行不会保存 cookie?

标签 java firefox cookies selenium junit

所以我正在试验 Selenium 自动化,我正在尝试编写一个测试用例来登录、转到特定页面、输入数据,然后按下提交。问题是当它运行时,它会输入凭据,按“提交”,网站返回:

This site uses HTTP cookies to verify authorization information. Please enable HTTP cookies to continue.

但是当我添加这一行时 [由//1 表示]:

driver.findElement(By.cssSelector("p > input[type=\"submit\"]")).click();

它允许登录通过,直到它到达发送消息页面 [由//2 表示],它再次请求凭据(就好像从未进行过登录一样)。那么 firefox 是不是根本不接受 cookies 呢?我该如何解决这个问题?

来源:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class LaPwn {
    private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();
    private String UserID = "";
    private String UserPW = "";
    private String UserPIN = "";

    public static void main(String[] args) throws Exception {

        UserInfo User = new UserInfo();

        User.setUserInfo();

        System.out.println(User.getUserID());
        System.out.println(User.getUserPW());
        System.out.println(User.getUserPIN());


        JUnitCore.main("LaPwn");
    }

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "https://my_url.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testLaPwn() throws Exception {
        driver.get(baseUrl + "/Login");
        //1
        driver.findElement(By.cssSelector("p > input[type=\"submit\"]")).click();
        //1
        driver.findElement(By.id("UserID")).clear();
        driver.findElement(By.id("UserID")).sendKeys("User.getUserID()");
        driver.findElement(By.name("PIN")).clear();
        driver.findElement(By.name("PIN")).sendKeys("User.getUserPW()");
        driver.findElement(By.cssSelector("p > input[type=\"submit\"]")).click();
        driver.findElement(By.id("apin_id")).sendKeys("User.getUserPIN()");
        driver.findElement(By.cssSelector("div.pagebodydiv > form > input[type=\"submit\"]")).click();

        //2
        driver.get(baseUrl + "/messagecenter");
        //2
        try {
            assertEquals("Send message:", driver.getTitle());
        } catch (Error e) {
            verificationErrors.append(e.toString());
        }
        driver.findElement(By.id("user")).clear();
        driver.findElement(By.id("user")).sendKeys("test123");
        driver.findElement(By.id("messg")).clear();
        driver.findElement(By.id("messg")).sendKeys("Hello test123!");
        driver.findElement(By.xpath("(//input[@name='SEND_BTN'])[2]")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}

最佳答案

根据您的问题陈述,您面临的问题是 selenium 正在打开一个未启用 cookie 的新 firefox 配置文件。

driver = new FirefoxDriver(); 这是您必须以打开启用 cookie 的配置文件的方式进行修复的地方。 一种方法是在 firefox 中创建您自己的配置文件并打开该配置文件,而不是直接通过 FirefoxDriver() 打开;

ProfilesIni profileObj = new ProfilesIni();
FirefoxProfile yourFFProfile = profileObj.getProfile("your profile");
driver = new FirefoxDriver(yourFFProfile);

这样您就可以在该配置文件中进行任何需要的设置,并在该设置下运行您的测试。如果需要启用 cookie,请在 firefox 选项中执行此操作。

以下是根据 seleniumhq.org 打开特定配置文件的另一种方法

File profileDir = new File("path/to/top/level/of/profile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.addAdditionalPreferences(extraPrefs);
WebDriver driver = new FirefoxDriver(profile);

查看来源以获取有关此主题的更多信息。 来源:http://docs.seleniumhq.org/docs/03_webdriver.jsp#modifying-the-firefox-profile

关于java - Selenium 测试运行不会保存 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16504038/

相关文章:

javascript - getitem 本地存储不工作

javascript - 评估 : odd session management in web-based application

java - Eclipse 不启动 (Ubuntu) : JVM terminated. 退出代码=2

java - 使用 application.properties 中的旧值

javascript - XUL 文本框 addEventListener

javascript - 隐藏所有站点页面上的元素

java - 运行 google 端点 api 资源管理器时出错

java - 哪里需要同步?

javascript - 将 Firefox 小书签加载为普通 URL

javascript - 如何在 Chrome 的 "developer bar"控制台中获得智能感知功能?