Java Selenium测试运行赢得';你不能保存饼干吗?
所以我正在试验Selenium自动化,我正在尝试编写一个测试用例,它可以登录,进入特定页面,输入数据,然后按submit。问题是,当它运行时,它会键入凭据,按“提交”,然后站点返回: 此站点使用HTTP Cookie验证授权信息。 请启用HTTP Cookie以继续 但是当我添加这一行[由//1表示]时:Java Selenium测试运行赢得';你不能保存饼干吗?,java,firefox,cookies,selenium,junit,Java,Firefox,Cookies,Selenium,Junit,所以我正在试验Selenium自动化,我正在尝试编写一个测试用例,它可以登录,进入特定页面,输入数据,然后按submit。问题是,当它运行时,它会键入凭据,按“提交”,然后站点返回: 此站点使用HTTP Cookie验证授权信息。 请启用HTTP Cookie以继续 但是当我添加这一行[由//1表示]时: driver.findElement(By.cssSelector("p > input[type=\"submit\"]")).click(); 它允许登录一直进行到它到达发送消息页
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正在打开一个新的firefox配置文件,其中未启用Cookie
driver=新的FirefoxDriver()代码>
这是您必须以这样的方式进行修复的地方:它会打开一个启用cookie的配置文件。
一种方法是在firefox中创建自己的配置文件并打开该配置文件,而不是直接由FirefoxDriver()打开
通过这种方式,您可以在该概要文件中执行任何需要的设置,并在该设置下运行测试。如果需要启用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);
有关此主题的更多信息,请查看来源。
来源:我实际上意识到我将基本URL定义为
baseUrl = "https://my_url.com/";
它将它连接起来,就像:
driver.get(baseUrl + "/Login");
对于“”而言。谢谢你的重播 你试过在那里添加一些等待吗?+1谢谢-这非常简单,我已经尝试了几天其他方法来解决这个问题:)要在Windows中查看/创建配置文件,你可以找到firefox.exe。在firefox.exe的位置打开一个命令提示符,然后运行firefox.exe-ProfileManager打开一个用于管理配置文件的小窗口。提示:firefox通常位于“C:\Program Files(x86)\Mozilla firefox\firefox.exe”或“C:\Program Files\Mozilla firefox\firefox.exe”中。打开文件夹后,按住shift键并右键单击白色区域,然后选择“在此处打开命令窗口”。在佛陀的回答中,它将查找名为“您的配置文件”的配置文件。
driver.get(baseUrl + "/Login");