沃梦达 / 编程问答 / php问题 / 正文

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

How do I run a PHPUnit Selenium test without having a new browser window run for each function?(如何在不为每个函数运行新的浏览器窗口的情况下运行 PHPUnit Selenium 测试?)

本文介绍了如何在不为每个函数运行新的浏览器窗口的情况下运行 PHPUnit Selenium 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这篇关于如何在不为每个函数运行新的浏览器窗口的情况下运行 PHPUnit Selenium 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐