Java maven selenium not working in a docker container

Viewed 46

I am new with docker containers and testing and have no idea how to solve this, been having a hard time these days. 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. The thing is I have been solving a lot of errors but still lots of them. Before I start, I use openstax/selenium-chrome image with java, maven,chromedriver already installed and JAVA_HOME already set. Java version is 17 on the container and the project has 1.8.

Chromedriver version is ChromeDriver 88.0.4324.96

Google-chrome version is Google Chrome 88.0.4324.96

1- I create the docker container from the image I already created with everything installed passing the maven project inside.

docker run -it -d -v C:\Users\user\Projects\TestAutomation:/app --name=testsel maven-selenium/selenium

2- I enter with the user

docker exec -it testsel /bin/bash

3- I switch to the directory that I have the mvn project

4- I execute the maven project with mvn test 5- this is my 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- Setting chrome options for 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);

7- The errors I am getting enter image description here

enter image description here

enter image description here

and at the end another error

 Expected condition failed: waiting for visibility of element located by By.id: username (tried for 60 second(s) with 500 milliseconds interval)(..)

I am using explicit wait in locating the elements

 wait = new WebDriverWait(driver, Duration.ofSeconds(60));

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))).sendKeys(Const.user);

but still not working.

Edit: This is the complete error at the end of test execution. enter image description here It fails at CBLogin because it's not finding the username element.

1 Answers

You can try increasing the windows size, as it might not be in the viewport , I was facing the same Issue and was able to solve it

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");

You could also try scrolling to element first if you think its not visible in viewport using actions class or JavascriptExecutor

Using Actions Class

  WebElement element = driver.findElement(By.id("my-id"));
    Actions actions = new Actions(driver);
    actions.moveToElement(element);
    actions.perform();

Using JavascriptExecutor

  WebElement element = driver.findElement(By.id("id_of_element"));
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Related