如何在不为每个函数运行新的浏览器窗口的情况下运行 PHPUnit Selenium 测试?

2022-01-25 00:00:00 selenium unit-testing php phpunit

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');
        }
    }
?>

解决方案

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.

相关文章