
[英]Java maven selenium webdriver not working in docker container
[英]Java maven selenium not working in a docker container
我是 docker 容器和测试的新手,不知道如何解决这个问题,这些天一直很难。 My tests are working in windows but when I create the docker container, my tests are not working, so I want to run a maven project in a docker container. 问题是我已经解决了很多错误,但仍然有很多。 在开始之前,我使用带有 java、maven、chromedriver 和 JAVA_HOME 的 openstax/selenium-chrome 图像。 容器上Java版本是17,项目有1.8。
Chromedriver 版本是 ChromeDriver 88.0.4324.96
Google-chrome 版本是 Google Chrome 88.0.4324.96
1-我从我已经创建的图像创建 docker 容器,其中安装了所有通过 maven 项目安装的容器。
docker run -it -d -v C:\Users\user\Projects\TestAutomation:/app --name=testsel maven-selenium/selenium
2-我与用户一起输入
docker exec -it testsel /bin/bash
3-我切换到我有 mvn 项目的目录
4-我使用 mvn 测试执行 maven 项目 5-这是我的 pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<includes>
<include>org/nameofproject/tests/Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
6- 为 linux 设置镀铬选项
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1080");
options.addArguments("--disable-gpu");
options.addArguments("--disable-extensions");
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
options.addArguments("--start-maximized");
options.addArguments("--headless");
System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");
driver = new ChromeDriver(options);
最后是另一个错误
Expected condition failed: waiting for visibility of element located by By.id: username (tried for 60 second(s) with 500 milliseconds interval)(..)
我正在使用显式等待来定位元素
wait = new WebDriverWait(driver, Duration.ofSeconds(60));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))).sendKeys(Const.user);
但仍然无法正常工作。
您可以尝试增加 windows 大小,因为它可能不在视口中,我遇到了同样的问题并且能够解决它
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars");
chromeOptions.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
chromeOptions.setExperimentalOption("useAutomationExtension", false);
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1580,1280");
如果您认为使用操作 class 或 JavascriptExecutor 在视口中不可见,您也可以先尝试滚动到元素
使用动作 Class
WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
使用 JavascriptExecutor
WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.