MDL-50163 behat: Return subcontext from session

Creating a new context with new mink context
with different session was keeping the session
and was not returning proper session information
This commit is contained in:
Rajesh Taneja 2015-11-17 12:59:06 +08:00
parent 131d4ac2b0
commit 8f76de32d9
2 changed files with 11 additions and 48 deletions

View File

@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper to initialise behat contexts from moodle code.
* Helper to get behat contexts from other contexts.
*
* @package core
* @category test
@ -25,8 +25,7 @@
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
use Behat\Mink\Session as Session,
Behat\Mink\Mink as Mink;
use \Behat\Behat\Context\BehatContext;
/**
* Helper to get behat contexts.
@ -39,28 +38,18 @@ use Behat\Mink\Session as Session,
class behat_context_helper {
/**
* List of already initialized contexts.
*
* @var array
* @var BehatContext main behat context.
*/
protected static $contexts = array();
protected static $maincontext = false;
/**
* @var Mink.
*/
protected static $mink = false;
/**
* Sets the browser session.
* Save main behat context reference to be used for finding sub-contexts.
*
* @param Session $session
* @param BehatContext $maincontext
* @return void
*/
public static function set_session(Session $session) {
// Set mink to be able to init a context.
self::$mink = new Mink(array('mink' => $session));
self::$mink->setDefaultSessionName('mink');
public static function set_main_context(BehatContext $maincontext) {
self::$maincontext = $maincontext;
}
/**
@ -76,36 +65,10 @@ class behat_context_helper {
*/
public static function get($classname) {
if (!self::init_context($classname)) {
if (!$subcontext = self::$maincontext->getSubcontextByClassName($classname)) {
throw coding_exception('The required "' . $classname . '" class does not exist');
}
return self::$contexts[$classname];
return $subcontext;
}
/**
* Initializes the required context.
*
* @throws coding_exception
* @param string $classname
* @return bool
*/
protected static function init_context($classname) {
if (!empty(self::$contexts[$classname])) {
return true;
}
if (!class_exists($classname)) {
return false;
}
$instance = new $classname();
$instance->setMink(self::$mink);
self::$contexts[$classname] = $instance;
return true;
}
}

View File

@ -256,7 +256,7 @@ class behat_hooks extends behat_base {
// We need the Mink session to do it and we do it only before the first scenario.
if (self::is_first_scenario()) {
behat_selectors::register_moodle_selectors($session);
behat_context_helper::set_session($session);
behat_context_helper::set_main_context($event->getContext()->getMainContext());
}
// Reset mink session between the scenarios.