<commit_before>package com.seleniumtests.ut.browserfactory;


import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.mockito.Mock;
import org.mockito.Mockito;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.Timeouts;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.seleniumtests.MockitoTest;
import com.seleniumtests.browserfactory.SeleniumGridDriverFactory;
import com.seleniumtests.browserfactory.SeleniumRobotCapabilityType;
import com.seleniumtests.connectors.selenium.SeleniumGridConnector;
import com.seleniumtests.core.SeleniumTestsContext;
import com.seleniumtests.core.SeleniumTestsContextManager;
import com.seleniumtests.customexception.ScenarioException;
import com.seleniumtests.customexception.SeleniumGridException;
import com.seleniumtests.driver.BrowserType;
import com.seleniumtests.driver.DriverConfig;
import com.seleniumtests.driver.TestType;
import com.seleniumtests.util.logging.DebugMode;

@PowerMockIgnore("javax.net.ssl.*")
@PrepareForTest({SeleniumGridDriverFactory.class})
public class TestSeleniumGridDriverFactory extends MockitoTest {

	@Mock
	private DriverConfig config;

	@Mock
	private Proxy proxyConfig;
	
	@Mock
	private SeleniumGridConnector gridConnector1;
	
	@Mock
	private SeleniumGridConnector gridConnector2;

	@Mock
	private SeleniumTestsContext context;
	
	@Mock
	private RemoteWebDriver driver;
	
	@Mock
	private RemoteWebDriver driver2;
	
	@Mock
	private Options options;
	
	@Mock
	private Timeouts timeouts;
	

	@BeforeMethod(groups= {"ut"})
	public void init() throws Exception {
		when(config.getTestContext()).thenReturn(context);
		Mockito.when(config.getDebug()).thenReturn(Arrays.asList(DebugMode.NONE));

		when(config.getBrowserType()).thenReturn(BrowserType.HTMLUNIT);
		when(config.getPlatform()).thenReturn("windows");
		
		// configure driver
		Map<String, String> capsMap = new HashMap<>();
		capsMap.put(CapabilityType.BROWSER_NAME, "htmlunit");
		capsMap.put(CapabilityType.VERSION, "70.0.1.2.3");
		Capabilities caps = new MutableCapabilities(capsMap);
		
		when(driver.manage()).thenReturn(options);
		when(driver.getCapabilities()).thenReturn(caps);
		when(driver2.manage()).thenReturn(options);
		when(driver2.getCapabilities()).thenReturn(caps);
		when(options.timeouts()).thenReturn(timeouts);
		
		when(gridConnector1.getHubUrl()).thenReturn(new URL("http://localhost:1111/wd/hub"));
		when(gridConnector2.getHubUrl()).thenReturn(new URL("http://localhost:2222/wd/hub"));
	}
	
	/**
	 * Check we create a driver from grid
	 * @throws Exception
	 */
	@Test(groups={"ut"})
	public void testDriverCreation() throws Exception {
		
		SeleniumTestsContextManager.setThreadContext(context);
		when(context.getTestType()).thenReturn(TestType.WEB);
		
		when(gridConnector1.isGridActive()).thenReturn(true);
		when(context.getSeleniumGridConnectors()).thenReturn(Arrays.asList(gridConnector1));
		
		// connect to grid
		PowerMockito.whenNew(RemoteWebDriver.class).withAnyArguments().thenReturn(driver);
		SeleniumGridDriverFactory driverFactory = new SeleniumGridDriverFactory(config);
		WebDriver newDriver = driverFactory.createWebDriver();
		
		// check that with a single driver creation, retry timeout is 30 mins
		Assert.assertEquals(driverFactory.getInstanceRetryTimeout(), SeleniumGridDriverFactory.DEFAULT_RETRY_TIMEOUT);
		
		// issue #280: also check that BrowserInfo is not null
		Assert.assertNotNull(newDriver);
		Assert.assertEquals(newDriver, driver);
		Assert.assertNotNull(driverFactory.getSelectedBrowserInfo());
		Assert.assertEquals(driverFactory.getSelectedBrowserInfo().getBrowser(), BrowserType.HTMLUNIT);
		Assert.assertEquals(driverFactory.getSelectedBrowserInfo().getVersion(), "70.0.1.2.3");
	}
	
	/**
	 * If grid is not active, driver is not created and exception is raised
	 * @throws Exception
	 */
	@Test(groups={"ut"}, expectedExceptions=SeleniumGridException.class)
	public void testDriverNotCreatedIfGridNotActive() throws Exception {
		
		try {
			SeleniumGridDriverFactory.setRetryTimeout(2);
			
			SeleniumTestsContextManager.setThreadContext(context);
			when(context.getTestType()).thenReturn(TestType.WEB);
			
			when(gridConnector1.isGridActive()).thenReturn(false);
			when(context.getSeleniumGridConnectors()).thenReturn(Arrays.asList(gridConnector1));
			
			// connect to grid
			PowerMockito.whenNew(RemoteWebDriver.class).withAnyArguments().thenReturn(driver);
			SeleniumGridDriverFactory driverFactory = new SeleniumGridDriverFactory(config);
			driverFactory.createWebDriver();
			
		} finally {
			SeleniumGridDriverFactory.setRetryTimeout(SeleniumGridDriverFactory.DEFAULT_RETRY_TIMEOUT);
		}
		
	}
	
	/**
	 * If exception is raised during driver creation, exception is raised
	 * @throws Exception
	 */
	@Test(groups={"ut"}, expectedExceptions=SeleniumGridException.class)
	public void testDriverNotCreatedIfError() throws Exception {
		
		try {
			SeleniumGridDriverFactory.setRetryTimeout(1);
			
			SeleniumTestsContextManager.setThreadContext(context);
			when(context.getTestType()).thenReturn(TestType.WEB);
			
			when(gridConnector1.isGridActive()).thenReturn(true);
			when(context.getSeleniumGridConnectors()).thenReturn(Arrays.asList(gridConnector1));
			
			// connect to grid;
			PowerMockito.whenNew(RemoteWebDriver.class).withParameterTypes(URL.class, Capabilities.class).withArguments(eq(new URL("http://localhost:1111/wd/hub")), any(DesiredCapabilities.class)).thenThrow(new WebDriverException(""));
			new SeleniumGridDriverFactory(config).createWebDriver();
			
		} finally {
			SeleniumGridDriverFactory.setRetryTimeout(SeleniumGridDriverFactory.DEFAULT_RETRY_TIMEOUT);
		}	
	}
	
	/**
	 * Check we use the first grid connector if it's available
	 * @throws Exception
	 */
	@Test(groups={"ut"})
	public void testDriverCreationWithSeveralGridConnectors() throws Exception {
		
		SeleniumTestsContextManager.setThreadContext(context);
		when(context.getTestType()).thenReturn(TestType.WEB);
		
		when(gridConnector1.isGridActive()).thenReturn(true);<commit_msg>issue #311: corrected expected exception<commit_after>package com.seleniumtests.ut.browserfactory;


import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.mockito.Mock;
import org.mockito.Mockito;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.Timeouts;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.seleniumtests.MockitoTest;
import com.seleniumtests.browserfactory.SeleniumGridDriverFactory;
import com.seleniumtests.browserfactory.SeleniumRobotCapabilityType;
import com.seleniumtests.connectors.selenium.SeleniumGridConnector;
import com.seleniumtests.core.SeleniumTestsContext;
import com.seleniumtests.core.SeleniumTestsContextManager;
import com.seleniumtests.customexception.ScenarioException;
import com.seleniumtests.customexception.SeleniumGridNodeNotAvailable;
import com.seleniumtests.driver.BrowserType;
import com.seleniumtests.driver.DriverConfig;
import com.seleniumtests.driver.TestType;
import com.seleniumtests.util.logging.DebugMode;

@PowerMockIgnore("javax.net.ssl.*")
@PrepareForTest({SeleniumGridDriverFactory.class})
public class TestSeleniumGridDriverFactory extends MockitoTest {

	@Mock
	private DriverConfig config;

	@Mock
	private Proxy proxyConfig;
	
	@Mock
	private SeleniumGridConnector gridConnector1;
	
	@Mock
	private SeleniumGridConnector gridConnector2;

	@Mock
	private SeleniumTestsContext context;
	
	@Mock
	private RemoteWebDriver driver;
	
	@Mock
	private RemoteWebDriver driver2;
	
	@Mock
	private Options options;
	
	@Mock
	private Timeouts timeouts;
	

	@BeforeMethod(groups= {"ut"})
	public void init() throws Exception {
		when(config.getTestContext()).thenReturn(context);
		Mockito.when(config.getDebug()).thenReturn(Arrays.asList(DebugMode.NONE));

		when(config.getBrowserType()).thenReturn(BrowserType.HTMLUNIT);
		when(config.getPlatform()).thenReturn("windows");
		
		// configure driver
		Map<String, String> capsMap = new HashMap<>();
		capsMap.put(CapabilityType.BROWSER_NAME, "htmlunit");
		capsMap.put(CapabilityType.VERSION, "70.0.1.2.3");
		Capabilities caps = new MutableCapabilities(capsMap);
		
		when(driver.manage()).thenReturn(options);
		when(driver.getCapabilities()).thenReturn(caps);
		when(driver2.manage()).thenReturn(options);
		when(driver2.getCapabilities()).thenReturn(caps);
		when(options.timeouts()).thenReturn(timeouts);
		
		when(gridConnector1.getHubUrl()).thenReturn(new URL("http://localhost:1111/wd/hub"));
		when(gridConnector2.getHubUrl()).thenReturn(new URL("http://localhost:2222/wd/hub"));
	}
	
	/**
	 * Check we create a driver from grid
	 * @throws Exception
	 */
	@Test(groups={"ut"})
	public void testDriverCreation() throws Exception {
		
		SeleniumTestsContextManager.setThreadContext(context);
		when(context.getTestType()).thenReturn(TestType.WEB);
		
		when(gridConnector1.isGridActive()).thenReturn(true);
		when(context.getSeleniumGridConnectors()).thenReturn(Arrays.asList(gridConnector1));
		
		// connect to grid
		PowerMockito.whenNew(RemoteWebDriver.class).withAnyArguments().thenReturn(driver);
		SeleniumGridDriverFactory driverFactory = new SeleniumGridDriverFactory(config);
		WebDriver newDriver = driverFactory.createWebDriver();
		
		// check that with a single driver creation, retry timeout is 30 mins
		Assert.assertEquals(driverFactory.getInstanceRetryTimeout(), SeleniumGridDriverFactory.DEFAULT_RETRY_TIMEOUT);
		
		// issue #280: also check that BrowserInfo is not null
		Assert.assertNotNull(newDriver);
		Assert.assertEquals(newDriver, driver);
		Assert.assertNotNull(driverFactory.getSelectedBrowserInfo());
		Assert.assertEquals(driverFactory.getSelectedBrowserInfo().getBrowser(), BrowserType.HTMLUNIT);
		Assert.assertEquals(driverFactory.getSelectedBrowserInfo().getVersion(), "70.0.1.2.3");
	}
	
	/**
	 * If grid is not active, driver is not created and exception is raised
	 * @throws Exception
	 */
	@Test(groups={"ut"}, expectedExceptions=SeleniumGridNodeNotAvailable.class)
	public void testDriverNotCreatedIfGridNotActive() throws Exception {
		
		try {
			SeleniumGridDriverFactory.setRetryTimeout(2);
			
			SeleniumTestsContextManager.setThreadContext(context);
			when(context.getTestType()).thenReturn(TestType.WEB);
			
			when(gridConnector1.isGridActive()).thenReturn(false);
			when(context.getSeleniumGridConnectors()).thenReturn(Arrays.asList(gridConnector1));
			
			// connect to grid
			PowerMockito.whenNew(RemoteWebDriver.class).withAnyArguments().thenReturn(driver);
			SeleniumGridDriverFactory driverFactory = new SeleniumGridDriverFactory(config);
			driverFactory.createWebDriver();
			
		} finally {
			SeleniumGridDriverFactory.setRetryTimeout(SeleniumGridDriverFactory.DEFAULT_RETRY_TIMEOUT);
		}
		
	}
	
	/**
	 * If exception is raised during driver creation, exception is raised
	 * @throws Exception
	 */
	@Test(groups={"ut"}, expectedExceptions=SeleniumGridNodeNotAvailable.class)
	public void testDriverNotCreatedIfError() throws Exception {
		
		try {
			SeleniumGridDriverFactory.setRetryTimeout(1);
			
			SeleniumTestsContextManager.setThreadContext(context);
			when(context.getTestType()).thenReturn(TestType.WEB);
			
			when(gridConnector1.isGridActive()).thenReturn(true);
			when(context.getSeleniumGridConnectors()).thenReturn(Arrays.asList(gridConnector1));
			
			// connect to grid;
			PowerMockito.whenNew(RemoteWebDriver.class).withParameterTypes(URL.class, Capabilities.class).withArguments(eq(new URL("http://localhost:1111/wd/hub")), any(DesiredCapabilities.class)).thenThrow(new WebDriverException(""));
			new SeleniumGridDriverFactory(config).createWebDriver();
			
		} finally {
			SeleniumGridDriverFactory.setRetryTimeout(SeleniumGridDriverFactory.DEFAULT_RETRY_TIMEOUT);
		}	
	}
	
	/**
	 * Check we use the first grid connector if it's available
	 * @throws Exception
	 */
	@Test(groups={"ut"})
	public void testDriverCreationWithSeveralGridConnectors() throws Exception {
		
		SeleniumTestsContextManager.setThreadContext(context);
		when(context.getTestType()).thenReturn(TestType.WEB);
		
		when(gridConnector1.isGridActive()).thenReturn(true);
{'fraction_non_alphanumeric': 0.11086797957695113, 'fraction_numerical': 0.006783369803063457, 'mean_word_length': 6.359634997316157, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5836098', 'n_tokens_mistral': 4461, 'n_tokens_neox': 4036, 'n_words': 633}