我希望在 Laravel 5.5 中使用 Dusk 2.0 运行浏览器测试时能够看到浏览器(特别是这样我可以使用 $browser-tinker() 并为其提供一些手动命令以进行调试)。
有谁知道如何做到这一点?我希望有一些简单的事情:
php artisan dusk --noheadless
Run Code Online (Sandbox Code Playgroud) 当我这样做时,我正在使用 Laravel Dusk 进行自动化测试:
$test = $browser->script('$(".page-sidebar-menu").text();');
dd($test);
Run Code Online (Sandbox Code Playgroud)
它返回空数组,但如果$(".page-sidebar-menu").text();在浏览器中运行,它将返回该类中的所有文本。
我这里哪里出错了?知道的请帮忙。
我正在尝试使用 Laravel/Dusk 在无头 chrome 中自动下载文件。在 GUI 模式下,文件在我的下载文件夹中下载得很好。但在无头模式下,根本不会下载。有什么方法可以解决这个问题吗?
当我运行 laravel dusk 测试时,cmd 显示许多控制台消息,例如:
DevTools listening on ws://127.0.0.1:12802/devtools/browser/dbffc66a-0b29-4149-a1b5-8f20259770c2
[0720/101840.929:INFO:CONSOLE(44479)] "Download the Vue Devtools extension for a better development
experience:
https://github.com/vuejs/vue-devtools", source: http://localhost:8000/js/app.js (44479)
[0720/101840.929:INFO:CONSOLE(44490)] "You are running Vue in development mode.
Make sure to turn on production mode when deploying for production.
See more tips at https://vuejs.org/guide/deployment.html", source: http://localhost:8000/js/app.js (
44490)
Run Code Online (Sandbox Code Playgroud)
我怎样才能防止这种情况发生?它在测试时浏览每个页面时显示此内容
我正在使用 laravel5.6并Dusk进行此特定测试。
我正在尝试在我的 dropzone 中声明文件上传。但是我的 Dropzone 是以我没有file输入元素的方式创建的。所以我不能使用该attach()方法。
所以我尝试了以下
$file = new \Symfony\Component\HttpFoundation\File\UploadedFile(base_path() . '/tests/samples/Cylinder.stl', 'Cylinder.stl');
$response = $this->actingAs( $this->user )
->from( 'my-url' )
->post( route('attachments.store' ) , [
'file' => $file
]);
Run Code Online (Sandbox Code Playgroud)
但是错误包包含这个错误
"errors" => Illuminate\Support\ViewErrorBag {#1194
#bags: array:1 [
"default" => Illuminate\Support\MessageBag {#1189
#messages: array:1 [
"file" => array:1 [
0 => "The file failed to upload."
]
]
#format: ":message"
}
]
}
Run Code Online (Sandbox Code Playgroud)
当然,当我手动执行时,这是有效的。
我正在使用 Laravel 框架开发 Web 应用程序。我正在我的应用程序上运行 Dus/浏览器单元测试。但是现在我在断言所选元素是否存在时遇到问题。
我有包含此代码片段的视图文件。
@if(auth()->user()->role == 1)
<a dusk="button-create" href="/create">
Create
</a>
@endif
Run Code Online (Sandbox Code Playgroud)
我正在测试该按钮是否存在于 Dusk 单元测试中。
$this->browse(function (Browser $browser) use ($admin) {
$browser->loginAs($admin)->visit(url);
$textContent = $browser->text("@button-create");
$this->assertNotEmpty($textContent);
});
Run Code Online (Sandbox Code Playgroud)
使用上面的代码测试按钮是否存在很好。但是当我测试按钮是否不存在时。
$this->browse(function (Browser $browser) use ($student) {
$browser->loginAs($student)->visit(url);
$textContent = $browser->text("@button-create");
$this->assertEmpty($textContent);
});
Run Code Online (Sandbox Code Playgroud)
我收到此错误。
no such element: Unable to locate element: {"method":"css selector","selector":"body [dusk="button-create"]"}
Run Code Online (Sandbox Code Playgroud)
我可以想到的一个解决方案是用另一个元素包装 html 元素并在其上设置选择器。像这样的东西。
<span dusk="button-create">
@if(auth()->user()->role == 1)
<a href="/create">
Create
</a>
@endif
</span>
Run Code Online (Sandbox Code Playgroud)
但我不想用另一个 html 标签包装它。是否可以不包装另一个标签?如何?
我有一个带有 Sail 的 Laravel,我想用 Laravel Dusk 进行自动化测试。我按照Dusk Documentation和Sail + Dusk Installation中的所有说明进行操作,但是当我运行默认测试时,我收到以下错误消息:
Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"args":["--disable-gpu","--headless","--no-sandbox","--window-size=1920,1080"]},"acceptInsecureCerts":true}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"args":["--disable-gpu","--headless","--no-sandbox","--window-size=1920,1080"]},"acceptInsecureCerts":true}}
Failed to connect to localhost port 4444: Connection refused
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:333
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:136
/var/www/html/tests/DuskTestCase.php:69
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:218
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/helpers.php:234
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:219
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:97
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:65
/var/www/html/tests/Browser/ExampleTest.php:21
Run Code Online (Sandbox Code Playgroud)
但如果在 Insomnia 中执行请求,它运行不会出现任何问题:
这是我的 docker-compose:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes: …Run Code Online (Sandbox Code Playgroud) 在最后一天左右,我开始看到我的 Laravel Dusk 测试在我的 CI/CD 环境中失败(GitHub Actions)。
突然,运行一直运行良好的命令:
php artisan dusk:chrome-driver `/opt/google/chrome/chrome --version | cut -d " " -f3 | cut -d "." -f1`
Run Code Online (Sandbox Code Playgroud)
..失败并出现错误:
file_get_contents(https://chromedriver.storage.googleapis.com/LATEST_RELEASE_116):无法打开流:HTTP请求失败!HTTP/1.1 404 未找到
尝试通过强制版本 114 来解决此问题:
php artisan install:chrome-driver 114
Run Code Online (Sandbox Code Playgroud)
...失败并出现错误:
Facebook\WebDriver\Exception\SessionNotCreatedException:会话未创建:此版本的 ChromeDriver 仅支持 Chrome 版本 114
我该如何解决这个问题并让我的测试再次开始通过?
我收到此错误:
数据库(宅基地)不存在。
当我尝试这个简单的黄昏测试时:
class ShowArticleTest extends DuskTestCase
{
use DatabaseMigrations;
/** @test */
public function it_shows_all_articles()
{
Article::create([
'title' => 'My First Article',
'body' => 'Some body text.'
]);
$this->browse(function (Browser $browser) {
$browser->visit('/articles')
->assertSee('My First Article');
});
}
}
Run Code Online (Sandbox Code Playgroud)
我可以在堆栈跟踪中看到错误来自处理请求的控制器方法/articles,如下所示:
public function all()
{
Article::all();
}
Run Code Online (Sandbox Code Playgroud)
因此,测试本身似乎可以访问数据库,但控制器不能访问数据库。
该.env.dusk.local文件如下所示:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Run Code Online (Sandbox Code Playgroud)
该homestead文件正在我的项目根文件夹中创建。该文件也在虚拟机内找到,权限如下所示:
-rw-rw-r-- 1个流浪者
尝试设置DATABASE为database/testing.sqlitein .env.dusk.local。Dusk 启动后会创建该文件,但错误仍然表明找不到数据库database/testing.sqlite。
数据库(database/testing.sqlite)不存在。
可以使用 CLI 访问数据库文件sqlite3,并且可以毫无问题地查询记录。 …
我目前正在开发 Laravel CRUD 应用程序,我想知道为什么 PHPUnit 不支持抓取浏览器(正如我所读到的)。我已经通过 PHPUnit 覆盖了我的项目的基础,但我也想测试链接、标签、按钮点击等。所以我已经有了一个强大的单元测试基础。
现在我读到 Dusk 为 DOM 测试提供了一个爬虫。我应该一起使用两者吗(甚至可能吗?)还是应该迁移到 Dusk?我不确定 Dusk 是否提供与 PHPUnit 相同的功能,并且如上所述,我确实已经拥有强大的 phpunit 测试基础。
从现在开始,由于 50:50 的测试用例,我有点陷入困境,因为我还需要测试 DOM 是否提供了正确的信息。
感谢任何帮助或专家建议。
先感谢您!