Bryan Miller
Bryan Miller

Reputation: 3323

Selenium browser window size

I'm new at using selenium. I have downloaded the Selenium Standalone Server and successfully got it running by typing

java -jar selenium-server-standalone-2.14.0.jar

into the command line.

After running

 phpunit.bat

from the command line, all of my tests will pass as expected except for one unless I manually maximize the Firefox browser window that automatically is opened during the testing process.

While the tests are running, when the Firefox browser window opens, if I don't maximize that window while my login test is running, then that test will fail and somehow redirect to a unexpected page on my website. If I do maximize the window before the test completes, the "login" button gets clicked as expected, the correct page gets loaded, and the test passes.

Thus, I'm wondering if there is a way to change the settings somewhere so that the Firefox browser would just open maximized while the tests are running?

I've Googled around and discovered some various code snippets that might help, but I'm not sure where the PHP version of this code is or where to put some similar code for the version of Selenium I'm using (the Selenium Standalone Server):

# repositioning and resizing browser window:
driver.manage.window.move_to(300, 400)
driver.manage.window.resize_to(500, 800)
driver.manage.window.maximize

or this which is C#, but I need PHP and don't know where to find the right code or where to put it:

driver.Manage().Window.Maximize();   

Here is the code for the login Selenium test in question (using Laracasts/Integrated) from my tests/SeleniumTest.php file:

<?php

use Laracasts\Integrated\Extensions\Selenium;
use Laracasts\Integrated\Services\Laravel\Application as Laravel;

class SeleniumTest extends Selenium
{
    use Laravel;
    /**
     * Tests to see if the login page loads
     */
    public function testToSeeIfLoginLoads()
    {
        $this->visit('/login')
            ->see('Login')->see('Email Address')->see('Password')
            ->type('[email protected]', 'email')->type('mypassword', 'password')
            ->waitForElement('log_in')
           ->click('log_in')
            ->waitForElement('table_summary')
            ->see('Complete Course Summary');
    }

Upvotes: 2

Views: 4227

Answers (2)

Cartitza
Cartitza

Reputation: 96

I'm not using Laravel, I'm using classic phpunit + selenium RC and/or webdriver and this works for me on both implementations, right after opening the url, if I only want this for a specific test:

$this->open("/");
$this->getEval("window.resizeTo(1225, 996); window.moveTo(0,200);");
//or $this->windowMaximize();

or for facebook-webdriver:

$this->driver->get('myurl/');
$this->driver->manage()->window()->setSize(new WebDriverDimension(1225, 996));

If you want it for all tests, you should find the setUp method and put it there. In your case, for a specific test, this should work:

$this
  ->visit('/')->session->window($this->session->window_handle())
  ->postSize(['height' => 996, 'width' => 1225]);

To import WebDriverDimensions add

use Facebook\WebDriver\WebDriverDimension;

More info on usage, here.

Upvotes: 8

sraje
sraje

Reputation: 376

Have you tried overriding the setUpPage function?

public function setUpPage()
    {
         $this->currentWindow()->maximize();
    }

Upvotes: 2

Related Questions