Selenium测试运行不会保存cookies吗?(Selenium test runs won&#

2019-09-02 16:59发布

所以我使用Selenium自动化实验,而且我想写登录后,进入到一个特定的页面,输入数据的测试用例,然后按下提交。 问题是,当它运行时,它的类型了凭据,按“提交”网站返回:

本网站使用HTTP cookie来验证授权信息。 请启用HTTP cookies来继续。

但是当添加此线[表示由// 1]:

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

它允许登录,直到它到达发送消息页面[由// 2记]经历,它要求再次凭据(好像没有登录什么时候进行过)。 所以火狐不接受所有Cookie吗? 我该如何解决?

资源:

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;
        }
    }
}

Answer 1:

根据您的问题声明,你所面临的问题是,硒打开一个新的Firefox配置文件尚未启用的cookie。

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



Answer 2:

其实,我意识到,我所定义的基本URL作为

    baseUrl = "https://my_url.com/";

和它相连来,如:

    driver.get(baseUrl + "/Login");

对于“ https://my_url.com//Login ”。 感谢您的重播,但!



文章来源: Selenium test runs won't save cookies?