gpt4 book ai didi

Laravel 帆 + 黄昏 + Selenium : Connection refused

转载 作者:行者123 更新时间:2023-12-05 08:37:26 25 4
gpt4 key购买 nike

我有一个带 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

但是如果在 Insomnia 中执行请求,它运行没有任何问题:

Insomnia request

这是我的 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:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
- selenium
selenium:
image: 'selenium/standalone-chrome'
volumes:
- '/dev/shm:/dev/shm'
networks:
- sail
ports:
- 4444:4444
#depends_on:
# - laravel.test
mysql:
image: 'mysql:8.0'
ports:
- '${DB_PORT}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
redis:
image: 'redis:alpine'
ports:
- '${REDIS_PORT}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
# memcached:
# image: 'memcached:alpine'
# ports:
# - '11211:11211'
# networks:
# - sail
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- 1025:1025
- 8025:8025
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local

还有我的 DuskTestCase

<?php

namespace Tests;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\TestCase as BaseTestCase;

abstract class DuskTestCase extends BaseTestCase {
use CreatesApplication;

/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare() {
if (!static::runningInSail()) {
static::startChromeDriver();
}
}

/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver() {
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
'--no-sandbox',
'--window-size=1920,1080',
]);

return RemoteWebDriver::create(
'http://localhost:4444/wd/hub', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)->setCapability('acceptInsecureCerts', TRUE)
);
}
}

我真的不知道我做错了什么。抱歉我的英语不好,我是巴西人。

最佳答案

经过很多天,我终于设法解决了我的问题。阅读Docker documentation我发现我可以使用图像名称作为 URL,所以我切换到 http://selenium:4444 并且测试完美运行!

这是我的新代码:

<?php

namespace Tests;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\TestCase as BaseTestCase;

abstract class DuskTestCase extends BaseTestCase {
use CreatesApplication;

/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare() {
if (!static::runningInSail()) {
static::startChromeDriver();
}
}

/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver() {
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
'--no-sandbox',
'--window-size=1920,1080',
]);

return RemoteWebDriver::create(
'http://selenium:4444/wd/hub', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)->setCapability('acceptInsecureCerts', TRUE)
);
}
}

希望能帮到有同样问题的人!

关于Laravel 帆 + 黄昏 + Selenium : Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65569147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com