Selenium测试运行不会保存Cookie? [英] Selenium test runs won't save cookies?

查看:325
本文介绍了Selenium测试运行不会保存Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


验证授权信息。
请启用HTTP cookie继续。


但是当我添加这行[表示为// 1]:

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

它允许登录通过,直到它到达发送消息页面[表示为// 2 ],它再次请求凭据(如果没有登录)。所以是firefox不接受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\]))。
// 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\]))。
driver.findElement(By.id(apin_id))。sendKeys(User.getUserPIN());
driver.findElement(By.cssSelector(div.pagebodydiv> form> input [type = \submit\]))。

// 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]))。
}

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

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

私人布尔值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 = new FirefoxDriver();
这是您必须以这种方式修复它打开一个配置文件,其中启用Cookie。
一种方法是在firefox中创建自己的配置文件并打开该配置文件而不是直接打开FirefoxDriver();

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

这样,您可以在该配置文件中进行所需的设置,并在该设置下运行测试。



以下是根据seleniumhq.org打开特定个人资料的另一种方法。

 文件profileDir =新文件(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


So I'm experimenting with Selenium automation, and I'm trying to write a testcase that logs in, goes to a specific page, inputs data, then presses submit. The issue is that when it runs, it types out the credentials, presses "Submit" the site returns:

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

But then when I added this line [denoted by //1]:

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

It allowed the login to go through until it gets to the send message page [denoted by //2], it asks for credentials again (as if no login was ever made). So is firefox not accepting cookies at all? How do I fix this?

Source:

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

解决方案

Based on your problem statement, the problem you are facing is that selenium is opening a fresh firefox profile where cookies are not enabled.

driver = new FirefoxDriver(); This is where you have to fix in such a way that it opens up a profile where cookies are enabled. One way is to create your own profile in firefox and open that profile instead of directly opening by FirefoxDriver();

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

This way you can do what ever setting you need to in that profile and run your tests under that settings. If enabling cookies is the need, do that in firefox options.

Following is another way to open a specific profile as per 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);

Check the source for more info on this topic. Source: http://docs.seleniumhq.org/docs/03_webdriver.jsp#modifying-the-firefox-profile

这篇关于Selenium测试运行不会保存Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆