Search code examples
selenium-gridbehat

Selenium grid for different users (with Behat and Mink)


Suppose this test logic in a chat system:

  • user1 is in home page using browser1 in vm1
  • user1 wait until see "myText" in browser1 in vm1
  • user2 write "myText" using browser2 in vm2 (so user1 can proced)

Essentially is a sessions distribution inside a Selenium grid. How to do this?


Solution

  • Behat\Mink\Mink provides you a way to create, update, delete sessions on your own. Suppose you have a bdd scenario:

    Scenario: Users can receive messages sent from their friends
     Given "User 1" user sends "Hello" message to "User 2" user
      Then "User 2" receives "Hello" message from "User 1" user
    

    Suppose you have the next methods inside your ChatContext file

    /**
     * @Given /^:sender user sends :message message to :receiver user$/
     */
    public function userSendsAMessageToUser($sender, $message, $receiver) {
        $this->initialiseSessions();
    
        /* Now you need to set default session as firstSession and open browser and send message*/
        $this->getMink()->setDefaultSessionName('firstSession');
    
        $this->visit('your site');
    }
    
    protected function initialiseSessions() {
        /* Initialise your driver with all the required attributes*/
        $driver = new Selenium2Driver();
        $this->firstSession = new Session($driver);
    
        $driver = new Selenium2Driver();
        $this->secondSession = new Session($driver);
    
        $this->getMink()->registerSession('firstSession', $this->firstSession);
        $this->getMink()->registerSession('secondSession', $this->secondSession);
    
    }
    

    Then you need to implement checking message method and for this you need to switch the session $this->getMink()->setDefaultSessionName('secondSession');