加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何使用PHPUnit,Selenium和php-webdriver进行测试后保持登录状

发布时间:2020-12-13 15:59:11 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用它们之间的共享会话运行多个测试,从登录开始. ?php class ExampleTest extends PHPUnit_Framework_TestCase { /** * @var RemoteWebDriver */ protected $webDriver; protected $host = 'http://localhost:4444/wd/hub'; protected $browser
我正在尝试使用它们之间的共享会话运行多个测试,从登录开始.

<?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();
       }

我的第一个测试是登录,工作正常:

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();
   }

然后我想测试一个表行的单击(基于行内的值):

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

不幸的是我收到以下错误:

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

我确信这是由于测试没有使用相同的会话(用户没有登录因此无法访问/特许经营页面),因为如果我在testLogin()函数中包含这两个指令,它可以正常工作.

我知道我做错了什么?我仍然希望能够使用这些“findElement(WebDriverBy ::”的东西.

非常感谢提前!

解决方法

您的测试需要独立,以便您可以毫无问题地运行其中一个测试.这意味着对于每个测试,您必须执行登录,执行您的操作,然后注销.如果我可以给你一个建议,请创建一个方法登录,您将在测试中使用它.在此登录方法中,您将获取元素并与它们进行交互.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读