Run whole selenium project in docker (Gradle + Selenium + java + junit + docker) - java

My current project is java selenium (with selenide framework) auto-tests with gradle and junit.
Now, I want to wrap my whole project to docker container, to make it possible to run it on other machines using only docker.
As I see it:
User run my docker image
Image have installed java + chrome + selenium + gradle
Project tests launched within container.
(optional) image shares test results outside image (or I can connect to container and take a look at them).
What am I suppose to do?
A saw a lot of tutorials about browsers in containers, selenoid, etc.(which is cool).
But I can't find a solution for my question.
Thanks!

Suggest to run tests as docker-compose multi-container application.
It will have 2 services in docker-compose as i see it:
browser - based on selenium Chrome browser image
tests - based on custom image extending java base image. Custom image Dockerfile should have gradle installed and tests jar file built in it.
Tests should drive Chrome browser using RemoteWebDriver initialized as below (note browser hostname where remote Chrome is listening).
public void createChromeDriverForRemote(){
WebDriver driver = new RemoteWebDriver("http://browser:4444/wd/hub", DesiredCapabilities.chrome());
}
See quick start here

What you need to do is:
Create a docker image that has Java, Chrome, selenium, gradle, junit, etc
Once you have the image, run it on your local on any port example: 4444
Switch to RemoteWebdriver
public static String remote_url_chrome = "http://localhost:4444/wd/hub";
ChromeOptions options = new ChromeOptions();
driver.set(new RemoteWebDriver(new URL(remote_url_chrome), options));
Run the test now

Related

Automation for "Chrome Legacy Window" (Chromium) using Winium

I'm trying to automate a Windows application GUI using Winium. The app is using both WPF windows and "Chrome Legacy Window" (Chromium) windows.
I'm using the tool "Automation Spy" to inspect the GUI elements' ids inside the WPF windows to use with Winium. Automation Spy can't inspect elements in the "Chrome Legacy Window" windows in the same manner that Winium can't access these elements.
"Chrome Legacy Window" is a WEB window, so it requires automation with Selenium.
How do I use Selenium to hook on a Chromium window, which is not a browser like Firefox, Chrome and similar?
"Remote Debugging Port" was the solution for my problem.
I added the "remote-debugging-port=XXXX" to my CEF (Chromium Embedded Framework) command:
https://blog.chromium.org/2011/05/remote-debugging-with-chrome-developer.html
This allowed me to see and manage my app's CEF windows through localhost:XXXX.
I used both Winium and Selenium to test my app.
Winium for all my WPF windows and selenium for all my CEF windows.
My GUI test starts with Winium Driver to open my app and navigate WPF windows.
Each time I need to debug a CEF window, I'm opening a Chrome Driver using selenium with the "remote-debugging-port" argument, which allows me to click elements inside that window. When I'm finish with this chromium window I'm closing the selenium driver and continues with Winium.
Use Selenium and Winium with IntelliJ IDEA
Selenium is a portable framework for testing and automating web applications.
Winium is a Selenium-based tool for testing and automating desktop applications on Windows.
In order to use these modules inside IntelliJ IDEA project, follow these steps:
Download selenium-server-standalone jar file (e.g. selenium-server-standalone-3.141.59.jar)
https://www.seleniumhq.org/download/
Download winium-webdriver jar file (e.g. winium-webdriver-0.1.0-1.jar)
http://central.maven.org/maven2/com/github/2gis/winium/winium-webdriver/0.1.0-1/
Add both jars to your project structure: File → Project Structure → Dependencies → +
Now all Selenium and Winium imports should work. For example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;
import org.openqa.selenium.winium.WiniumDriverService;
Use ChromeDriver with Selenium
Follow these steps:
Install the Java JDK and add its bin directory to your system PATH
variable.
Create a directory that will contain all files. This tutorial uses c:\temp.
Download ChromeDriver and extract it (e.g. chromedriver_win32.zip provides chomedriver.exe)
https://sites.google.com/a/chromium.org/chromedriver/downloads
Download selenium-server-standalone-X.X.X-alpha-1.zip (e.g. selenium-server-standalone-4.0.0-alpha-1.zip)
http://selenium-release.storage.googleapis.com/index.html
Download a CEF binary distribution client from Cefbuilds and extract (e.g. cef_binary_76.1.13+gf19c584+chromium-76.0.3809.132_windows64.tar.bz2)
http://opensource.spotify.com/cefbuilds/index.html
Your directory structure should now look similar to this:
c:\temp\
cef_binary_3.2171.1979_windows32_client\
Release\
cefclient.exe (and other files)
chromedriver.exe
Example.java
selenium-server-standalone-2.44.0.jar
For more information you can read this wiki:
https://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver.md
Use WiniumDriver with Winium
Follow these steps:
Download the Winium.Desktop.Driver.zip and extract it to c:\temp
https://github.com/2gis/Winium.Desktop/releases
Java Code Example
Start a winium driver and open your app:
DesktopOptions desktopOption = new DesktopOptions();
desktopOption.setApplicationPath("Path_to_your_app.exe");
File drivePath = new File("C:\\temp\\Winium.Desktop.Driver.exe");
WiniumDriverService service = new WiniumDriverService.Builder().usingDriverExecutable(drivePath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();
service.start();
WiniumDriver winiumDriver = WiniumDriver(service, desktopOption);
Navigate and test WPF windows using winium. For example:
winiumDriver.findElement(By.id("someElementID")).click();
Create a ChromeOptions object which holds all needed selenium information such as chromium client and remote debugging port.
Make sure to change the "XXXX" port to your remote debugging port.
System.setProperty("webdriver.chrome.driver", "c:/temp/chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("c:/temp/cef_binary_76.1.13+gf19c584+chromium-76.0.3809.132_windows64_client/Release/cefclient.exe");
chromeOptions.addArguments("remote-debugging-port=XXXX");
Open a chrome driver (selenium) using the chrome options object.
WebDriver seleniumDriver = ChromeDriver(chromeOptions);
Use element inside the chromium windows. For example:
seleniumWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("someButtonClassName")));
seleniumDriver.findElement(By.className("someButtonClassName")).click();
When you're done with the CEF window, close selenium driver and continue with the winium driver:
seleniumDriver.quit();
winiumDriver.findElement(By.id("someElementID")).click();
Close main winium driver:
winiumDriver.quit();

Can I run my selenium WebDriver code in another computer's chorme browser?

I created a java selenium project using maven on computer A and I am able to run tests there successfully.
What I want to do is to run it successfully also on computer B.
This is my code:
private static String currentDirectory = System.getProperty("user.dir");
public static final WebDriver getDriver(Browsers type){
return driverMap.get(type).get();
}
private static final Supplier<WebDriver> chromeDriverSupplier = () -> {
System.setProperty("webdriver.chrome.driver", currentDirectory + File.separator + "chromedriver.exe");
return new ChromeDriver();
Is there a reason why I am not able to run it on computer B?
Is computer B must have "chromedriver.exe" installed in the project directory or can I achieve it without downloading "chromedriver.exe"?
Thanks
You need to have chromedriver.exe in computer B as well.
I used WebdriverManager as suggested in the link provided and it solved the issue
Yes this is possible using selenium grid. Selenium Grid allows you to start a hub and node, then by using a remote webdriver to connect to these other machines to run tests.
Here is the URL for the documentation
This will also allow you to achieve parallel execution of test cases.
Once you have this set up you will set your remote driver with the following code:
WebDriver driver = new RemoteDriver(new URL(nodeAddress, capabilities);
Below is also the link to remote driver maven dependency:
maven dependency

How do I specify the Selenium Hub URL when running Serenity tests from Eclipse?

I'm trying to use the Serenity BDD testing framework with JUnit, instead of using Selenium directly, but I can't figure out how give the Serenity-managed WebDriver instance the URL of my Selenium Hub in a way that works for running tests from Eclipse (with "Run As > JUnit Test").
Using #Managed with driver="remote" correctly tries to create a org.openqa.selenium.remote.RemoteWebDriver instance...
#RunWith(SerenityRunner.class)
public class SerenityIT {
#Managed(driver="remote") WebDriver browser;
//[...]
}
... but this fails with
Caused by: java.lang.NullPointerException: null at
java.net.URL.(URL.java:532) ~[na:1.8.0_151]
which isn't surprising because there is no URL specified. So how should I pass the Selenium Hub URL?
You can also configure it in serenity.conf file
So something like that;
# Remote
webdriver {
driver = remote
remote {
url="http://localhost:4445/wd/hub"
driver=chrome
}
}
It turns our Serenity also loads serenity.properties when running tests from Eclipse, even though this is neither documented nor implied. I wrote one and Serenity found it, which is confirmed by logs in the console:
DEBUG [net.thucydides.core.util.PropertiesFileLocalPreferences:115] -
LOADING LOCAL PROPERTIES FROM
/integration-testing/serenity.properties
Selenium Hub's URL can then be provided as documented with webdriver.remote.url. Below are the properties I'm currently using, with an example of passing Firefox preferences.
webdriver.driver=remote
webdriver.remote.driver=firefox
webdriver.remote.url=http://127.0.0.1:4444/wd/hub
webdriver.timeouts.implicitlywait=10000
firefox.preferences=devtools.jsonview.enabled=false

Selenium, Java, Maven, PhantomJS + TeamCity

I've got a project in Selenium/Java with tests that normally run on chromedriver.exe (I work on Windows) - no problems at all.
I have recently decided to add the project to our TeamCity that runs on Linux and since it cannot run tests on a browser, I want to switch to PhantomJS driver.
I've tried multiple variations of all the solutions I've found online and still can't get it working.
Currently I've got the phantomjs binary file in my resources, and I get the driver like this in an enum class:
PHANTOMJS {
#Override
public WebDriver initNewDriver() {
System.setProperty("phantomjs.binary.path", ClassLoader.getSystemResource(RELATIVE_PATH_TO_FILE_IN_RESOURCES).getFile());
return new PhantomJSDriver();
}
Then in the test class I start with driver.get(SOME_URL) - which is the furthest it ever got.
No matter what I do, when I tried to run it on TeamCity through 'clean test', I get something like this:
[userLoginTest] java.lang.IllegalStateException: The driver executable does not exist: /opt/buildAgent1/work/c4641c7cfd3331f7/web/drivers/phantomjslinux/phantomjs
[14:41:41]
[userLoginTest] java.lang.IllegalStateException: The driver executable does not exist: /opt/buildAgent1/work/c4641c7cfd3331f7/web/drivers/phantomjslinux/phantomjs
at com.google.common.base.Preconditions.checkState(Preconditions.java:518)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:123)
at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:254)
at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:190)
at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:104)
at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:94)
at test.core.base.SeleniumDriver$3.initNewDriver(SeleniumDriver.java:47)
at test.core.base.TestBase.(TestBase.java:25)
I don't really understand TeamCity well and it's difficult to find solutions that are applicable to my project and that I actually understand. So any help is welcome. Thanks :)
Solved by running npm install phantomjs on the server and using PhantomJS from there.

Selenium Tests hang when attempting to be run with Bamboo

I'm attempting to use Bamboo's build and deployment capabilities to run Selenium Automated tests with my project.
We're currently using a Maven task to build and run regular JUNIT tests, and the plan is to use another Maven task to run the Selenium tests after the code has been successfully deployed to the server. At the moment, everything seems to run just fine locally, but when bamboo attempts to run the Selenium tests it seems to hang indefinitely. Unfortunately I don't have remote access to the server to watch it first hand, but I do know that it's a Microsoft server running with OS version: Windows 2012 R2 64-bit. I also know that the server is using java version "1.8.0_101", which is the same as my local setup. I've included a sample of the code I'm running below.
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SeleniumTestExample {
WebDriver driver;
#Before
public void setup(){
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer32bit.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(null, ieCapabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}
#Test
public void printPageTitle(){
System.out.println("Title of Page is: " + driver.getTitle());
driver.quit();
}
}
When run through Bamboo, the only output in the logs are the lines...
Started InternetExplorerDriver server (32-bit)
2.53.1.0
Listening on port 8080
Only local connections are allowed
CI or Bamboo server should be used for controlling your tests. You should not try to run your tests on a CI server. The issue you are having is probably because of that. Your are trying to use CI server as you local machine, which it will not respond the same way as your local. Instead you should use selenium grid in your setup to remotely connect to a machine by making use of hub and node. You may also have to use remote webdriver. Also have a look at this post
I've seen this using TeamCity, in this case the IE tells you that its executable will only accept connections from the local machine. According to Selenium org
The HTTP server started by the IEDriverServer.exe sets an access control list to only accept connections from the local machine, and disallows incoming connections from remote machines. At present, this cannot be changed without modifying the source code to the IEDriverServer.exe. To run the Internet Explorer driver on a remote machine, use the Java standalone remote server in connection with your language binding's equivalent of RemoteWebDriver.
So first run a chromedriver via passing through a param like so :
chromedriver --whitelisted-ips=""
This will basically whitelist all IP's, not always an ideal solution of course. But will show you that your tests can run on this CI configuration. Next thing to look for is your users privileges. Ask your admin to grant you more permissions in order to do your job. Keep in mind that IE's Protected mode may require some additional changes from your user. If none of this helps, consider Selenium grid with IE nodes.
Try to get rid of the line in the code:
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer32bit.exe");
First of all, it tells where selenium should look for the webdriver for IE. Since the Bamboo server is a windows machine, you have to set it up with the absolute path of the file, like "C:\test\webdriver\IEDriverServer32bit.exe".
Secondly, the property could be set using environment variables of the Bamboo task.
Thirdly, if you want to define it on the fly, you can define the property in pom.xml as:
<webdriver.ie.driver.path>
C:\test\webdriver\IEDriverServer32bit.exe
</webdriver.ie.driver.path>
and use it in a system property with help of maven-surefire-plugin.
then you can run test with the command
mvn test -Dwebdriver.ie.driver.path=C:\test\webdriver\IEDriverServer32bit.exe
with whatever path you want.

Categories