Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create cookie before open() in Selenium

This code works only in *googlechrome.

$this->browserBot->setCommandLineFlags('commandLineFlags=--disable-web-security');
$this->browserBot->setBrowser('*googlechrome');
$this->browserBot->setHost('localhost');
$this->browserBot->setPort(4444);

$this->browserBot->setBrowserUrl('http://example.com');
$this->browserBot->start();
$this->browserBot->createCookie('foo=bar', 'path=/; domain=.example.com');
$this->browserBot->open('http://example.com/print_cookie.php');

In *firefox and *iexplore works only this:

$this->browserBot->start();
$this->browserBot->open('http://example.com/blank_page.html');
$this->browserBot->createCookie('foo=bar', 'path=/; domain=.example.com');
$this->browserBot->open('http://example.com/print_cookie.php');

Can I create cookie before open() (without redundant open() call) in *firefox and etc.?

like image 303
Mikhail Avatar asked Jun 15 '11 12:06

Mikhail


People also ask

How do you make cookies in Selenium?

To use the Selenium Cookie API with a Selenium Grid, you can use RemoteWebdriver and call the Selenium Cookie methods on the RemoteWebDriver object. Instead of running your Selenium tests on your local computer, you might want to run tests on a (Cloud) Selenium grid.

Can we handle cookies in Selenium?

Importance of Cookie Handling in Selenium Automation WebDriver. When testing a web application using Selenium Webdriver, testers can create, update or delete a cookie.

Which method is used to access cookies Selenium?

Using Selenium WebDriver API, we can query and interact with browser cookies with the help of following WebDriver's built-in methods. Get Cookies: This statement is used to return the list of all Cookies stored in web browser. manage(). getCookies();

What is returned by driver () Manage ()?

driver. manage() is a method that returns instance of options interface, now the options interface has method window() that returns instance of window interface, this window interface has method maximize() which maximizes the window.


1 Answers

This all depends on what is injected into the browser at each time.

The 2nd way that you documented is the way I would do it to make sure it worked over multiple browser versions. I don't think that you can do this the first way for every browser.

like image 199
AutomatedTester Avatar answered Oct 04 '22 23:10

AutomatedTester