ChromeUser.java 4.31 KB
Newer Older
1
/*
2
 * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

18
package io.openvidu.test.browsers;
19

20 21
import java.net.MalformedURLException;
import java.net.URL;
22
import java.nio.file.Path;
23
import java.nio.file.Paths;
24 25
import java.util.HashMap;
import java.util.Map;
26
import java.util.concurrent.TimeUnit;
27

28 29
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
30
import org.openqa.selenium.remote.RemoteWebDriver;
31 32 33

public class ChromeUser extends BrowserUser {

34 35
	public ChromeUser(String userName, int timeOfWaitInSeconds, boolean runningAsRoot) {
		this(userName, timeOfWaitInSeconds, generateDefaultScreenChromeOptions(runningAsRoot));
36 37
	}

38
	public ChromeUser(String userName, int timeOfWaitInSeconds, String screenToCapture, boolean runningAsRoot) {
39 40
		this(userName, timeOfWaitInSeconds, generateCustomScreenChromeOptions(screenToCapture, runningAsRoot,
				Paths.get("/opt/openvidu/fakeaudio.wav")));
41 42 43 44 45
	}

	public ChromeUser(String userName, int timeOfWaitInSeconds, Path fakeVideoLocation) {
		this(userName, timeOfWaitInSeconds, generateFakeVideoChromeOptions(fakeVideoLocation));
	}
46

47 48
	private ChromeUser(String userName, int timeOfWaitInSeconds, ChromeOptions options) {
		super(userName, timeOfWaitInSeconds);
49
		options.setAcceptInsecureCerts(true);
50 51
		
		options.addArguments("--disable-infobars");
52 53 54 55 56

		Map<String, Object> prefs = new HashMap<String, Object>();
		prefs.put("profile.default_content_setting_values.media_stream_mic", 1);
		prefs.put("profile.default_content_setting_values.media_stream_camera", 1);
		options.setExperimentalOption("prefs", prefs);
57

58 59 60 61
		String REMOTE_URL = System.getProperty("REMOTE_URL_CHROME");
		if (REMOTE_URL != null) {
			log.info("Using URL {} to connect to remote web driver", REMOTE_URL);
			try {
62
				this.driver = new RemoteWebDriver(new URL(REMOTE_URL), options);
63 64 65 66 67
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		} else {
			log.info("Using local web driver");
68
			this.driver = new ChromeDriver(options);
69
		}
70

71
		this.driver.manage().timeouts().setScriptTimeout(this.timeOfWaitInSeconds, TimeUnit.SECONDS);
72 73 74
		this.configureDriver();
	}

75
	private static ChromeOptions generateDefaultScreenChromeOptions(boolean runningAsRoot) {
76 77 78 79 80 81
		ChromeOptions options = new ChromeOptions();
		// This flag avoids to grant the user media
		options.addArguments("--use-fake-ui-for-media-stream");
		// This flag fakes user media with synthetic video
		options.addArguments("--use-fake-device-for-media-stream");
		// This flag selects the entire screen as video source when screen sharing
82 83
		options.addArguments("--auto-select-desktop-capture-source=Entire screen");

84 85 86 87
		if (runningAsRoot) {
			options.addArguments("--no-sandbox");
		}

88 89 90
		return options;
	}

91 92
	private static ChromeOptions generateCustomScreenChromeOptions(String screenToCapture, boolean runningAsRoot,
			Path audioFileLocation) {
93 94
		ChromeOptions options = new ChromeOptions();
		// This flag selects the entire screen as video source when screen sharing
95
		options.addArguments("--auto-select-desktop-capture-source=" + screenToCapture);
96 97
		options.addArguments("--use-fake-device-for-media-stream");
		options.addArguments("--use-file-for-fake-audio-capture=" + audioFileLocation.toString());
98 99 100 101 102

		if (runningAsRoot) {
			options.addArguments("--no-sandbox");
		}

103 104 105 106 107 108 109 110 111 112 113 114 115 116
		return options;
	}

	private static ChromeOptions generateFakeVideoChromeOptions(Path videoFileLocation) {
		ChromeOptions options = new ChromeOptions();
		// This flag avoids to grant the user media
		options.addArguments("--use-fake-ui-for-media-stream");
		// This flag fakes user media with synthetic video
		options.addArguments("--use-fake-device-for-media-stream");
		// This flag sets the video input as
		options.addArguments("--use-file-for-fake-video-capture=" + videoFileLocation.toString());
		return options;
	}

117
}