Selenium tests won't save cookies?

So, I'm experimenting with Selenium automation, and I'm trying to write a test file that logs into the system, goes to a specific page, enters the data, and then clicks submit. The problem is that when it starts, it displays the credentials, clicks "Submit", which returns the site:

This site uses HTTP cookies to verify authorization information. Enable HTTP cookies to continue.

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

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

This allowed us to register until it gets to the message sending page [indicated by // 2], it again asks for credentials (as if no entry was made). So does firefox not accept cookies at all? How to fix it?

A 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; } } } 
+6
source share
2 answers

Depending on your problem, the problem you are facing is that selenium opens a new firefox profile in which cookies are not enabled.

driver = new FirefoxDriver(); Here you have to fix it so that it opens a profile in which cookies are allowed. One way is to create your own profile in firefox and open that profile instead of directly opening FirefoxDriver ();

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

Thus, you can do what you need in this profile and run tests in these settings. If you enable cookies, do this in the firefox settings.

Below is another way to open a specific profile according to 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 out the source for more information on this topic. Source: http://docs.seleniumhq.org/docs/03_webdriver.jsp#modifying-the-firefox-profile

+8
source

I really realized that I defined the base URL as

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

and he concatenated it like this:

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

for " https://my_url.com//Login ". Thanks for your replay!

+1
source

Source: https://habr.com/ru/post/944802/


All Articles