I am using protractor to test an angular app which I configured to restart the browser after each test in conf file. However it leads to session errors. I was able to trace this to a helper class I setup that uses expected conditions.
'use strict';
let EC = browser.ExpectedConditions;
let timeOut = 30000;
class ProHelper {
constructor() {}
async waitForUrl(urlSubstring, time = timeOut) {
let isMatchingUrl = EC.urlContains(urlSubstring);
await browser.wait(isMatchingUrl, time, `Timed out waiting for ${urlSubstring}`);
}
...
}
module.exports = ProHelper
The helper class is used inside the page object classes and in some specs. The first test runs fine. However the second one will fail as soon it uses one of the helper methods. It seems the helper methods are still referencing the original browser instance. I tried declaring EC inside the constructor and inside the methods but I get the same error. Is there any way I can re-initialize the helper class after a browser restart?
I made a few changes to get around the issue.
Removed the use of the helper from the specs. It is now only used in the page object classes.
Second I changed browser.ExpectedConditions; to protractor.ExpectedConditions.
Instead of using restartBrowserBetweenTests I added browser.restart() in afterEach for each spec.
That solved my session issue.
Related
I am following up the Heros tutorial from angular 2.0 section.5 services.
The documentation says its not necessary to wrap the this.heroes inside a function
constructor(private heroService: HeroService) { }
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
which doesnt work. and getting error as
Error: TypeError: Cannot read property 'getHeroes' of undefined(…)
Whereas if i use onInit then it works (the completion of the section).
constructor(private heroService: HeroService) {
}
ngOnInit() {
this.getHeroes();
}
getHeroes() {
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
}
Why i am getting the result only while running under ngOnInit?
My understanding is that the component class is a kind of toolbox, where you define properties and methods.
Methods will then respond to Angular hooks - like ngAfterViewInit or ngOnInit - or to events, like click().
Sometimes components need to load data when they are instanciated, in which case you can use ngOnInit or constructor to call your loading method.
You do not directly call methods in your class definition. I think that if you did, it could be an issue when importing a class, or when extending a class.
You don't want the class to go fetch your data each time you import your class; rather, you want to have complete control over when your data will be imported.
Here are the best practices for Angular 2
I need to do the following in MEF:
Whenever I resolve a certain interface I need a piece of code to run which gives the implementation.
It must never be a singleton, so MEF must always run that particular piece of code.
private IContract MethodToExecute()
var resolved = container.GetExport(); --> must run the MethodToExecute
I'm looking for best way of using session within zf application.
At first I did something like this - in init method of controller superclass I initialized session:
class Vovkin_Controller_Action extends Zend_Controller_Action
{
protected $_session;
public function init()
{
// here I define namespace
// ...
$this->_session = new Zend_Session_Namespace($nameSpace);
parent::init();
}
...
}
after that session in controller was used in this way:
public function someAction()
{
$this->_session->user = $user;
}
but I found this approach not very handy for other parts of system, like plugins, services, etc, because there I had to init session in other way, but I want keep it in one place if it's possible. So I decided to change it to this approach https://stackoverflow.com/a/2506447.
Now I have a few action helpers to provide access for sessions with different namespaces, it works like this:
public function someAction()
{
$this->_helper->session()->user = $user;
}
and so far it looks useful, because I can get access to session namespaces in other parts of system, for example in services, in this way:
class Vovkin_Model_Service_UserLoginService
{
public function login()
{
$session = Zend_Controller_Action_HelperBroker::getStaticHelper('session')->direct();
...
}
....
}
but how much it's correct to use it in this way, from point of architecture and used resources?
Thanks.
The answer you have refereed to is from Rob Allen, one of the main contributors of Zend Framework, so it's right to some extent. You can go with the action helpers on controllers without any problems.
But outside it, it's completely wrong. Services doesn't has nothing with action helpers and the front controller. You can't put a dependency on it (services to action helpers).
So, as the application bootstrap works as a container for initializing the application, it's reasonable to get the necessary values from there. The first thing I would suggest you is to use Zend_Registry. But as far as I cal tell you, it would be the same initializing the session object again, since it will not be wiped, it's just an object referencing the native $_SESSION superglobals. So, simply call
new Zend_Session_Namespace($nameSpace).
But again, this is wrong. You should not let your services know how sessions are handled (thus creating the objects inside it):
$session = Zend_Controller_Action_HelperBroker::getStaticHelper('session')->direct()
or even
$session = Zend_Registry('userSession')
or
$session = new Zend_Session_Namespace('userSession')
With that you are also not using the bootstrap container at all. Instead you should provide a common interface to deal with sessions (it could be $_SESSION or even a database) and inject it into the service as a parameter (e.g. __construct($session)). But that's a whole new subject (Dependency Injection).
So, you have two options considering the current state of the ZendFramework 1.11 (that's already old and full of bad practices):
1) You'll use services through controllers:
So you will get the session through the action helper and then pass it as a parameter to your service.
new Vovkin_Model_Service_UserLoginService($session)
2) You will use services independently of controllers and will get the dependencies through the bootstrap container:
Well, the worst thing is that to get the bootstrap you need to have a frontController dependency.
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
So, avoid it, and go with the first option, injecting the dependency instead. Although, if you really want it in that way, access it directly:
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
$bootstrap->get('userSession')
Sadly, in the end, it's everything wrong. But it's the best you can do with ZF 1. You should look forward to ZF 2 and Symfony 2 to better understand these concepts.
Here it's a good explanation: http://symfony.com/doc/current/book/service_container.html
(I know that you're using ZF, but it doesn't matter, the concept is the key)
A have lift app starting ssh daemon in Boot.scala.
Here is the problem: when i run container:restart / in sbt session I get Address alread in use exception.
Now two questions:
Is it right way to start dependent service in Boot.scala?
Anyway how is it possible to handle container:stop event?
I think the Lift-y way to do it is with LiftRules.unloadHooks.
It's not well-documented (AFAIK), but if you look in the Lift source code, you'll see that when the LiftServlet is destroy()ed, the functions defined in LiftRules.unloadHooks are executed.
You can add functions to the unloadHooks RulesSeq with the append or prepend method, depending on what order you want them executed in. So, in your bootstrap.liftweb.Boot.boot method, you might do something like this:
sshDaemon.start()
LiftRules.unloadHooks.append( () => sshDaemon.stop() )
(Assuming that was how you started and stopped your SSH daemon.)
I'm not 100% certain the LiftServlet.destroy() method gets invoked when the sbt web-plugin's container:restart command is run - that's determined by the plugin and its interaction with Jetty, not by Lift - but the container:stop command should definitely do the trick.
I'm not familiar with Lift, but this advice should work for any Servlet based web application.
Register a ServletContextListener in your web.xml, and release any resources in the contextDestroyed method. (The startup should be done in the contextCreated method.)
You can use setAttribute / getAttribute to store and later retrieve the server.
Putting this all together:
import javax.servlet.{ServletContextEvent, ServletContextListener}
final class SshListener extends ServletContextListener{
val attributeKey = "sshServer"
def contextInitialized(sce: ServletContextEvent) {
val server = new Server()
server.start()
sce.getServletContext.setAttribute(attributeKey, server)
}
def contextDestroyed(sce: ServletContextEvent) {
Option(sce.getServletContext.getAttribute(attributeKey)).foreach(_.asInstanceOf[Server].stop())
}
}
class Server {
def start()
def stop()
}
When executing the second line of this code Rhino Mocks throws an InvalidOperationException with a message "This action is invalid when the mock object is in replay state"
var mockScanner = MockRepository.GenerateMock<PortScanner>(null);
mockScanner.Expect((scanner => { scanner.Scan(null, null); }));
Stepping through the code in a debugger one can see the debugger run the method defined in the class and directly after control leaves this method the exception occurs.
This similar code in another test does work without issue
var mockView = MockRepository.GenerateMock<IScanView>(null);
mockView.Expect(view => { view.Close(); });
var controller = new ScanController(mockView);
controller.Exit();
mockView.VerifyAllExpectations();
The only difference that I can think of that might be of any consequense between theese two tests is that Exit is a member on an interface while Scan is a virtual member on a class
What am I missing?
Update
Further exploration has indicated that this is related to the way Rhino handles virtual methods. I am focusing mmy study of the documentation here now
The exception was caused because Rhino Mocks did not have the required level of access to the type in order to mock it properly. Granting internal access to the Rhino Mocks assembly using InternalsVisibleTo solved the problem.
It's noteworthy that this does not affect interfaces. I believe the reason for this is because the mocking framework needs to override the implementation on a class where there is none on an interface.
What happens if you remove the extra set of parentheses from the first expression?
var mockScanner = MockRepository.GenerateMock<PortScanner>(null);
mockScanner.Expect( scanner => { scanner.Scan(null, null); } );