How do I run a PHPUnit Selenium test without having a new browser window run for each function?

ぃ、小莉子 提交于 2019-12-19 08:05:01

问题


I am trying to run a selenium test case using PHPUnit. And the first thing I do is trying the login function, this works perfect but then I want to run a function to check information on the page following the login but it opens a new browser instead of continuing in the current browser window. The reason this is a problem is because the page is setup to remove login authentication when the window is closed so if you use $this->url() to go to the page it gives the error that I need to login. This is my code right now, It starts the browser and runs the function to test the login form, then it closes the browser, open a new one and run the link check. This of course results in an error due to the authentication error because the window was closed. I could run all the tests in one function but that is really sloppy coding and I want to avoid this. Anyone know how to solve this?

<?php
    class TestMyTest extends PHPUnit_Extensions_Selenium2TestCase {
        public function setUp()
        {
            $this->setBrowser("firefox");
            $this->setBrowserUrl("https://**************************");
        }

        public function testLoginForm()
        {

            $this->url("login.php");
            $this->byLinkText('Forgot your password?');
            $form = $this->byCssSelector('form');
            $this->byName('username')->value('test');
            $this->byName('password')->value('1234');
            $form->submit();
        }


        public function testCheckForMainMenueLinks ()
        {
            $this->url("index.php");
            $this->byLinkText('Home');
            $this->byLinkText('Products');
            $this->byLinkText('About us');
            $this->byLinkText('Contact');
        }
    }
?>

回答1:


To share browser sessions in Selenium2TestCase, you must set sessionStrategy => 'shared' in your initial browser setup:

public static $browsers = array(
    array(
        '...
        'browserName' => 'iexplorer',
        'sessionStrategy' => 'shared',
        ...
    )
);

The alternative (default) is 'isolated'.




回答2:


Okej so I guess you can just call the function directly from another function like so:

public function testOne
{
#code
$this->Two();
}

public function Two()
{
#code
$this->Three();
}

public function Three()
{
#code
}

and so on, this will just run the next function without a new browser, however, if it fails anywhere in any test the whole test is stoped so the feedback wont bee as good as individual tests.




回答3:


make assetrions in one function because this is functional test. i am new to phpunit and selenium too, but I successfully test all in one like this:

public function testAuth(){  

$this->open('register.php&XDEBUG_SESSION_START=PHPSTORM');
$this->assertTextPresent('Register');
$this->type('name=email', "...");
$this->type('name=firstname', "...");
$this->type('name=lastname', "...");       
$this->type('name=password', "...");
$this->type('name=verifyPassword', "...");
$this->click("reg-butt");
$this->waitForPageToLoad("5000");
$this->assertTextPresent('Profile');
$this->open('logout.php');
$this->assertTextPresent('text from redirect page');
$this->open('login.php');
.....

}



回答4:


An elegant way to set the session shared is to use PHPUnit's setUpBeforeClass() method:

public static function setUpBeforeClass()
{
    self::shareSession(true);
}



回答5:


You can call PHPUnit_Extensions_SeleniumTestCase::shareSession(true) to enable browser window reuse.

In the manual it says:

From Selenium 1.1.1, an experimental feature is included allowing the user to share the session between tests. The only supported case is to share the session between all tests when a single browser is used. Call PHPUnit_Extensions_SeleniumTestCase::shareSession(true) in your bootstrap file to enable session sharing. The session will be reset in the case of not successul tests (failed or incomplete); it is up to the user to avoid interactions between tests by resetting cookies or logging out from the application under test (with a tearDown() method).



来源:https://stackoverflow.com/questions/16734237/how-do-i-run-a-phpunit-selenium-test-without-having-a-new-browser-window-run-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!