How to stay logged in after testing with PHPUnit, Selenium and php-webdriver

I am trying to run several tests with a common session between them, starting with login.

   <?php

   class ExampleTest extends PHPUnit_Framework_TestCase {

       /**
        * @var \RemoteWebDriver
        */

       protected $webDriver;
       protected $host = 'http://localhost:4444/wd/hub';
       protected $browser = array (
         'browserName' => 'chrome',
         'sessionStrategy' => 'shared'
       );

       public function setUp()
       {
         $this->webDriver = RemoteWebDriver::create($this->host, $this->browser);
       }

       public function tearDown()
       {
         $this->webDriver->quit();
       }

My first test is a login that works great:

   public function testLogin()
   {
     $this->webDriver->get('http://localhost:8888/public');
     $this->webDriver->findElement(WebDriverBy::name("login"))->sendKeys("logintest");
     $this->webDriver->findElement(WebDriverBy::name("password"))->sendKeys("passwordtest");
     $this->webDriver->findElement(WebDriverBy::className("btn"))->click();
   }

Then I want to check the click on the table row (based on the value inside the row):

   public function testFranchiseClick()
   {
     $this->webDriver->get('http://localhost:8888/public/franchises');
     $this->webDriver->findElement(WebDriverBy::xpath("//td[contains(text(), 'TestPortal')]"))->click();
   }

Unfortunately, I get the following error:

   NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//td[contains(text(), 'TestPortal')]"}

I am sure that this is due to tests that do not use the same session (the user is not registered, therefore, does not have access to the / franchises page), because it works fine if I include these two commands in the testLogin () function .

Any idea what I'm doing wrong? I still want to be able to use these "findElement (WebDriverBy ::" things.

Many thanks!

+4
2

, . , , , . , , . .

+1

Selenium, PHP, tearDown() ?

0

Source: https://habr.com/ru/post/1616143/


All Articles